Package edu.uoc.pacman.model
Class Level
java.lang.Object
edu.uoc.pacman.model.Level
Represents a level in the game.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
The name of the level configuration file.Ghosts that are part of the level.private int
The height of the map/labyrinth.private int
Number of lives in the level.Stores the items which are part of the map/labyrinth.private static final int
The minimum height that the map must have.private static final int
The minimum width that the map must have.private Pacman
Reference toPacman
object, i.e. the player.private int
Level's score.private int
Number that allows us to manage when the actions happen.private static final int
It is the refresh time.private int
The width of the map/labyrinth. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addMapItem
(MapItem item) Addsitem
to themapItemList
as long asitem
is notnull
.void
addPoints
(int points) Add the value ofpoints
to the attributescore
as long aspoints
is greater than 0.void
Decreases 1 the number of lives as long as the number of lives is not zero.Gets the firstBlinky
object in the Ghost list of the level.Getter of the attributefileName
private String
This is a helper method forparse()
which returns the first non-empty and non-comment line from the reader.Getter of the attributeghostList
.int
Getter of the attributeheight
.int
getLives()
Getter of the attributelives
.getMapItem
(int x, int y) Returns the map/labyrinth item which is in the position (x,y).getMapItem
(Position position) Returns the map/labyrinth item which is inposition
.Getter of the attributemapItemList
.Returns an Iterator of the attributemapItemList
.Getter ot the attributepacman
.int
getScore()
Getter of the attributescore
.int
getWidth()
Getter of the attributewidth
.boolean
hasWon()
Checks if the level was finished.void
Increases 1 the number of lives.boolean
isPathable
(Position position) Given a position, it indicates if such a position is pathable or not.private void
parse()
Parses/Reads level's data from the given file.
It also checks which the board's requirements are met.void
removeMapItem
(MapItem item) Removesitem
from themapItemList
.private void
setFileName
(String fileName) Setter of the attributefileName
.void
Sets the behaviour of all the ghosts in the level to FRIGHTENED.private void
setHeight
(int height) Setter of the attributeheight
.private void
setLives
(int lives) Setter of the attributelives
.private void
setWidth
(int width) Setter of the attributewidth
.void
update()
Refreshes/Updates the game when needed.
-
Field Details
-
MIN_WIDTH
private static final int MIN_WIDTHThe minimum width that the map must have.- See Also:
-
MIN_HEIGHT
private static final int MIN_HEIGHTThe minimum height that the map must have.- See Also:
-
fileName
The name of the level configuration file. -
width
private int widthThe width of the map/labyrinth. -
height
private int heightThe height of the map/labyrinth. -
mapItemList
Stores the items which are part of the map/labyrinth. -
pacman
Reference toPacman
object, i.e. the player. -
ghostList
Ghosts that are part of the level. -
score
private int scoreLevel's score. -
lives
private int livesNumber of lives in the level. -
tick
private int tickNumber that allows us to manage when the actions happen. An action will be performed, when tick == UPDATE_GAME. -
UPDATE_GAME
private static final int UPDATE_GAMEIt is the refresh time. In other words, it is the speed of the game.- See Also:
-
-
Constructor Details
-
Level
Constructor with argument.- Parameters:
filename
- Value for the attributefileName
.lives
- Initial number of lives in the level.- Throws:
LevelException
- If there are any problems while parsing the level configuration file.
-
-
Method Details
-
setFileName
Setter of the attributefileName
.- Parameters:
fileName
- New value for the attributefileName
.
-
getFileName
Getter of the attributefileName
- Returns:
- Current value of the attribute
fileName
-
getLives
public int getLives()Getter of the attributelives
.- Returns:
- The value of the attribute
lives
.
-
setLives
private void setLives(int lives) Setter of the attributelives
.- Parameters:
lives
- Number of lives to assign to the attributelives
.
-
increaseLives
public void increaseLives()Increases 1 the number of lives. -
decreaseLives
public void decreaseLives()Decreases 1 the number of lives as long as the number of lives is not zero. -
parse
Parses/Reads level's data from the given file.
It also checks which the board's requirements are met.- Throws:
LevelException
- When there is any error while parsing the file or some board's requirement is not satisfied.
-
getFirstNonEmptyLine
This is a helper method forparse()
which returns the first non-empty and non-comment line from the reader.- Parameters:
br
- BufferedReader object to read from.- Returns:
- First line that is a parsable line, or
null
there are no lines to read. - Throws:
IOException
- if the reader fails to read a line.
-
getWidth
public int getWidth()Getter of the attributewidth
.- Returns:
- The current value of the attribute
width
.
-
setWidth
Setter of the attributewidth
.- Parameters:
width
- New value for the attributewidth
.- Throws:
LevelException
- Whenwidth
is less thanMIN_WIDTH
.
-
getHeight
public int getHeight()Getter of the attributeheight
.- Returns:
- The current value of the attribute
height
.
-
setHeight
Setter of the attributeheight
.- Parameters:
height
- New value for the attributeheight
.- Throws:
LevelException
- Whenheight
is less thanMIN_HEIGHT
.
-
getMapItemList
Getter of the attributemapItemList
.- Returns:
- The current value of the attribute
mapItemList
.
-
getMapItemListIterator
Returns an Iterator of the attributemapItemList
.- Returns:
- Iterator of the attribute
mapItemList
.
-
getMapItem
Returns the map/labyrinth item which is in the position (x,y).- Parameters:
x
- Value of the position in the X axis.y
- Value of the position in the Y axis.- Returns:
- If any, the item which is the position (x,y). Otherwise,
null
.
-
getMapItem
Returns the map/labyrinth item which is inposition
.- Parameters:
position
- The position where the item that we want to get is.- Returns:
- If any, the item which is
position
. Otherwise,null
. - Throws:
NullPointerException
- Whenposition
isnull
.
-
addMapItem
Addsitem
to themapItemList
as long asitem
is notnull
.- Parameters:
item
- Item which we want to add.
-
removeMapItem
Removesitem
from themapItemList
.- Parameters:
item
- Item which we want to remove.
-
getGhostList
Getter of the attributeghostList
.- Returns:
- The current value of the attribute
ghostList
.
-
getScore
public int getScore()Getter of the attributescore
.- Returns:
- The current value of the attribute
score
.
-
addPoints
public void addPoints(int points) Add the value ofpoints
to the attributescore
as long aspoints
is greater than 0.- Parameters:
points
- Number to add to the attributescore
.
-
getPacman
Getter ot the attributepacman
.- Returns:
- The current value of the attribute
pacman
.
-
getBlinky
Gets the firstBlinky
object in the Ghost list of the level. -
isPathable
Given a position, it indicates if such a position is pathable or not.- Parameters:
position
- Position which must be checked.- Returns:
true
if the position is pathable. Otherwise,false
.
-
setGhostsFrightened
public void setGhostsFrightened()Sets the behaviour of all the ghosts in the level to FRIGHTENED. -
hasWon
public boolean hasWon()Checks if the level was finished. This happens when all the objects that areScorable
andPickable
have been picked.- Returns:
true
if the level was finished/won. Otherwise,false
.
-
update
public void update()Refreshes/Updates the game when needed.
-