Class Ghost
Character
entity.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Behaviour
Behaviour in which the ghost is.protected ChaseBehaviour
Object that allows us to assign a specificChaseBehaviour
class.private Position
Position that the ghost tries to reach when its behaviour isSCATTER
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if another object instance is equal to this ghost.Gets the behaviour that the ghost currently is in.Getter of the attributescatterPosition
.private Position
Returns the target position of the ghosts according to its current behaviour.boolean
hit()
Checks if the ghost hits Pacman, i.e. if ghost's position and Pacman's position are the same.void
kill()
Kills the ghost and add its points to the level's score.void
move()
Moves the ghost according to the game rules.
If the targetPosition isnull
(e.g. because ghost's behavior is INACTIVE}, then the ghost does not move.private void
Decreases thebehaviour
's duration and changes the ghost's behaviour to the next behaviour ifduration
is 0.void
reset()
Resets the ghost back to its start position where it is "alive" (as the Character does) and also with abehaviour
ofINACTIVE
and facingUP
.void
setBehaviour
(Behaviour behaviour) Sets the current behaviour of the ghost.
Its duration overrides the value of the attributeduration
.private void
setScatterPosition
(Position scatterPosition) Setter of the attributescatterPosition
.toString()
Represents this ghost in a comma-separated string format.
Format is: "x,y,DIRECTION,BEHAVIOUR:behaviourDuration".
DIRECTION is the uppercase enum type value forDirection
.
BEHAVIOUR is the uppercase enum type value forBehaviour
.
Example:
"2,3,UP,SCATTER,3"Methods inherited from class edu.uoc.pacman.model.entities.characters.Character
alive, getDirection, getDuration, getLevel, getStartPosition, isDead, setDirection, setDuration, setLevel
Methods inherited from class edu.uoc.pacman.model.entities.Entity
getPosition, getSprite, isPathable, setPathable, setPosition, setSprite
-
Field Details
-
behaviour
Behaviour in which the ghost is. -
scatterPosition
Position that the ghost tries to reach when its behaviour isSCATTER
. -
chaseBehaviour
Object that allows us to assign a specificChaseBehaviour
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 isSCATTER
.direction
- The direction that the ghost faces.behaviour
- Current behaviour of the ghost.sprite
- Value ofSprite
linked to the entity.level
- Reference to the current level object.
-
-
Method Details
-
getBehaviour
Gets the behaviour that the ghost currently is in.- Returns:
- Current value of the attribute
behaviour
.
-
setBehaviour
Sets the current behaviour of the ghost.
Its duration overrides the value of the attributeduration
. If thebehaviour
parameter isnull
, then no changes are made.- Parameters:
behaviour
- New value for the attributebehaviour
.
-
nextBehaviour
private void nextBehaviour()Decreases thebehaviour
's duration and changes the ghost's behaviour to the next behaviour ifduration
is 0. The value of the attributeduration
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 abehaviour
ofINACTIVE
and facingUP
. -
equals
Checks if another object instance is equal to this ghost. Ghosts are equal if they have the same "dead status", behaviour, direction, position (remember thatPosition
overridesequals
) and its duration is identical. -
toString
Represents this ghost in a comma-separated string format.
Format is: "x,y,DIRECTION,BEHAVIOUR:behaviourDuration".
DIRECTION is the uppercase enum type value forDirection
.
BEHAVIOUR is the uppercase enum type value forBehaviour
.
Example:
"2,3,UP,SCATTER,3" -
getScatterPosition
Getter of the attributescatterPosition
.- Returns:
- The current value of the attribute
scatterPosition
.
-
setScatterPosition
Setter of the attributescatterPosition
.- Parameters:
scatterPosition
- New value for the attributescatterPosition
.
-
getTargetPosition
Returns the target position of the ghosts according to its current behaviour.-
CHASE: the value returned by
ChaseBehaviour
'sgetChasePosition
. -
SCATTER, FRIGHTENED: the value returned by
getScatterPosition
. -
Other values:
null
.
- Returns:
- The target position according to the ghost's current behaviour.
-
CHASE: the value returned by
-
move
public void move()Moves the ghost according to the game rules.
If the targetPosition isnull
(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.
-
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 toINACTIVE
and Pacman's state isNORMAL
, then pacman must be killed.When either the ghost or Pacman are killed, then this method returns
true
. Otherwise,false
. -
kill
public void kill()Kills the ghost and add its points to the level's score. Moreover, it assignsINACTIVE
to itsbehaviour
.
-