Package playground
Class SpaceInvadersLevel
java.lang.Object
playground.Playground
playground.SpaceInvadersLevel
- Direct Known Subclasses:
Level1
,Level2
,Level3
,Level4
,Level5to7
,LevelBoss
,LevelHitTwice
,LevelMovingHitObjects
,LevelMovingObjects
,LevelWithBox
Class that realizes all the game logic of a very simple game level. The level contains for now
only two objects that are
GameObject
subclasses: FallingStar
and
EgoObject
. Functions performed by this class are:
- initially set up the level, spawn all object etc., in method
prepareLevel(java.lang.String)
- React to game events in
actionIfEgoCollidesWithCollect(GameObject, GameObject)
,actionIfEgoCollidesWithEnemy(GameObject, GameObject)
, etc. - define basic object movement rules for all objects in the level in the various
ObjectController subclasses:
EgoController
andFallingStarController
.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BufferedImage[]
protected double[]
protected static final double
protected static final int
protected static final int
protected boolean
protected static final double
protected static final double
static final double
protected Animation
protected static final double
protected static final double
protected static final double
protected static final double
protected static final Color
protected static final double
protected Animation
protected BufferedImage[]
protected double[]
protected File
protected static final double
protected static final int
protected boolean
protected int
protected static final int
protected static final int
protected static final int
protected static final double
static final double
protected File
protected static final double
protected static final double
protected static final double
protected double
Fields inherited from class playground.Playground
addables, canvasX, canvasY, flags, FLAGS_ALL, FLAGS_GLOBAL, FLAGS_LEVEL, gameObjects, gameTime, keyEvents, level, mouseEvents, pausedFlag, removables, timeStep
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) void
actionIfEgoCollidesWithCollect
(GameObject collect, GameObject ego) implements game behavior if ego object of player touches a collectableGameObject
.(package private) void
actionIfEgoCollidesWithEnemy
(GameObject enemy, GameObject ego) implements behaviour of game when ego object is touching an enemy.(package private) void
actionIfEgoObjectIsHit
(GameObject eshot, GameObject ego) implements what happens if the eog object of player is hit by a shot.(package private) void
actionIfEnemyIsHit
(GameObject e, GameObject shot) implements game behavior if an enemy object is hit by a players' shot.void
applies the logic of the level: For now, this is just about deleting shots that are leaving the screen and calling methods 'actionIf..' in case objects collide.(package private) double
calculates and returns the probability that an enemy shots.protected double
returns a number representing the speed of an enemy object in X direction (pixels/second)protected double
returns a number representing the speed of an enemy object in Y direction (pixels/second)protected int
returns the maximum number of collectables' instances (which are created at level start)protected int
returns the maximum number of enemy instances (which are created at level start)(package private) void
(package private) void
(package private) void
(package private) ObjectController
protected void
protected GameObject
createEnemyShotObject
(GameObject parentObject, String name, ObjectController limitedTimeController) (package private) void
createExplosion
(double gameTime, GameObject e, String basename, double interval, Color color) (package private) GameObject
createSingleCollect
(String name) (package private) GameObject
createSingleEnemy
(String name, double x_enemy, double y_enemy, double vx_enemy, double vy_enemy, ObjectController enemyController, double gameTime) (package private) void
boolean
gameOver()
protected String
simply returns the text that should be displayed at level startboolean
int
calculates and returns the preferred size of the level (in pixel) for X-directionint
calculates and returns the preferred size of the level (in pixel) for Y-directionvoid
prepareLevel
(String id) initially sets up the level.void
(re)draws the level but NOT the objects, they draw themselves.protected void
Adds ego object and stars and displays startup message.Methods inherited from class playground.Playground
addObject, addObjectNow, collectObjects, deleteObject, deleteObjectNow, getGameTime, getGlobalFlag, getKeyEvents, getLevelFlag, getMouseEvents, getObject, getOrCreateGlobalFlag, getOrCreateLevelFlag, getSizeX, getSizeY, getTimestep, isPaused, processKeyEvents, processMouseEvents, redraw, reset, resetFlags, setGameTime, setGlobalFlag, setLevelFlag, setPaused, setTimestep, togglePause, updateObjects
-
Field Details
-
SHOTSPEED
public static final double SHOTSPEED- See Also:
-
EGOSPEED
public static final double EGOSPEED- See Also:
-
LEVEL2STARS
protected static final int LEVEL2STARS- See Also:
-
BONUS_DURATION
protected static final double BONUS_DURATION- See Also:
-
ENEMYSPEEDX
protected static final double ENEMYSPEEDX- See Also:
-
ENEMYSPEEDY
protected static final double ENEMYSPEEDY- See Also:
-
ENEMYSCALE
protected static final double ENEMYSCALE- See Also:
-
ENEMYSHOTSPEED
protected static final double ENEMYSHOTSPEED- See Also:
-
NRSHARDS
protected static final int NRSHARDS- See Also:
-
EXPL_DURATION
protected static final double EXPL_DURATION- See Also:
-
NR_ENEMIES
protected static final int NR_ENEMIES- See Also:
-
NR_COLLECT
protected static final int NR_COLLECT- See Also:
-
EXPL_COLOR
-
SHARDSPEED
protected static final double SHARDSPEED- See Also:
-
STARSPEED
protected static final double STARSPEED- See Also:
-
STARTTEXTSPEED
protected static final double STARTTEXTSPEED- See Also:
-
STARTPERIOD
protected static final double STARTPERIOD- See Also:
-
DYING_INTERVAL
protected static final double DYING_INTERVAL- See Also:
-
CANVASX
protected static final int CANVASX- See Also:
-
CANVASY
protected static final int CANVASY- See Also:
-
EGORAD
protected static final double EGORAD- See Also:
-
LEVEL_INIT_TIME
protected static final double LEVEL_INIT_TIME- See Also:
-
nextShot
protected int nextShot -
lost
protected boolean lost -
doneLevel
protected boolean doneLevel -
starttime
protected double starttime -
smash
-
laser
-
alienImage
-
alienshowTime
protected double[] alienshowTime -
heartImage
-
heartshowTime
protected double[] heartshowTime -
enemyAnim
-
heartAnim
-
-
Constructor Details
-
SpaceInvadersLevel
public SpaceInvadersLevel()
-
-
Method Details
-
prepareLevel
initially sets up the level. Not called by user interaction, but called every time a layer is restarted from scratch. So make sure that this is possible. Here, resources are loaded only once even if method is called several times.- Specified by:
prepareLevel
in classPlayground
- Parameters:
id
- String identifies level.
-
redrawLevel
(re)draws the level but NOT the objects, they draw themselves. Called by the main game loop. This method mainly draws the background and the scoreboard.- Specified by:
redrawLevel
in classPlayground
- Parameters:
g2
- Graphics2D object that can, and should be, draw on.
-
applyGameLogic
public void applyGameLogic()applies the logic of the level: For now, this is just about deleting shots that are leaving the screen and calling methods 'actionIf..' in case objects collide.- Specified by:
applyGameLogic
in classPlayground
-
gameOver
public boolean gameOver()- Specified by:
gameOver
in classPlayground
-
levelFinished
public boolean levelFinished()- Specified by:
levelFinished
in classPlayground
-
preferredSizeX
public int preferredSizeX()calculates and returns the preferred size of the level (in pixel) for X-direction- Specified by:
preferredSizeX
in classPlayground
-
preferredSizeY
public int preferredSizeY()calculates and returns the preferred size of the level (in pixel) for Y-direction- Specified by:
preferredSizeY
in classPlayground
-
setupInitialState
protected void setupInitialState()Adds ego object and stars and displays startup message. Is called from applyGameLogic -
getStartupMessage
simply returns the text that should be displayed at level start- Returns:
- a string that is displayed at start. Should be not longer than 30 characters.
-
calcEnemySpeedX
protected double calcEnemySpeedX()returns a number representing the speed of an enemy object in X direction (pixels/second)- Returns:
- a positive value
-
calcEnemySpeedY
protected double calcEnemySpeedY()returns a number representing the speed of an enemy object in Y direction (pixels/second)- Returns:
- a positive value
-
calcNrEnemies
protected int calcNrEnemies()returns the maximum number of enemy instances (which are created at level start)- Returns:
- a positive value
-
calcNrCollect
protected int calcNrCollect()returns the maximum number of collectables' instances (which are created at level start)- Returns:
- a positive value
-
createEnemyShotObject
protected GameObject createEnemyShotObject(GameObject parentObject, String name, ObjectController limitedTimeController) -
createEnemyShot
-
calcEnemyShotProb
double calcEnemyShotProb()calculates and returns the probability that an enemy shots. Used bycreateEnemyShot(GameObject)
.- Returns:
- a positive value between 0 (no chance) to 1 (for sure).
-
createEnemyController
ObjectController createEnemyController() -
createSingleEnemy
GameObject createSingleEnemy(String name, double x_enemy, double y_enemy, double vx_enemy, double vy_enemy, ObjectController enemyController, double gameTime) -
createSingleCollect
-
createEnemies
void createEnemies() -
createCollectables
void createCollectables() -
createEgoObject
void createEgoObject() -
createStars
void createStars() -
createExplosion
-
actionIfEnemyIsHit
implements game behavior if an enemy object is hit by a players' shot. It creates an explosion effect, plays a sound and adds 200 points to the current score (and it removes the enemy object and the shot object).- Parameters:
e
- enemy which was hitshot
- the shot object that hit the enemy
-
actionIfEgoCollidesWithCollect
implements game behavior if ego object of player touches a collectableGameObject
. Here it adds one extra life and displays a text for a limited time on screen "Extra life!!". The duration is set in constantEXPL_DURATION
.- Parameters:
collect
- item touched by egoego
- the players ego object
-
actionIfEgoCollidesWithEnemy
implements behaviour of game when ego object is touching an enemy. It displays a text "AUAA!!" for a certain time on the game screen. Time span is defined as constant inBONUS_DURATION
.- Parameters:
enemy
- the enemy that was hitego
- the ego object of the player
-
actionIfEgoObjectIsHit
implements what happens if the eog object of player is hit by a shot. It removes the shot from screen, reduces lives by 1, removes the ego and end current playing.- Parameters:
eshot
- the shot object that hits the egoego
- the ego object of the player
-