Class Game

java.lang.Object
controller.Game
All Implemented Interfaces:
Runnable, EventListener, EntityEventListener, UserEventListener

public class Game extends Object implements Runnable, EntityEventListener, UserEventListener
The main hook of our game. This class will both perform the necessary updates directly on the display of the actual game playing area and act as a manager for the user control interface (the VIEW) and central mediator for the game logic. It is the CONTROLLER. In presents the game playing area and contains the game loop that cycles round all entities in the game asking them to update their state (at least updateState) and then drawing them in the appropriate place. With the help of an inner class it will also allow the player to control the main ship (updateState and shoot). As the Controller it will be informed when the user wants something to happen via the user control interface and will also respond when entities within our game detect events (e.g. alien killed, player died) and will take appropriate game actions.
  • Constructor Details

  • Method Details

    • run

      public void run()
      run the game
      Specified by:
      run in interface Runnable
    • setUpForNewGame

      public final void setUpForNewGame()
      Description copied from interface: UserEventListener
      User wants to start a new game
      Specified by:
      setUpForNewGame in interface UserEventListener
    • tryToFire

      public void tryToFire()
      Attempt to fire a shot from the player. Its called "try" since we must first check that the player can fire at this point, i.e. has he/she waited long enough between shots
    • randomAlienFire

      public void randomAlienFire()
      Randomly choose an alien to fire at the players ship. Only allow this a a certain speed (ie time between shots)
    • gameLoop

      public void gameLoop()
      The main game loop. This loop is running during all game play and is responsible for the following activities:

      - Working out the elapsed time (in milliseconds) to update moves - Moving the game entities - Drawing the screen contents (entities, text) - Updating game events

    • getGamePanel

      public JPanel getGamePanel()
      Returns:
      The panel the game is played in.
    • entityDied

      public void entityDied(EntityDiedEvent e)
      Something in the game has died so register this as a game event (by adding it to the game events queue) so that the game can respond (will need to check if its the ship which means we LOSE or if its the last alien which means we WIN!)
      Specified by:
      entityDied in interface EntityEventListener
      Parameters:
      e - The updateState events details
    • entityMoved

      public void entityMoved(EntityMoveEvent e)
      Something in the game has moved so register this as an event (by adding it to the game events queue) so that the game can respond (will need to check for collision with other game entities)
      Specified by:
      entityMoved in interface EntityEventListener
      Parameters:
      e - The updateState events details
    • collisionHasOccured

      public void collisionHasOccured(EntityHitEvent e)
      Only game logic needed when an entity hits another entity is when an AlienEntity hits the bottom border. Then the game finishes.
      Specified by:
      collisionHasOccured in interface EntityEventListener
      Parameters:
      e - EntityHitEvent
    • exitGame

      public void exitGame(UserEvent e)
      Exits the game
      Specified by:
      exitGame in interface UserEventListener
      Parameters:
      e - a UserEvent
    • startPlaying

      public void startPlaying(UserEvent e)
      Method called as a result of a UserEvent - wants to start the game
      Specified by:
      startPlaying in interface UserEventListener
      Parameters:
      e - details of the View that the event came from
    • pauseGame

      public void pauseGame(UserEvent e)
      Needed to be a UserEventListener Method called as a result of a UserEvent - wants to pause the game
      Specified by:
      pauseGame in interface UserEventListener
      Parameters:
      e - details of the View that the event came from
    • saveCurrentGameResults

      public void saveCurrentGameResults(UserEvent e)
      This method is needed in order to be a UserEventListener Method called as a result of a UserEvent - user wants to save the setting for this game
      Specified by:
      saveCurrentGameResults in interface UserEventListener
      Parameters:
      e - details of the View that the event came from but at the moment the data provide is not actually needed
    • changeFiringInterval

      public void changeFiringInterval(UserEvent e)
      changeFiringInterval
      Specified by:
      changeFiringInterval in interface UserEventListener
      Parameters:
      e - UserEvent
    • changeMoveSpeed

      public void changeMoveSpeed(UserEvent e)
      changeMoveSpeed
      Specified by:
      changeMoveSpeed in interface UserEventListener
      Parameters:
      e - UserEvent
    • undoLastSave

      public void undoLastSave(UserEvent userEvent)
      Specified by:
      undoLastSave in interface UserEventListener
    • getUsersHighScores

      public void getUsersHighScores(UserEvent userEvent) throws Exception
      Gets the users high scores from the database then asks the View to display the high scores
      Specified by:
      getUsersHighScores in interface UserEventListener
      Parameters:
      userEvent - Contains the users details ie the username
      Throws:
      Exception
    • getAllHighScores

      public void getAllHighScores()
      Gets the game high scores from the database then asks the View to display the high scores
      Specified by:
      getAllHighScores in interface UserEventListener
    • changeSettingForWantingIntroInfo

      public void changeSettingForWantingIntroInfo(boolean wantIntroInfo)
      Specified by:
      changeSettingForWantingIntroInfo in interface UserEventListener
    • isPaused

      public boolean isPaused()
      Method returns the value of the variable paused.
      Returns:
      true if paused false otherwise.
    • setPaused

      public void setPaused(boolean paused)
      Method pauses the game to allow changes to the current game (eg firing interval) to be implemented.
      Parameters:
      paused - used to set whether game is paused or not