vooga.gameEngine.game
Class AbstractGame

java.lang.Object
  extended by vooga.gameEngine.game.GameRelatedObject
      extended by vooga.gameEngine.game.AbstractGame
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, PauseListener
Direct Known Subclasses:
AIDemo, BallPathBuilder, CameraTest, DummyGame, Pinball, Pong, RobotUnicornAttack, SpaceInvaders, StickFighterGame, WormsDemo

public abstract class AbstractGame
extends GameRelatedObject
implements PauseListener

AbstractGame is main class implemented by all games. It provides methods required for general game-running, such as starting and ending the whole game or the current level. AbstractGame objects hold a reference to a GameManager object. This manager has several methods useful in executing a game, such as adding or removing an Object from the game. If, during game play, the game necessitates the addition or removal of a particular game Object, the suggested solution is to do so by firing an event. The AbstractGame should listen for this event and then call the proper method in the GameManager.

Author:
Scott Brothers, Geoffrey Lawler
See Also:
Serialized Form

Constructor Summary
AbstractGame(GameManager manager)
          Creates a game given its GameManager.
 
Method Summary
abstract  boolean endGame()
          Ends the current game.
abstract  boolean endLevel()
          Ends the current level.
abstract  java.lang.Integer[] getAllowedPlayerNumbers()
          Specifies what number of players are allowed in a network game.
abstract  CameraController getCameraController()
          Provides access to the CameraController for this game.
 GameManager getManager()
          Provides access to the manager for this AbstractGame.
 boolean loadLevel(java.util.Collection<? extends java.lang.Object> items)
          Loads all the Objects of the level into memory and readies the level.
 boolean loadLevel(java.io.File level)
          Parses an XML file of all the Objects in a level, loads the items into memory, and readies the level.
 void pauseStatusChanged(PauseEvent e)
          Pauses or resumes all aspects of the game.
abstract  boolean quitGame()
          Quits the current game.
 void removeAllKeyInputs()
          Removes all assignments made by setKeyInput(KeyStroke, Class) or setKeyInput(KeyStroke, Class, EventListener).
 void removeAllKeyInputs(java.util.Collection<javax.swing.KeyStroke> keyStrokes)
          Removes the assignments made by setKeyInput(keyStroke, Class) or setKeyInput(keyStroke, Class, EventListener, where keyStroke represents an entry of keyStrokes.
 void removeKeyInput(javax.swing.KeyStroke keyStroke)
          Removes the assignment made by setKeyInput(keyStroke, Class) or setKeyInput(keyStroke, Class, EventListener.
 boolean restartLevel()
          Restarts the current level.
 void setKeyInput(javax.swing.KeyStroke keyStroke, java.lang.Class<? extends java.util.EventObject> event)
          Assigns a particular KeyStroke to an event in the game.
 void setKeyInput(javax.swing.KeyStroke keyStroke, java.lang.Class<? extends java.util.EventObject> event, java.util.EventListener listener)
          Assigns a particular KeyStroke to an event in the game and to a specific listener.
 void setMouseInput(java.lang.Integer mouseButtonID, java.lang.Class<? extends java.util.EventObject> event)
          Assigns a particular MouseEvent to an event in the game.
abstract  boolean startGame()
          Initializes anything necessary for the game to start, and starts the game.
abstract  boolean startLevel()
          Initializes anything necessary for a level to start, and starts the level.
abstract  void whenPaused(boolean gameLoopPaused)
          Determines what should occur game-wise during an attempt to pause a game.
abstract  void whenResumed()
          Defines what should occur game-wise during an attempt to resume the game.
 
Methods inherited from class vooga.gameEngine.game.GameRelatedObject
addEventListener, clearListeners, equals, fire, fire, fire, getEventListeners, getID, getListenerCount, removeEventListener
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractGame

public AbstractGame(GameManager manager)
Creates a game given its GameManager. If manager is set to null, an exception will be thrown, as all games must have a GameManager.

Parameters:
manager - the GameManager managing this game
Method Detail

startGame

public abstract boolean startGame()
Initializes anything necessary for the game to start, and starts the game. This method will be invoked by the game engine. Thus, an instance of an AbstractGame need not call this method.

Returns:
true if the game successfully started; false otherwise

endGame

public abstract boolean endGame()
Ends the current game. This method should be called by the instance of an AbstractGame when it is determined that the game is over.

Returns:
true if the game successfully ended; false otherwise

pauseStatusChanged

public void pauseStatusChanged(PauseEvent e)
Pauses or resumes all aspects of the game. Tells the manager to pause or resume the game loop and any engine components (such as listening and responding to user input) and then pauses any game-specific parts by invoking whenPaused(boolean) or whenResumed().

Specified by:
pauseStatusChanged in interface PauseListener
Parameters:
e - the PauseEvent

whenPaused

public abstract void whenPaused(boolean gameLoopPaused)
Determines what should occur game-wise during an attempt to pause a game. Note that this method will be called by pauseStatusChanged(PauseEvent).

Parameters:
gameLoopPaused - whether the game loop was successfully paused

whenResumed

public abstract void whenResumed()
Defines what should occur game-wise during an attempt to resume the game. Note that this method will be called by pauseStatusChanged(PauseEvent).


quitGame

public abstract boolean quitGame()
Quits the current game. Differs from endGame() in that this method would be called if the user attempts to quit in the middle of a game. This method should be called by the instance of an AbstractGame when it is determined that a quit is desired.

Returns:
true if quit was successful; false otherwise

loadLevel

public boolean loadLevel(java.util.Collection<? extends java.lang.Object> items)
Loads all the Objects of the level into memory and readies the level. Note that this method WILL add these Objects through the GameManager to the collection of items for the current level.

Parameters:
items - the Collection of Objects that are needed for a particular level
Returns:
true if the level was loaded successfully; false otherwise

loadLevel

public boolean loadLevel(java.io.File level)
Parses an XML file of all the Objects in a level, loads the items into memory, and readies the level. Note that this method WILL add these Objects to the level.

Parameters:
level - the XML file containing all the Objects for a particular level.
Returns:
true if the level was loaded successfully; false otherwise

startLevel

public abstract boolean startLevel()
Initializes anything necessary for a level to start, and starts the level. Note that this method is called by loadLevel(java.util.Collection), so all GameItemss have already been loaded. Note: There is a StartLevelListener-StartLevelEvent pair provided for convenience. If used, the startLevelRequested method of the StartLevelListener should contain a call to this method. Moreover, if one has a collection of items that define a particular level or a file that contains such items, this method can call loadLevel(Collection) or loadLevel(File) rather than adding them to the manager directly.

Returns:
true if the level was started successfully; false otherwise

endLevel

public abstract boolean endLevel()
Ends the current level. This method should be called by the instance of an AbstractGame when it is determined that the current level is over. Note: There is a EndLevelListener-EndLevelEvent pair provided for convenience. If used, the endLevelRequested method of the EndLevelListener should contain a call to this method.

Returns:
true if the level was ended successfully; false otherwise

restartLevel

public boolean restartLevel()
Restarts the current level. Note: NEVER FULLY IMPLEMENTED. Currently returns false to signify that a restart by invoking this method is not possible.

Returns:
true if the level was restarted successfully; false otherwise

setKeyInput

public void setKeyInput(javax.swing.KeyStroke keyStroke,
                        java.lang.Class<? extends java.util.EventObject> event)
Assigns a particular KeyStroke to an event in the game. When the KeyStroke is made, the event is fired to all listeners for this event.

Parameters:
keyStroke - a particular key stroke
event - The Class of the event to be fired if the given key stroke is pressed (or released).

setKeyInput

public void setKeyInput(javax.swing.KeyStroke keyStroke,
                        java.lang.Class<? extends java.util.EventObject> event,
                        java.util.EventListener listener)
Assigns a particular KeyStroke to an event in the game and to a specific listener. When the specific KeyStroke is made, the event will be fired ONLY to the given listener.

Parameters:
keyStroke - a particular key stroke
event - The Class of the event to be fired if the given key stroke is pressed (or released).
listener - The listener to whom the event should be fired

setMouseInput

public void setMouseInput(java.lang.Integer mouseButtonID,
                          java.lang.Class<? extends java.util.EventObject> event)
Assigns a particular MouseEvent to an event in the game.

Parameters:
mouseButton - a MouseEvent
event - The Class of the event to be fired if the given MouseEvent is fired.

removeKeyInput

public void removeKeyInput(javax.swing.KeyStroke keyStroke)
Removes the assignment made by setKeyInput(keyStroke, Class) or setKeyInput(keyStroke, Class, EventListener.

Parameters:
keyStroke - to remove

removeAllKeyInputs

public void removeAllKeyInputs(java.util.Collection<javax.swing.KeyStroke> keyStrokes)
Removes the assignments made by setKeyInput(keyStroke, Class) or setKeyInput(keyStroke, Class, EventListener, where keyStroke represents an entry of keyStrokes.

Parameters:
keyStrokes - to remove

removeAllKeyInputs

public void removeAllKeyInputs()
Removes all assignments made by setKeyInput(KeyStroke, Class) or setKeyInput(KeyStroke, Class, EventListener).


getCameraController

public abstract CameraController getCameraController()
Provides access to the CameraController for this game. This method will be called by the GameManager in order to set the CameraController.

Returns:
the CameraController

getManager

public GameManager getManager()
Provides access to the manager for this AbstractGame. Changed to public for the development team.

Returns:
the GameManager for this game

getAllowedPlayerNumbers

public abstract java.lang.Integer[] getAllowedPlayerNumbers()
Specifies what number of players are allowed in a network game. This method returns an Array of Integers specifying which numbers of players are allowed during network play. Thus a game which supports 2 and 4 players but not 3 should return an array {2,4}. It is unlikely that a network game would ever allow a single player and thus should not return {1,...}. Games which are not networked need not implement this method.

Returns:
An array specifying all allowed numbers of players for a networked instance of a game.