client.framework.models
Class AbstractGame

java.lang.Object
  extended by MIDlet
      extended by client.framework.models.AbstractGame
Direct Known Subclasses:
BrickBlock

public abstract class AbstractGame
extends MIDlet

This class is the main model for the framework. It contains the state information for the game, and is responsible for displaying the correct displayable element at all times. In addition, this class is responsible for communicating using a Communicator, object as well as handling the actions received from the Communicator. This class is the framework's implementation of MIDlet, and contains the startApp method, which is a J2ME application's main method. Finally, this class contains the two threads responsible for the running of a game in the framework.

Author:
Eivind Sorteberg, Martin Jarrett

Field Summary
protected  Communicator communicator
           
protected  GameView game
           
protected  LobbyView lobby
           
static boolean MOVEMENT_PREDICTION
           
protected  MessageParser parser
           
protected  AbstractPlayer self
           
static java.lang.String SEP_ACTION
           
static int[][] TEAMCOLORS
           
 
Constructor Summary
AbstractGame()
           
 
Method Summary
 void addPlayer(AbstractPlayer player)
          Adds a player to the player list.
 void addSetting(java.lang.String setting, java.lang.Object value)
          Adds a setting to the game.
protected abstract  GameView createGameView()
          Creates a game view of the implementing game's specified type, and returns this.
protected  void destroyApp(boolean unconditional)
          Called when the MIDlet is closed.
 void fireChangeSettingsSelected()
          Method called when the user selects "Change settings" in the game lobby.
 void fireHitTrap()
          Method called when the local player scores.
 void fireMessagesChanged(java.lang.String message, boolean append)
          Method fired when the list of messages is updated.
 void firePlayerListChanged()
          Called when the contents of the player list have changed, e.g. because of changed score or added or removed players.
 void firePlayerReady(AbstractPlayer player, java.lang.String value)
          Method called when one of the players has signalled that he is ready or not ready.
 void fireReadySelected()
          Method called when the user selects "Ready" in the game lobby.
 void fireReturnToGameSelected()
          Called when the player wishes to rejoin the running game after having visited the lobby view.
 void fireSelfMoved(int[] movement)
          Method called when the local player has moved on the game board.
 void fireSessionSelected(int selectedSession)
          Called when a player has selected to join a session.
 void fireSettingsUpdated(java.util.Hashtable settings)
          Method called after returning from the "Change Settings" screen.
 void flashAndVibrate()
          Method called to make the phone flash and vibrate.
abstract  java.lang.String getGameName()
          Returns the name of the game.
 int getNumberOfPlayers()
          Returns the number of players participating in the game.
 MessageParser getParser()
          Returns the model's MessageParser implementation.
protected  AbstractPlayer getPlayer(java.lang.Object id)
          Returns the player with the specified id.
 java.util.Hashtable getPlayerlist()
          Returns a list containing the players currently connected to the game.
 AbstractPlayer getSelf()
          Returns the local player representation.
 java.util.Vector getSessions()
          Returns a list of the found sessions.
 java.util.Hashtable getSettings()
          Returns the list of active settings, where each setting is of the form :.
 Displayable getView()
          Returns the currently displayed view.
protected abstract  boolean handleSpecialSettings(boolean init)
          Checks whether the state of the game is valid according to the game settings.
 boolean isTeamsEnabled()
          Checks whether the teams are enabled or not.
 void loadGame(java.lang.String[] values)
          Method called when one of the players have initiated the game.
abstract  AbstractPlayer newPlayer(java.lang.String address, java.lang.String name)
          Returns a new player object of the type used in the game, which is a subclass of AbstractPlayer.
 void notifyAboutMessageReceived(java.lang.String textMessage)
          Method that is called whenever the application receives a text message from one of the other participants.
protected abstract  void notifyAboutSpecialActionReceived(AbstractPlayer player, java.lang.String action, java.lang.String[][] values)
          Method used to parse and interpret messages received from other participants, that are not among the default actions defined in AbstractGame.
protected  void pauseApp()
          Called when the MIDlet enters its paused state, which can happen for example if the MIDlet is minimized.
 void quit()
          Called when closing the application.
 void refreshSessionList()
          Called when the player wishes to refresh the session list.
 void removePlayer(AbstractPlayer player)
          Removes one of the game participants from the list of players, and from the game board if a game is running
 void sendPlayerInfo()
          Sends the local player's data to the server.
 void setView(Displayable view)
          Changes the view currently displayed to the one specified.
 void showAlert(java.lang.String title, java.lang.String message, int duration_secs, Displayable nextScreen)
          Shows a message for a given amount of time, before changing to another screen.
protected  void startApp()
          Starts the MIDlet.
 void startFramework(java.lang.String name, java.lang.String[] addresses, int packetSize)
          Called after the player has specified a name.
 void startGame()
          Called when the player selects "start game" in the lobby menu.
 void stopGame(java.lang.String reason)
          Ends the game and returns to the lobby.
 void viewLobby()
          Method called to set the lobby view as the active view.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOVEMENT_PREDICTION

public static final boolean MOVEMENT_PREDICTION
See Also:
Constant Field Values

SEP_ACTION

public static final java.lang.String SEP_ACTION
See Also:
Constant Field Values

TEAMCOLORS

public static final int[][] TEAMCOLORS

parser

protected MessageParser parser

communicator

protected Communicator communicator

self

protected AbstractPlayer self

lobby

protected LobbyView lobby

game

protected GameView game
Constructor Detail

AbstractGame

public AbstractGame()
Method Detail

startFramework

public void startFramework(java.lang.String name,
                           java.lang.String[] addresses,
                           int packetSize)
Called after the player has specified a name. Initializes all lists, displays the lobby view and initialises the communication object.

Parameters:
name - The name of the local player.
addresses - The address(es) of the server(s) to connect to
packetSize - The maximum packet size (when using UDP)

createGameView

protected abstract GameView createGameView()
Creates a game view of the implementing game's specified type, and returns this.

Returns:
The game view that the game uses to draw its game objects.

getParser

public MessageParser getParser()
Returns the model's MessageParser implementation.

Returns:
An implementation of the MessageParser interface.

getPlayerlist

public java.util.Hashtable getPlayerlist()
Returns a list containing the players currently connected to the game.

Returns:
The players currently in the game.

getSettings

public java.util.Hashtable getSettings()
Returns the list of active settings, where each setting is of the form :.

Returns:
The settings for the game.

addSetting

public void addSetting(java.lang.String setting,
                       java.lang.Object value)
Adds a setting to the game. All settings must be of the form :, where the name is the name of the setting. The name is used as the setting's ID, so each setting must have a unique name. The value can be either a String, an Integer or a Boolean object.

Parameters:
setting - The name of the new setting.
value - The value of the new setting.

getSelf

public AbstractPlayer getSelf()
Returns the local player representation.

Returns:
The local player object.

addPlayer

public void addPlayer(AbstractPlayer player)
Adds a player to the player list.

Parameters:
player - The new player.

removePlayer

public void removePlayer(AbstractPlayer player)
Removes one of the game participants from the list of players, and from the game board if a game is running

Parameters:
player - The player to remove.

getNumberOfPlayers

public int getNumberOfPlayers()
Returns the number of players participating in the game.

Returns:
The size of the player vector.

showAlert

public void showAlert(java.lang.String title,
                      java.lang.String message,
                      int duration_secs,
                      Displayable nextScreen)
Shows a message for a given amount of time, before changing to another screen.

Parameters:
title - The title of the message to display.
message - The message to display.
duration_secs - How long the message is displayed.
nextScreen - The screen to display after the message has been shown.

setView

public void setView(Displayable view)
Changes the view currently displayed to the one specified. This method is used to toggle between game, lobby and list views.

Parameters:
view - The view to display.

getView

public Displayable getView()
Returns the currently displayed view.

Returns:
The currently displayed view.

fireMessagesChanged

public void fireMessagesChanged(java.lang.String message,
                                boolean append)
Method fired when the list of messages is updated. This list is mostly used for debugging information. Currently, the list is not in use, but it can be used to print messages to work around the problem of mobile phones not having a console.

Parameters:
message - The message to display.
append - Whether the message should append or overwrite previous messages

fireReadySelected

public void fireReadySelected()
Method called when the user selects "Ready" in the game lobby. Toggles ready status on or off, and notifies all other users in the network.


fireChangeSettingsSelected

public void fireChangeSettingsSelected()
Method called when the user selects "Change settings" in the game lobby. A screen for modifying the current settings is then displayed.

See Also:
SettingsWindow

fireSettingsUpdated

public void fireSettingsUpdated(java.util.Hashtable settings)
Method called after returning from the "Change Settings" screen. Checks whether any of the settings have been changed. If so, the new values from the settings are transmitted to all participants.

Parameters:
settings - The values of the settings.

viewLobby

public void viewLobby()
Method called to set the lobby view as the active view.


flashAndVibrate

public void flashAndVibrate()
Method called to make the phone flash and vibrate.


fireHitTrap

public void fireHitTrap()
Method called when the local player scores. The players score is updated, and all participants are notified about the event.


firePlayerReady

public void firePlayerReady(AbstractPlayer player,
                            java.lang.String value)
Method called when one of the players has signalled that he is ready or not ready. The player's status is then updated.

Parameters:
player - The player sending the signal.
value - "true" if the player is ready, "false" if not.

startGame

public void startGame()
Called when the player selects "start game" in the lobby menu. Controls whether all players have signalled they are ready to start the game. If so, a START command is issued to all the participants and the game view is displayed.


loadGame

public void loadGame(java.lang.String[] values)
Method called when one of the players have initiated the game. Changes the view to the GameView, and initiates the neccessary two threads.

Parameters:
values - The screen resolution for this particular game.
See Also:
@param value The game screen resolution

fireSelfMoved

public void fireSelfMoved(int[] movement)
Method called when the local player has moved on the game board.

Parameters:
movement - How much the local player has moved.

notifyAboutMessageReceived

public void notifyAboutMessageReceived(java.lang.String textMessage)
Method that is called whenever the application receives a text message from one of the other participants. The text message is specified in the form ":"[]"@"[]. The values may contain one or more attributes. How the values are used depends on the action.

Parameters:
textMessage - A string representing the message.
See Also:
Action

stopGame

public void stopGame(java.lang.String reason)
Ends the game and returns to the lobby. The local threads are stopped to free resources.

Parameters:
reason - The reason for ending the game.

firePlayerListChanged

public void firePlayerListChanged()
Called when the contents of the player list have changed, e.g. because of changed score or added or removed players. Notifies the lobby screen of this event.


getGameName

public abstract java.lang.String getGameName()
Returns the name of the game.

Returns:
The name of the game.

newPlayer

public abstract AbstractPlayer newPlayer(java.lang.String address,
                                         java.lang.String name)
Returns a new player object of the type used in the game, which is a subclass of AbstractPlayer. This method should create a new player object, for instance like: "return new BBPlayer(address, name, defaultSize, defaultSpeed);" The default size and speed of the Player object should be defined in the implementation of AbstractPlayer.

Parameters:
address - The player's network address.
name - The name of the player.
Returns:
An instance of an AbstractPlayer subclass.
See Also:
AbstractPlayer.AbstractPlayer(String, String, int, int)

handleSpecialSettings

protected abstract boolean handleSpecialSettings(boolean init)
Checks whether the state of the game is valid according to the game settings. This method is called contineously throughout the game, and should be implemented the same way as the handleSettings() method.

Parameters:
init - Whether the method is called on game initialisation or not
Returns:
True if all settings are valid, false if not
See Also:
handleSettings(boolean)

notifyAboutSpecialActionReceived

protected abstract void notifyAboutSpecialActionReceived(AbstractPlayer player,
                                                         java.lang.String action,
                                                         java.lang.String[][] values)
Method used to parse and interpret messages received from other participants, that are not among the default actions defined in AbstractGame. For a message, the sender (AbstractPlayer) is specified, and a three-letter word identifying the action leading to the message. The final parameter is a String containing one or more values corresponding to the action.

Parameters:
player - The sender of the message
action - The action specified in the message
values - The value(s) contained in the message

startApp

protected void startApp()
                 throws MIDletStateChangeException
Starts the MIDlet.

Throws:
MIDletStateChangeException

pauseApp

protected void pauseApp()
Called when the MIDlet enters its paused state, which can happen for example if the MIDlet is minimized.


destroyApp

protected void destroyApp(boolean unconditional)
                   throws MIDletStateChangeException
Called when the MIDlet is closed. Sends a disconnection notification to the server.

Parameters:
unconditional - If true when this method is called, the MIDlet must cleanup and release all resources.
Throws:
MIDletStateChangeException

getPlayer

protected AbstractPlayer getPlayer(java.lang.Object id)
Returns the player with the specified id. If none of the players in the player list has this id, the method assumes that the player must be the local player.

Parameters:
id - The id of the player.
Returns:
The player having the specified id, or the local player if no players have this id.

sendPlayerInfo

public void sendPlayerInfo()
Sends the local player's data to the server.

See Also:
AbstractPlayer.getAttributeValues()

quit

public void quit()
Called when closing the application.

See Also:
destroyApp(boolean)

getSessions

public java.util.Vector getSessions()
Returns a list of the found sessions.

Returns:
The session list.

fireSessionSelected

public void fireSessionSelected(int selectedSession)
Called when a player has selected to join a session. If the player is allowed to join the session (the session is not full), the server is notified.

Parameters:
selectedSession - The session the player wishes to join.

refreshSessionList

public void refreshSessionList()
Called when the player wishes to refresh the session list. Causes the client to reconnect to the server.


isTeamsEnabled

public boolean isTeamsEnabled()
Checks whether the teams are enabled or not.

Returns:
True if teams are enabled; false if not.

fireReturnToGameSelected

public void fireReturnToGameSelected()
Called when the player wishes to rejoin the running game after having visited the lobby view.