Package playground

Class SpaceInvadersLevel

java.lang.Object
playground.Playground
playground.SpaceInvadersLevel
Direct Known Subclasses:
Level1, Level2, Level3, Level4, Level5to7, LevelBoss, LevelHitTwice, LevelMovingHitObjects, LevelMovingObjects, LevelWithBox

public class SpaceInvadersLevel extends Playground
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:
  • Field Details

  • Constructor Details

    • SpaceInvadersLevel

      public SpaceInvadersLevel()
  • Method Details

    • prepareLevel

      public void prepareLevel(String id)
      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 class Playground
      Parameters:
      id - String identifies level.
    • redrawLevel

      public void redrawLevel(Graphics2D g2)
      (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 class Playground
      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 class Playground
    • gameOver

      public boolean gameOver()
      Specified by:
      gameOver in class Playground
    • levelFinished

      public boolean levelFinished()
      Specified by:
      levelFinished in class Playground
    • preferredSizeX

      public int preferredSizeX()
      calculates and returns the preferred size of the level (in pixel) for X-direction
      Specified by:
      preferredSizeX in class Playground
    • preferredSizeY

      public int preferredSizeY()
      calculates and returns the preferred size of the level (in pixel) for Y-direction
      Specified by:
      preferredSizeY in class Playground
    • setupInitialState

      protected void setupInitialState()
      Adds ego object and stars and displays startup message. Is called from applyGameLogic
    • getStartupMessage

      protected String 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

      protected void createEnemyShot(GameObject e)
    • calcEnemyShotProb

      double calcEnemyShotProb()
      calculates and returns the probability that an enemy shots. Used by createEnemyShot(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

      GameObject createSingleCollect(String name)
    • createEnemies

      void createEnemies()
    • createCollectables

      void createCollectables()
    • createEgoObject

      void createEgoObject()
    • createStars

      void createStars()
    • createExplosion

      void createExplosion(double gameTime, GameObject e, String basename, double interval, Color color)
    • actionIfEnemyIsHit

      void actionIfEnemyIsHit(GameObject e, GameObject shot)
      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 hit
      shot - the shot object that hit the enemy
    • actionIfEgoCollidesWithCollect

      void actionIfEgoCollidesWithCollect(GameObject collect, GameObject ego)
      implements game behavior if ego object of player touches a collectable GameObject. Here it adds one extra life and displays a text for a limited time on screen "Extra life!!". The duration is set in constant EXPL_DURATION.
      Parameters:
      collect - item touched by ego
      ego - the players ego object
    • actionIfEgoCollidesWithEnemy

      void actionIfEgoCollidesWithEnemy(GameObject enemy, GameObject ego)
      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 in BONUS_DURATION .
      Parameters:
      enemy - the enemy that was hit
      ego - the ego object of the player
    • actionIfEgoObjectIsHit

      void actionIfEgoObjectIsHit(GameObject eshot, GameObject ego)
      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 ego
      ego - the ego object of the player