All Implemented Interfaces:
Hitable, Movable, Scorable
Direct Known Subclasses:
Blinky, Clyde, Inky, Pinky

public abstract class Ghost extends Character implements Scorable
Represents an abstract Ghost class which defines a specific Character entity.
  • Field Details

    • behaviour

      private Behaviour behaviour
      Behaviour in which the ghost is.
    • scatterPosition

      private Position scatterPosition
      Position that the ghost tries to reach when its behaviour is SCATTER.
    • chaseBehaviour

      protected ChaseBehaviour chaseBehaviour
      Object that allows us to assign a specific ChaseBehaviour class.
  • Constructor Details

    • Ghost

      protected Ghost(Position startPosition, Position scatterPosition, Direction direction, Behaviour behaviour, Sprite sprite, Level level)
      Constructor with arguments.
      Parameters:
      startPosition - The initial/start position of the ghost. Its value is the one which is provided by the level configuration file.
      scatterPosition - The position which the ghost tries to reach when its behaviour is SCATTER.
      direction - The direction that the ghost faces.
      behaviour - Current behaviour of the ghost.
      sprite - Value of Sprite linked to the entity.
      level - Reference to the current level object.
  • Method Details

    • getBehaviour

      public Behaviour getBehaviour()
      Gets the behaviour that the ghost currently is in.
      Returns:
      Current value of the attribute behaviour.
    • setBehaviour

      public void setBehaviour(Behaviour behaviour)
      Sets the current behaviour of the ghost.
      Its duration overrides the value of the attribute duration. If the behaviour parameter is null, then no changes are made.
      Parameters:
      behaviour - New value for the attribute behaviour.
    • nextBehaviour

      private void nextBehaviour()
      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 the ghost back to its start position where it is "alive" (as the Character does) and also with a behaviour of INACTIVE and facing UP.
      Overrides:
      reset in class Character
    • equals

      public boolean equals(Object o)
      Checks if another object instance is equal to this ghost. Ghosts are equal if they have the same "dead status", behaviour, direction, position (remember that Position overrides equals) and its duration is identical.
      Overrides:
      equals in class Object
      Returns:
      true if equal, false otherwise.
    • toString

      public String toString()
      Represents this ghost in a comma-separated string format.
      Format is: "x,y,DIRECTION,BEHAVIOUR:behaviourDuration".
      DIRECTION is the uppercase enum type value for Direction.
      BEHAVIOUR is the uppercase enum type value for Behaviour.
      Example:
      "2,3,UP,SCATTER,3"
      Overrides:
      toString in class Character
      Returns:
      "x,y,DIRECTION,BEHAVIOUR:behaviourDuration"
    • getScatterPosition

      public Position getScatterPosition()
      Getter of the attribute scatterPosition.
      Returns:
      The current value of the attribute scatterPosition.
    • setScatterPosition

      private void setScatterPosition(Position scatterPosition)
      Setter of the attribute scatterPosition.
      Parameters:
      scatterPosition - New value for the attribute scatterPosition.
    • getTargetPosition

      private Position getTargetPosition()
      Returns the target position of the ghosts according to its current behaviour.
      • CHASE: the value returned by ChaseBehaviour's getChasePosition.
      • SCATTER, FRIGHTENED: the value returned by getScatterPosition.
      • Other values: null.
      Returns:
      The target position according to the ghost's current behaviour.
    • move

      public void move()
      Moves the ghost according to the game rules.
      If the targetPosition is null (e.g. because ghost's behavior is INACTIVE}, then the ghost does not move.

      In order to decide how to reach the targetPosition, i.e in which direction the ghost should move, this method calculates the euclidean distance of the 4 potential positions and choose the one with the smallest distance.

      The new position is the one that meets the all three requirements below:

      • It has the smallest distance to the targetPosition.
      • It is pathable, and
      • its direction is not opposite to the current one.
      • if two or more directions have the same distance to the targetPosition, then the new position will be the last direction in the enum Direction.

      In addition to set the new position, this method sets the direction and invokes the hit method in order to check if the ghost hits Pacman in the new position.

      In any case, this method invokes nextBehaviour.

      Hint: Use Double.MAX_VALUE.

      Specified by:
      move in interface Movable
      Since:
      move in interface Movable.
    • hit

      public boolean hit()
      Checks if the ghost hits Pacman, i.e. if ghost's position and Pacman's position are the same.

      If both positions are identical, then the ghost must be killed when its behaviour is FRIGHTENED. Otherwise, if the ghost's behaviour is different to INACTIVE and Pacman's state is NORMAL, then pacman must be killed.

      When either the ghost or Pacman are killed, then this method returns true. Otherwise, false.

      Specified by:
      hit in interface Hitable
      Returns:
      true when the ghost and Pacman collide as long as the ghost is not INACTIVE. Otherwise, it returns false.
      Since:
      kill in interface Hitable.
    • kill

      public void kill()
      Kills the ghost and add its points to the level's score. Moreover, it assigns INACTIVE to its behaviour.
      Overrides:
      kill in class Character
      Since:
      kill in class Character.