client.framework.views
Class GameView

java.lang.Object
  extended by GameCanvas
      extended by client.framework.views.GameView
Direct Known Subclasses:
BBBoard

public abstract class GameView
extends GameCanvas

The class responsible for running the game and displaying the game board. Contains lists of objects to draw to the game board, as well methods for detecting collisions and handling movement. The class is also responsible for detecting in-game user inputs and handling these.

Author:
Martin Jarrett, Eivind Sorteberg

Field Summary
protected  TiledLayer background
           
protected  int boardHeight
           
protected  int boardWidth
           
protected  LayerManager layerManager
           
protected  AbstractGame model
           
 
Constructor Summary
GameView(AbstractGame model)
          Constructor for the GameView class.
 
Method Summary
 void check()
          Method called continously throughout a game.
 void checkInput()
          Checks whether any keys have been pressed.
protected abstract  void clean()
          Abstract method that should contain functionality for releasing occupied resources and resetting the game view's state.
 void commandAction(Command command, Displayable displayable)
          Method inherited from the CommandListener interface.
protected abstract  Image createBackgroundImage()
          Abstract method that is used for defining the background of the game board.
abstract  java.lang.Object detectObjectCollision(AbstractPlayer player, int[] movement)
          This method is called continuously throughout the game in order to detect collisions with game objects other than players.
protected  AbstractPlayer detectPlayerCollision(AbstractPlayer mover, int[] movement)
          Detects whether the local player collides with any of the other players.
protected  int detectWallCollision(AbstractPlayer player, int[] movement)
          Checks whether the player collides with one of the four walls.
 void drawBoard(Graphics graphics)
          Draws the game board with its objects on the middle of the mobile phone's screen.
 void fireBoardChanged()
          Called whenever a change to the game board has been detected.
 int[] getSize()
          Returns the size of the game board.
abstract  void handleObjectCollision(java.lang.Object collidesWith, int[] movement)
          This method is called whenever a collision with a game specific object is detected, and is responsible for handling this event.
protected abstract  void handlePlayerCollision(AbstractPlayer collidesWith, int[] movement)
          This method is called whenever a collision with another player is detected, and is responsible for handling this event.
protected  void initPlayers()
          Initialises the players' sprites.
 boolean isRunning()
          Checks whether a game is currently running.
 void notifyAboutPlayerAdded(AbstractPlayer player)
          Adds the specified player to the game board.
 void removePlayer(AbstractPlayer player)
          Removes the specified player from the game board.
protected abstract  void specialCheck()
          Method called each time the check() method is called.
 void startGame(int[] screenResolution)
          Starts a new game with the specified size of the game board.
 void stopGame()
          Stops the current game, and sets the running parameter to false.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

model

protected AbstractGame model

layerManager

protected LayerManager layerManager

background

protected TiledLayer background

boardWidth

protected int boardWidth

boardHeight

protected int boardHeight
Constructor Detail

GameView

public GameView(AbstractGame model)
Constructor for the GameView class. Initialises the game objects, creates the game board and initialises the commands.

Parameters:
model - The underlying game model, which handles communication.
Method Detail

fireBoardChanged

public void fireBoardChanged()
Called whenever a change to the game board has been detected. The game board is updated, and all game objects are redrawn.


check

public void check()
Method called continously throughout a game. Checks whether the local player collides with a wall, another player or a game object. If a collision is detected, corresponding actions are carried out.


getSize

public int[] getSize()
Returns the size of the game board.

Returns:
The size of the game board.

checkInput

public void checkInput()
Checks whether any keys have been pressed. For handling key events other than directional keys, this method should be overridden. In the default implementation, the only key detected is the "FIRE" key. Pressing this key leads to the player list being drawn on top of the game board. This method is currently a little bugged, and leads to the player list flickering instead of being nicely displayed.

See Also:
check()

drawBoard

public void drawBoard(Graphics graphics)
Draws the game board with its objects on the middle of the mobile phone's screen.

Parameters:
graphics - The GameCanvas' Graphics object.

commandAction

public void commandAction(Command command,
                          Displayable displayable)
Method inherited from the CommandListener interface. This method is called whenever the user selects a command displayed at the bottom of the screen. In this implementation, the only command available is "Exit", which returns the player to the lobby.

Parameters:
command - The command causing the method to be called.
displayable - The currently displayed screen.

detectPlayerCollision

protected AbstractPlayer detectPlayerCollision(AbstractPlayer mover,
                                               int[] movement)
Detects whether the local player collides with any of the other players. If so, actions needs to be taken. The method returns the player with which the local player collides if any, or null if no collision is detected.

Parameters:
mover - The player trying to perform the movement.
movement - The movement to take place.
Returns:
The player with which a collision is detected, or null if no collision is detected.

initPlayers

protected void initPlayers()
Initialises the players' sprites. Makes use of each player's Sprite object, and places these on the game board using the layerManager object.

See Also:
AbstractPlayer.getSprite()

createBackgroundImage

protected abstract Image createBackgroundImage()
Abstract method that is used for defining the background of the game board. This method should return a single image that fills the entire screen.

Returns:
The background image for the game board.

specialCheck

protected abstract void specialCheck()
Method called each time the check() method is called. This method should contain the game specific checks that need to be performed as long as the game is running.

See Also:
check()

handlePlayerCollision

protected abstract void handlePlayerCollision(AbstractPlayer collidesWith,
                                              int[] movement)
This method is called whenever a collision with another player is detected, and is responsible for handling this event. The move has not yet been performed, but the movement parameter contains the move that will cause the collision if it is carried out.

Parameters:
collidesWith - The player with which a collision is detected.
movement - The move causing the collision
See Also:
detectPlayerCollision(client.framework.models.AbstractPlayer, int[])

handleObjectCollision

public abstract void handleObjectCollision(java.lang.Object collidesWith,
                                           int[] movement)
This method is called whenever a collision with a game specific object is detected, and is responsible for handling this event. The move has not yet been performed, but the movement parameter contains the move that would cause the collision if it is carried out.

Parameters:
collidesWith - The object with which a collision is detected.
movement - The move causing the collision.
See Also:
detectObjectCollision(client.framework.models.AbstractPlayer, int[])

detectObjectCollision

public abstract java.lang.Object detectObjectCollision(AbstractPlayer player,
                                                       int[] movement)
This method is called continuously throughout the game in order to detect collisions with game objects other than players. The method should run through all objects active in the game and check whether a collision is detected. If a collision is detected, the method should return the colliding object, if not it should return null.

Parameters:
player - The player trying to move
movement - The movement to take place.
Returns:
The object with which a collision is detected.

detectWallCollision

protected int detectWallCollision(AbstractPlayer player,
                                  int[] movement)
Checks whether the player collides with one of the four walls. If so, actions should be taken (usually denying the player the move).

Parameters:
player - The player trying to move
movement - The movement to take place
Returns:
A four digit binary representation of the collisions, in the order WENS.
See Also:
check()

removePlayer

public void removePlayer(AbstractPlayer player)
Removes the specified player from the game board. Is called whenever a player is disconnected from the session.

Parameters:
player - The player to remove from the game board.

notifyAboutPlayerAdded

public void notifyAboutPlayerAdded(AbstractPlayer player)
Adds the specified player to the game board. Called whenever a new player connects to the session.

Parameters:
player - The player to add to the game board.

startGame

public void startGame(int[] screenResolution)
Starts a new game with the specified size of the game board.

Parameters:
screenResolution - An array containing the height and width of the game board.

isRunning

public boolean isRunning()
Checks whether a game is currently running.

Returns:
True if a game is running, false if not.

stopGame

public void stopGame()
Stops the current game, and sets the running parameter to false. Calls the clean() method to release occupied resources.


clean

protected abstract void clean()
Abstract method that should contain functionality for releasing occupied resources and resetting the game view's state.