All Implemented Interfaces:
Hitable, Movable

public class Pacman extends Character
Represents the player object that appears in the game, which is called Pacman.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private State
    Stores the state of the Pacman.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pacman(Position startPosition, Direction direction, State state, Level level)
    Constructor with arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    eat()
    Performs the eat action.
    Getter of the attribute state.
    boolean
    hit()
    Checks if Pacman is in the same position which any ghost.
    void
    Kills Pacman and decreases one life in the current level.
    void
    Moves Pacman across the board only one step.
    private void
    Decreases the behaviour's duration and changes the ghost's behaviour to the next behaviour if duration is 0.
    void
    Resets Pacman back to its start position where it is "alive" (as Character does) and also with a state of INVINCIBLE and facing UP.
    void
    Sets the direction of Pacman.
    If the new direction is null, then the direction is not set and remains the same.

    Because Pacman has 4 different sprites depending on its direction, this method also updates the value of the attribute sprite, i.e.
    void
    setState(State state)
    Setter of the attribute state.
    If the new state is null, then the state is not set and remains the same.

    Methods inherited from class edu.uoc.pacman.model.entities.characters.Character

    alive, getDirection, getDuration, getLevel, getStartPosition, isDead, setDuration, setLevel, toString

    Methods inherited from class edu.uoc.pacman.model.entities.Entity

    getPosition, getSprite, isPathable, setPathable, setPosition, setSprite

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • state

      private State state
      Stores the state of the Pacman.
  • Constructor Details

    • Pacman

      public Pacman(Position startPosition, Direction direction, State state, Level level)
      Constructor with arguments.
      Parameters:
      startPosition - The initial/start position of the Pacman. Its value is the one which is provided by the level configuration file.
      direction - The direction that the Pacman faces.
      state - The initial state of Pacman.
      level - Reference to the current level object.
  • Method Details

    • getState

      public State getState()
      Getter of the attribute state.
      Returns:
      The current value of the attribute state.
    • setState

      public void setState(State state)
      Setter of the attribute state.
      If the new state is null, then the state is not set and remains the same.
      Parameters:
      state - New value for the attribute state.
    • nextState

      private void nextState()
      Decreases the behaviour's duration and changes the ghost's behaviour to the next behaviour if duration is 0. The value of the attribute duration cannot be negative.
      • CHASE goes to SCATTER
      • FRIGHTENED and SCATTER and INACTIVE go to CHASE.
    • reset

      public void reset()
      Resets Pacman back to its start position where it is "alive" (as Character does) and also with a state of INVINCIBLE and facing UP.
      Overrides:
      reset in class Character
    • move

      public void move()
      Moves Pacman across the board only one step.

      If the MapItem which is in the potential new position is pathable, then this method moves Pacman into this position. Otherwise, Pacman stays in its current position.

      After moving, Pacman will eat the item that occupies the cell/tile/position and will add its score to the game score.

      Lastly, it will invoke the hit method.

      In any case, this method invokes nextState.

    • setDirection

      public void setDirection(Direction direction)
      Sets the direction of Pacman.
      If the new direction is null, then the direction is not set and remains the same.

      Because Pacman has 4 different sprites depending on its direction, this method also updates the value of the attribute sprite, i.e. PACMAN_UP, PACMAN_LEFT, PACMAN_RIGHT and PACMAN_DOWN.
      Specified by:
      setDirection in interface Movable
      Overrides:
      setDirection in class Character
      Parameters:
      direction - New value for the attribute direction.
      Since:
      setDirection in interface Movable.
    • eat

      private void eat()
      Performs the eat action. This means which picks Pickable objects up and add its points to the level's score, if it is needed. Remember that some Pickable objects are Scorable ones as well.

      If Pacman picks an energizer up, then the behaviour of all the ghosts must be FRIGHTENED. Moreover, Pacman's state must be EATER.

      If Pacman picks a life up, then one life is increased for the current level.

      When any Pickable objects is picked up, then it is removed from the labyrinth/map and replaced by a Path item.

    • hit

      public boolean hit()
      Checks if Pacman is in the same position which any ghost. If this happens, then checks ghost's status:
      • If Pacman's state is INVINCIBLE, then nothing happens and this method returns false.
      • If the ghost is FRIGHTENED, then the ghost is killed. This method returns true.
      • If the ghost is INACTIVE, then nothing happens and this method returns false.
      Returns:
      true if Pacman collides with any ghost and the aforesaid cases are met. Otherwise, false.
      Since:
      hit in interface Hitable.
    • kill

      public void kill()
      Kills Pacman and decreases one life in the current level. Moreover, it assigns INVINCIBLE to its state.
      Overrides:
      kill in class Character
      Since:
      kill in class Character.