fang
Class GameLevel

java.lang.Object
  extended by fang.GameLevel

public abstract class GameLevel
extends Object

This class mimicks the GameLoop. Any game which extends GameLoop can be made to extend GameLevel instead and then can be incorporated into a multi-level game. See the examples on the FANG Engine website.

Author:
Jam Jenkins

Field Summary
protected  AnimationCanvas canvas
          the place where sprites are drawn.
protected  Random random
          the random number generator use this generator for all random number in order to maintain consistency in multiplayer games.
 
Constructor Summary
GameLevel()
           
 
Method Summary
abstract  void advanceFrame(double timePassed)
          This method is where the game logic will be applied.
 void cancelAlarm(Alarm alarm)
          removes all pending alarms on this object.
 void cancelAllAlarms()
          removes all pending alarms.
 void cleanUp()
          this method will be called by the FANG Engine just before terminating the level in case the level needs an opportunity to perform some final operations
 void clearPersistence()
          clears all peristent objects in this level from the collections of persistent objects
 void clearPersistence(Alarm alarm)
          removes the alarm from the collection of alarms which will persist after the level ends.
 void clearPersistence(Sound sound)
          removes the sound from the collection of sounds which will persist after the level ends.
 void clearPersistence(Sprite sprite)
          removes the sprite from the collection of sprites which will persist after the level ends.
protected  void finishGame()
          sets the game state to game over.
 void finishLevel()
          finishes this level such that advanceFrame will not be called again.
 Alarm[] getAlarms()
          gets the list of alarms scheduled to go off in the future.
 int getID()
          gets the index of the current player.
 int getNumberOfPlayers()
          gets the number of connected players in this game.
 Alarm[] getPersistentAlarms()
          gets all the alarms which are set to persist after the level ends.
 Sound[] getPersistentSounds()
          gets all the sounds which are set to persist after the level ends.
 Sprite[] getPersistentSprites()
          gets all the sprites which are set to persist after the level ends.
 Player getPlayer()
          gets the current player object.
protected  Player getPlayer(int playerIndex)
          gets the player object for a given player number.
protected  Player getPlayer(String name)
          gets the player object for a given player name.
 URL getResource(String filename)
          loads a resource relative to the package of the game.
 double getTime()
          gets the time in seconds since the game has started
 boolean imagesCached()
          gets whether to clear the cached images or not.
 void initializePersistantState(Alarm[] alarms, Sprite[] sprites, Sound[] sounds)
          copies all of the peristent alarms, sprites, and sounds into this level's persistence collections
 boolean isMuted()
          returns true if muted, false if not muted
 boolean isPaused()
          returns true if paused, false if the game is running
 boolean isPeristent(Alarm alarm)
          indicates whether this alarm is part of the collection of alarms which will persist after the level terminates
 boolean isPeristent(Sound sound)
          indicates whether this sound is part of the collection of sounds which will persist after the level terminates
 boolean isPeristent(Sprite sprite)
          indicates whether this sprite is part of the collection of sprites which will persist after the level terminates
 void pauseToggle()
          changes between paused and unpaused
 void persist(Alarm alarm)
          adds an alarm to the collection of alarms which will persist after the level ends.
 void persist(Sound sound)
          adds a sound to the collection of sounds which will persist after the level ends.
 void persist(Sprite sprite)
          adds a sprite to the collection of sprites which will persist after the level ends.
 void postAdvanceFrame()
           
protected  void removeLevelObjects()
          removes all the non-persistent sprites, alarms, and sounds
 void resetTime()
          makes the current time zero
 void scheduleAbsolute(Alarm alarm, double absolute)
          sets and alarm to go off at a time relative to the beginning of time (zero).
 void scheduleRelative(Alarm alarm, double relative)
          sets and alarm to go off relative to the current time.
 void sendMessage(Object localMessage)
          sends a message to all of the clients
 void setCacheImages(boolean cacheImages)
          sets whether to clear the cached images or not.
 void setCacheSounds(boolean cacheSounds)
          sets whether to clear the cached sounds or not.
 void setGameLoop(GameLoop loop)
          sets the GameLoop object this level will use to implement the majority of its methods.
 void setHelp(String help)
          sets the help screen.
 void setNextLevel(GameLevel level)
          sets the next level which will execute once this level has finished.
 boolean soundsCached()
          gets whether to clear the cached sounds or not.
abstract  void startLevel()
          starts the level.
 void startOver()
          starts the game over.
 void startOverLevel()
          begins this level again by the next advanceFrame
 void toggleAudible()
          Deprecated. this method is depricated and should not be used; use toggleSound instead
 void toggleSound()
          switches between mute and audible.
 String toString()
          gets the name of this class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

canvas

protected AnimationCanvas canvas
the place where sprites are drawn. This is not initialized until startLevel is called. You will get a NullPointerException if you try to use the canvas in the constructor.


random

protected Random random
the random number generator use this generator for all random number in order to maintain consistency in multiplayer games. This is not initialized until startLevel is called. You will get a NullPointerException if you try to use the canvas in the constructor.

Constructor Detail

GameLevel

public GameLevel()
Method Detail

setCacheImages

public void setCacheImages(boolean cacheImages)
sets whether to clear the cached images or not. By default, the cached is cleared at the end of each level in order to avoid using too much memory. Keeping the images in the cache can help the next level load more quickly, but too many accumulated images will cause the game to run out of memory.

Parameters:
cacheImages - true indicates the images currently in the cache should remain in the cache, false means to clear them.

setCacheSounds

public void setCacheSounds(boolean cacheSounds)
sets whether to clear the cached sounds or not. By default, the cached is cleared at the end of each level in order to avoid using too much memory. Keeping the sounds in the cache can help the next level load more quickly, but too many accumulated sounds will cause the game to run out of memory.

Parameters:
cacheSounds - true indicates the sounds currently in the cache should remain in the cache, false means to clear them.

imagesCached

public boolean imagesCached()
gets whether to clear the cached images or not. By default, the cached is cleared at the end of each level in order to avoid using too much memory. Keeping the images in the cache can help the next level load more quickly, but too many accumulated images will cause the game to run out of memory.

Returns:
true if the images in cache will remain in cache for the next level, false means they will be cleared.

soundsCached

public boolean soundsCached()
gets whether to clear the cached sounds or not. By default, the cached is cleared at the end of each level in order to avoid using too much memory. Keeping the sounds in the cache can help the next level load more quickly, but too many accumulated sounds will cause the game to run out of memory.

Returns:
true if the sounds in cache will remain in cache for the next level, false means they will be cleared.

initializePersistantState

public void initializePersistantState(Alarm[] alarms,
                                      Sprite[] sprites,
                                      Sound[] sounds)
copies all of the peristent alarms, sprites, and sounds into this level's persistence collections

Parameters:
alarms - the alarms which came from the last level
sprites - the sprites which came from the last level
sounds - the sounds which came from the last lelel

setGameLoop

public void setGameLoop(GameLoop loop)
sets the GameLoop object this level will use to implement the majority of its methods.

Parameters:
loop - the GameLoop object of the main game

toggleAudible

@Deprecated
public void toggleAudible()
Deprecated. this method is depricated and should not be used; use toggleSound instead

switches between mute and audible


toggleSound

public void toggleSound()
switches between mute and audible. Forwards all real work the GameLoop.toggleSound


isMuted

public boolean isMuted()
returns true if muted, false if not muted

Returns:
WhetherMuted

sendMessage

public void sendMessage(Object localMessage)
sends a message to all of the clients

Parameters:
localMessage - a Serializable message which is sent to all clients

isPaused

public boolean isPaused()
returns true if paused, false if the game is running

Returns:
the state of the game running/paused

pauseToggle

public void pauseToggle()
changes between paused and unpaused


getResource

public URL getResource(String filename)
loads a resource relative to the package of the game. Be careful to make sure your resource name matches the case of the actual resource. Some file systems are not case sensitive which will cause the resource to load properly when developing the game, but will not load properly when deployed in a jar file.

Parameters:
filename - the name of the file
Returns:
the resource as a URL

startLevel

public abstract void startLevel()
starts the level. At this point the number of players is now known. This method should create and add sprites to the canvas. This method is called for you by the FANG Engine and should not be called directly.


advanceFrame

public abstract void advanceFrame(double timePassed)
This method is where the game logic will be applied. In general the game will respond to user input and other events such as sprite collisions. This method is called for you by the FANG Engine and should not be called directly.

Parameters:
timePassed - the duration of time which has passed since the last frame was calculated or displayed.

postAdvanceFrame

public void postAdvanceFrame()

cleanUp

public void cleanUp()
this method will be called by the FANG Engine just before terminating the level in case the level needs an opportunity to perform some final operations


finishGame

protected final void finishGame()
sets the game state to game over. This not only ends the level, but also ends the game.


removeLevelObjects

protected void removeLevelObjects()
removes all the non-persistent sprites, alarms, and sounds


setNextLevel

public void setNextLevel(GameLevel level)
sets the next level which will execute once this level has finished.

Parameters:
level - the next level

finishLevel

public void finishLevel()
finishes this level such that advanceFrame will not be called again.


toString

public String toString()
gets the name of this class

Overrides:
toString in class Object
Returns:
the name of the class

startOverLevel

public void startOverLevel()
begins this level again by the next advanceFrame


persist

public void persist(Alarm alarm)
adds an alarm to the collection of alarms which will persist after the level ends.

Parameters:
alarm - the alarm to keep after the level terminates

persist

public void persist(Sprite sprite)
adds a sprite to the collection of sprites which will persist after the level ends.

Parameters:
sprite - the sprite to keep after the level terminates

persist

public void persist(Sound sound)
adds a sound to the collection of sounds which will persist after the level ends.

Parameters:
sound - the sound to keep after the level terminates

clearPersistence

public void clearPersistence(Alarm alarm)
removes the alarm from the collection of alarms which will persist after the level ends.

Parameters:
alarm - the alarm to remove from the persistent collection

clearPersistence

public void clearPersistence(Sprite sprite)
removes the sprite from the collection of sprites which will persist after the level ends.

Parameters:
sprite - the sprite to remove from the persistent collection

clearPersistence

public void clearPersistence(Sound sound)
removes the sound from the collection of sounds which will persist after the level ends.

Parameters:
sound - the sound to remove from the persistent collection

clearPersistence

public void clearPersistence()
clears all peristent objects in this level from the collections of persistent objects


getPersistentAlarms

public Alarm[] getPersistentAlarms()
gets all the alarms which are set to persist after the level ends.

Returns:
the array of alarms

getPersistentSprites

public Sprite[] getPersistentSprites()
gets all the sprites which are set to persist after the level ends.

Returns:
the array of sprites

getPersistentSounds

public Sound[] getPersistentSounds()
gets all the sounds which are set to persist after the level ends.

Returns:
the array of sounds

getNumberOfPlayers

public int getNumberOfPlayers()
gets the number of connected players in this game. This method may only be called once the gaming engine has called the startGame method.

Returns:
the number of players who originally connected to the game

getID

public int getID()
gets the index of the current player. This index is the one that corresponds to the playerMouse, keyboard and messages arrays. This method should not be called before the gaming engine calls the startGame method.

Returns:
the index of the player

getPlayer

protected final Player getPlayer(int playerIndex)
gets the player object for a given player number.

Parameters:
playerIndex - the player number. See getNumberOfPlayers to determine how many players are connected to this game.
Returns:
the Player object which contains that user's keyboard, mouse, and any messages sent

getPlayer

protected final Player getPlayer(String name)
gets the player object for a given player name.

Parameters:
name - String name associated with the Player
Returns:
the Player object which contains that user's keyboard, mouse, and any messages sent

getPlayer

public Player getPlayer()
gets the current player object. This method is equivalent to calling the getPlayer method with getID as the player number.

Returns:
the Player object which contains that user's keyboard, mouse, and any messages sent

scheduleRelative

public void scheduleRelative(Alarm alarm,
                             double relative)
sets and alarm to go off relative to the current time. For example, this method can be used to set off an alarm in 5 seconds from the current time.

Parameters:
alarm - the class to call the alarm method on
relative - the time from now in seconds to call the alarm method

scheduleAbsolute

public void scheduleAbsolute(Alarm alarm,
                             double absolute)
sets and alarm to go off at a time relative to the beginning of time (zero). For example, this method can be used to set off an alarm 30 seconds from the beginning time.

Parameters:
alarm - the class to call the alarm method on
absolute - the time in seconds to call the alarm method

cancelAlarm

public void cancelAlarm(Alarm alarm)
removes all pending alarms on this object. If there are no alarms with this object as the target, the method call is ignored.

Parameters:
alarm - the object that is the target of a pending alarm

cancelAllAlarms

public void cancelAllAlarms()
removes all pending alarms. If there are no pending alarms, the method call is ignored.


getAlarms

public Alarm[] getAlarms()
gets the list of alarms scheduled to go off in the future.

Returns:
the array of alarms in the order which they would go off

isPeristent

public boolean isPeristent(Alarm alarm)
indicates whether this alarm is part of the collection of alarms which will persist after the level terminates

Parameters:
alarm - the alarm to check
Returns:
true indicates the alarm will persist, false means it will be removed upon termination of the level

isPeristent

public boolean isPeristent(Sprite sprite)
indicates whether this sprite is part of the collection of sprites which will persist after the level terminates

Parameters:
sprite - the sprite to check
Returns:
true indicates the sprite will persist, false means it will be removed upon termination of the level

isPeristent

public boolean isPeristent(Sound sound)
indicates whether this sound is part of the collection of sounds which will persist after the level terminates

Parameters:
sound - the alarm to check
Returns:
true indicates the sound will persist, false means it will be removed upon termination of the level

startOver

public void startOver()
starts the game over. This consists of removing all the sprites, sounds, and alarms, then calling the startGame method. All of this will occur after the next termination of the advanceFrame method. Therefore, the game should be able to continue without error for at least one more frame before the startGame method is called.


setHelp

public void setHelp(String help)
sets the help screen. If no help screen is set, the default help screen asks the user to email the person who made the game and ask them to make a help screen. To avoid receiving emails asking for a help screen, provide one from the start. The html help screen does not offer full html support, it just displays the formatted text between the body tags. Therefore you should avoid using advanced formatting, java script and other advanced html features as they may not display properly.

Parameters:
help - the path to the html help screen which will be displayed

getTime

public double getTime()
gets the time in seconds since the game has started

Returns:
the time in seconds

resetTime

public void resetTime()
makes the current time zero