Class Pacman
java.lang.Object
edu.uoc.pacman.model.entities.Entity
edu.uoc.pacman.model.entities.characters.Character
edu.uoc.pacman.model.entities.characters.pacman.Pacman
Represents the player object that appears in the game, which is called Pacman.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
eat()
Performs the eat action.getState()
Getter of the attributestate
.boolean
hit()
Checks if Pacman is in the same position which any ghost.void
kill()
Kills Pacman and decreases one life in the current level.void
move()
Moves Pacman across the board only one step.private void
Decreases thebehaviour
's duration and changes the ghost's behaviour to the next behaviour ifduration
is 0.void
reset()
Resets Pacman back to its start position where it is "alive" (as Character does) and also with astate
ofINVINCIBLE
and facingUP
.void
setDirection
(Direction direction) Sets the direction of Pacman.
If the new direction isnull
, 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 attributesprite
, i.e.void
Setter of the attributestate
.
If the new state isnull
, 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
-
Field Details
-
state
Stores the state of the Pacman.
-
-
Constructor Details
-
Pacman
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
Getter of the attributestate
.- Returns:
- The current value of the attribute
state
.
-
setState
Setter of the attributestate
.
If the new state isnull
, then the state is not set and remains the same.- Parameters:
state
- New value for the attributestate
.
-
nextState
private void nextState()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 Pacman back to its start position where it is "alive" (as Character does) and also with astate
ofINVINCIBLE
and facingUP
. -
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
Sets the direction of Pacman.
If the new direction isnull
, 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 attributesprite
, i.e. PACMAN_UP, PACMAN_LEFT, PACMAN_RIGHT and PACMAN_DOWN.- Specified by:
setDirection
in interfaceMovable
- Overrides:
setDirection
in classCharacter
- Parameters:
direction
- New value for the attributedirection
.- Since:
- setDirection in interface
Movable
.
-
eat
private void eat()Performs the eat action. This means which picksPickable
objects up and add its points to the level's score, if it is needed. Remember that somePickable
objects areScorable
ones as well.
If Pacman picks an energizer up, then the behaviour of all the ghosts must be
FRIGHTENED
. Moreover, Pacman's state must beEATER
.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 aPath
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 returnsfalse
. -
If the ghost is
FRIGHTENED
, then the ghost is killed. This method returnstrue
. -
If the ghost is
INACTIVE
, then nothing happens and this method returnsfalse
.
- Returns:
true
if Pacman collides with any ghost and the aforesaid cases are met. Otherwise,false
.- Since:
- hit in interface
Hitable
.
- If Pacman's state is
-
kill
public void kill()Kills Pacman and decreases one life in the current level. Moreover, it assignsINVINCIBLE
to itsstate
.
-