Package playground
Class Playground
java.lang.Object
playground.Playground
- Direct Known Subclasses:
BreakoutLevelBase
,SpaceInvadersLevel
Playground represents a level of the game, focusing on the game LOGIC, i.e., not so much on the
graphical representation. In particular, an instance of Playground
- manages the different moving or static objects in a level (e.g., collisions, adding objects,
removing objects). This is mainly done by the methods
addObject(gameobjects.GameObject)
,deleteObject(java.lang.String)
. - processes keyboard inputs provided by GameLoop in
processKeyEvents(Stack)
andprocessMouseEvents(Stack)
- represents the state of a level represented by flags. Each flag has a name (a String)
and an arbitrary value of any type. Methods:
setLevelFlag(String, Object)
,getLevelFlag(String)
. As an example, the current score is a flag usually named "points", with an Integer as a value. This value can be retrieved and manipulated using the above mentioned methods.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
static final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a graphics object to a level.void
Adds a graphics object to a level.abstract void
Method meant to be filled with own code, handles the entore game logic (collision checks, timed events, ...).collectObjects
(String substr, boolean filterInactive) Puts objects with a certain substring in their name into a LinkedLisrt and returns them.void
deleteObject
(String id) Removes a graphics object from a level.void
Removes a graphics object from a level immediately, CAUTION.abstract boolean
gameOver()
double
returns time in seconds since level startstatic Object
getGlobalFlag
(String flag) Retrieves a level-wide flag by name.getLevelFlag
(String flag) Retrieves a graphics object by name.static Object
getOrCreateGlobalFlag
(String flag, Object value) checks for existence and if not creates the new global flag with the given initial value.getOrCreateLevelFlag
(String flag, Object createValue) int
getSizeX()
int
getSizeY()
double
boolean
isPaused()
abstract boolean
abstract int
abstract int
abstract void
prepareLevel
(String level) Sets up a single level.void
processKeyEvents
(Stack<KeyEvent> keyEvents) Method meant to be filled with own code, processes Keyboard inputs.void
processMouseEvents
(Stack<MouseEvent> mouseEvents) void
redraw
(Graphics2D g2) Internal, do not call directly.abstract void
To be redefined!!void
reset()
Reinitializes the level.void
resetFlags
(int mode) mode can be: FLAGS_ALL (all), FLAGS_GLOBAL(global), FLAGs_LEVEL(level)void
setGameTime
(double s) set the game time value (in seconds)static void
setGlobalFlag
(String flag, Object value) Sets a level-wide permanent flag.setLevelFlag
(String flag, Object value) void
setPaused
(boolean p) void
setTimestep
(double s) void
void
Calls all object update methods in level.
-
Field Details
-
FLAGS_GLOBAL
public static final int FLAGS_GLOBAL- See Also:
-
FLAGS_LEVEL
public static final int FLAGS_LEVEL- See Also:
-
FLAGS_ALL
public static final int FLAGS_ALL- See Also:
-
-
Constructor Details
-
Playground
public Playground()
-
-
Method Details
-
preferredSizeX
public abstract int preferredSizeX() -
preferredSizeY
public abstract int preferredSizeY() -
addObject
Adds a graphics object to a level.- Parameters:
o
- GameObject The object to be added
-
addObjectNow
Adds a graphics object to a level.- Parameters:
o
- GameObject The object to be added
-
collectObjects
Puts objects with a certain substring in their name into a LinkedLisrt and returns them.- Parameters:
substr
- The string that must be part of the object name if object is to be considered found.filterInactive
- if set true only active objects are considered.- Returns:
- a reference to a LinkedList filled with all objects that have substr in their name
-
deleteObject
Removes a graphics object from a level.- Parameters:
id
- String The unique identifier of the object
-
deleteObjectNow
Removes a graphics object from a level immediately, CAUTION.- Parameters:
id
- String The unique identifier of the object
-
getObject
Retrieves a graphics object by name.- Parameters:
id
- String Unique id of the object- Returns:
- reference to the requested game object, or null if not found
-
setGlobalFlag
Sets a level-wide permanent flag.- Parameters:
flag
- String Q unique name in this level. If it exists value is overwritten.value
- Object Any Object can be the value of a flag!
-
setLevelFlag
-
resetFlags
public void resetFlags(int mode) mode can be: FLAGS_ALL (all), FLAGS_GLOBAL(global), FLAGs_LEVEL(level)- Parameters:
mode
- can be only one ofFLAGS_GLOBAL
FLAGS_ALL
orFLAGS_LEVEL
-
getGlobalFlag
Retrieves a level-wide flag by name.- Parameters:
flag
- String Unique flag id- Returns:
- the value associated with flag, or null if the flag does not exist.
-
getOrCreateGlobalFlag
checks for existence and if not creates the new global flag with the given initial value. Returns the value. afterwards it is guaranteed that no priorly existing value is overridden and that it definitely exists (created if not present before).- Parameters:
flag
- String name for the global flag (created if not present)value
- Object value to be stored (used only if flag was not present)- Returns:
- the current value of the flag (maybe the initial one in case flag was not there before)
-
getLevelFlag
-
getOrCreateLevelFlag
-
reset
public void reset()Reinitializes the level. -
isPaused
public boolean isPaused() -
setPaused
public void setPaused(boolean p) -
togglePause
public void togglePause() -
processKeyEvents
Method meant to be filled with own code, processes Keyboard inputs.- Parameters:
keyEvents
- all collectedKeyEvent
s collected since last game loop.
-
processMouseEvents
-
getKeyEvents
-
getMouseEvents
-
applyGameLogic
public abstract void applyGameLogic()Method meant to be filled with own code, handles the entore game logic (collision checks, timed events, ...). -
prepareLevel
Sets up a single level. Prepares all objects etc.- Parameters:
level
- String a string identifying the level number etc
-
gameOver
public abstract boolean gameOver() -
levelFinished
public abstract boolean levelFinished() -
getSizeX
public int getSizeX() -
getSizeY
public int getSizeY() -
updateObjects
public void updateObjects()Calls all object update methods in level. Internal, never call directly. -
setTimestep
public void setTimestep(double s) -
getTimestep
public double getTimestep() -
setGameTime
public void setGameTime(double s) set the game time value (in seconds)- Parameters:
s
- seconds the game is running
-
getGameTime
public double getGameTime()returns time in seconds since level start -
redrawLevel
To be redefined!! Draws mainly h level background and global information like points etc.- Parameters:
g2
- Graphics2D abstract drawing object of java Swing, used to carry out all drawing operations.
-
redraw
Internal, do not call directly.- Parameters:
g2
- Graphics2D abstract drawing object of java Swing, used to carry out all drawing operations.
-