server.brickblock.fwimpl
Class BBEventHandler

java.lang.Object
  extended by server.brickblock.fwimpl.BBEventHandler
All Implemented Interfaces:
java.lang.Runnable, EventHandler<BBPlayer,BBSession>

public class BBEventHandler
extends java.lang.Object
implements EventHandler<BBPlayer,BBSession>

Thread that is responsible for generating traps and power up objects, as well as player positions on game initiation. In addition, this class is responsible for handling game events.

Author:
Martin Jarrett, Eivind Sorteberg

Field Summary
static int POWERUP_DURATION_SECS
           
static int POWERUP_INTERVAL_SECS
           
 
Constructor Summary
BBEventHandler(BBSession session)
          Creates the BBEventHandler object.
 
Method Summary
 void checkSettings()
          Checks if the game has timed out, or if the number of players connected to the session is too large.
 void fireEventOccured(AbstractPlayer player)
          Not implemented.
 void fireEventOccured(BBPlayer player, Event event, java.lang.Object[] values)
          Called when a game related event has occurred.
 void firePowerupAdded(Powerup powerup)
          Called when a power up object has been generated.
 void firePowerupRemoved(Powerup powerup)
          Called when a power up object has been removed.
 void generatePositions(java.util.List<BBPlayer> players)
          Generates random positions for the players given in the list.
static int[] generateRandomPosition(int screenWidth, int screenHeight)
          Generates a random position within the specified bounds.
 boolean isReady()
          Checks whether the thread has completed its pre-game calculations and is ready to start a game.
static boolean positionIsOccupied(int[] position, java.util.List<BBPlayer> players, int playerToCheck)
          Checks if a given position is occupied by any players.
 void run()
          The loop where power up objects are created, and the players' power up objects are controlled.
 void setSession(BBSession session)
          Sets this thread's session.
 void stop()
          Stops the thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POWERUP_INTERVAL_SECS

public static final int POWERUP_INTERVAL_SECS
See Also:
Constant Field Values

POWERUP_DURATION_SECS

public static final int POWERUP_DURATION_SECS
See Also:
Constant Field Values
Constructor Detail

BBEventHandler

public BBEventHandler(BBSession session)
Creates the BBEventHandler object. Initialises the power up list and generates the trap position.

Parameters:
session - This thread's session.
Method Detail

run

public void run()
The loop where power up objects are created, and the players' power up objects are controlled. This loop is started when a game is initiated, and stopped when the game is over.

Specified by:
run in interface java.lang.Runnable

checkSettings

public void checkSettings()
Checks if the game has timed out, or if the number of players connected to the session is too large. If so, the game is stopped.

Specified by:
checkSettings in interface EventHandler<BBPlayer,BBSession>

fireEventOccured

public void fireEventOccured(AbstractPlayer player)
Not implemented.

Parameters:
player - Not used.

firePowerupAdded

public void firePowerupAdded(Powerup powerup)
Called when a power up object has been generated. Sends a notification to the connected clients.

Parameters:
powerup - The new power up object.

firePowerupRemoved

public void firePowerupRemoved(Powerup powerup)
Called when a power up object has been removed. Sends a notification to the connected clients.

Parameters:
powerup - The power up that is to be removed.

setSession

public void setSession(BBSession session)
Sets this thread's session.

Specified by:
setSession in interface EventHandler<BBPlayer,BBSession>
Parameters:
session - This thread's session.

stop

public void stop()
Stops the thread. Called when the game is over.

Specified by:
stop in interface EventHandler<BBPlayer,BBSession>

fireEventOccured

public void fireEventOccured(BBPlayer player,
                             Event event,
                             java.lang.Object[] values)
Called when a game related event has occurred. The action that is taken depends on the event that has occurred.

Specified by:
fireEventOccured in interface EventHandler<BBPlayer,BBSession>
Parameters:
player - The player that caused the event.
event - The event.
values - Values related to the event.

generatePositions

public void generatePositions(java.util.List<BBPlayer> players)
Generates random positions for the players given in the list. Called when a new game is started, and the players are to be placed on the game board.

Parameters:
players - The list of players.

positionIsOccupied

public static boolean positionIsOccupied(int[] position,
                                         java.util.List<BBPlayer> players,
                                         int playerToCheck)
Checks if a given position is occupied by any players. Loops through the first players in the list, but not all, since positions will be generated for one and one player further down the list.

Parameters:
position - The position to check.
players - The list of relevant players.
playerToCheck - The last position in the list to check.
Returns:
True if the position is available, false if not.

generateRandomPosition

public static int[] generateRandomPosition(int screenWidth,
                                           int screenHeight)
Generates a random position within the specified bounds.

Parameters:
screenWidth - The maximum x-position.
screenHeight - The maximum y-position.
Returns:
The random position.

isReady

public boolean isReady()
Checks whether the thread has completed its pre-game calculations and is ready to start a game.

Specified by:
isReady in interface EventHandler<BBPlayer,BBSession>
Returns:
True if the thread is ready, false if not.