Class Game
java.lang.Object
controller.Game
- All Implemented Interfaces:
Runnable,EventListener,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 Summary
ConstructorsConstructorDescriptionGame(UserDetails userDetails, GameSettings gameSettings, UserViewInterface userControlPanel) Construct our game. -
Method Summary
Modifier and TypeMethodDescriptionvoidchangeFiringIntervalvoidchangeMoveSpeedvoidchangeSettingForWantingIntroInfo(boolean wantIntroInfo) voidOnly game logic needed when an entity hits another entity is when an AlienEntity hits the bottom border.voidSomething 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!)voidSomething 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)voidExits the gamevoidgameLoop()The main game loop.voidGets the game high scores from the database then asks the View to display the high scoresvoidgetUsersHighScores(UserEvent userEvent) Gets the users high scores from the database then asks the View to display the high scoresbooleanisPaused()Method returns the value of the variable paused.voidNeeded to be a UserEventListener Method called as a result of a UserEvent - wants to pause the gamevoidRandomly choose an alien to fire at the players ship.voidrun()run the gamevoidThis 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 gamevoidsetPaused(boolean paused) Method pauses the game to allow changes to the current game (eg firing interval) to be implemented.final voidUser wants to start a new gamevoidMethod called as a result of a UserEvent - wants to start the gamevoidAttempt to fire a shot from the player.voidundoLastSave(UserEvent userEvent)
-
Constructor Details
-
Game
public Game(UserDetails userDetails, GameSettings gameSettings, UserViewInterface userControlPanel) throws SQLException, Exception Construct our game.- Parameters:
userDetails- User details objectgameSettings- game setting objectuserControlPanel-- Throws:
SQLExceptionException
-
-
Method Details
-
run
-
setUpForNewGame
public final void setUpForNewGame()Description copied from interface:UserEventListenerUser wants to start a new game- Specified by:
setUpForNewGamein interfaceUserEventListener
-
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
- Returns:
- The panel the game is played in.
-
entityDied
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:
entityDiedin interfaceEntityEventListener- Parameters:
e- The updateState events details
-
entityMoved
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:
entityMovedin interfaceEntityEventListener- Parameters:
e- The updateState events details
-
collisionHasOccured
Only game logic needed when an entity hits another entity is when an AlienEntity hits the bottom border. Then the game finishes.- Specified by:
collisionHasOccuredin interfaceEntityEventListener- Parameters:
e- EntityHitEvent
-
exitGame
Exits the game- Specified by:
exitGamein interfaceUserEventListener- Parameters:
e- a UserEvent
-
startPlaying
Method called as a result of a UserEvent - wants to start the game- Specified by:
startPlayingin interfaceUserEventListener- Parameters:
e- details of the View that the event came from
-
pauseGame
Needed to be a UserEventListener Method called as a result of a UserEvent - wants to pause the game- Specified by:
pauseGamein interfaceUserEventListener- Parameters:
e- details of the View that the event came from
-
saveCurrentGameResults
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:
saveCurrentGameResultsin interfaceUserEventListener- Parameters:
e- details of the View that the event came from but at the moment the data provide is not actually needed
-
changeFiringInterval
changeFiringInterval- Specified by:
changeFiringIntervalin interfaceUserEventListener- Parameters:
e- UserEvent
-
changeMoveSpeed
changeMoveSpeed- Specified by:
changeMoveSpeedin interfaceUserEventListener- Parameters:
e- UserEvent
-
undoLastSave
- Specified by:
undoLastSavein interfaceUserEventListener
-
getUsersHighScores
Gets the users high scores from the database then asks the View to display the high scores- Specified by:
getUsersHighScoresin interfaceUserEventListener- 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:
getAllHighScoresin interfaceUserEventListener
-
changeSettingForWantingIntroInfo
public void changeSettingForWantingIntroInfo(boolean wantIntroInfo) - Specified by:
changeSettingForWantingIntroInfoin interfaceUserEventListener
-
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
-