vooga.gameEngine.controller
Class NetworkController

java.lang.Object
  extended by vooga.gameEngine.game.GameRelatedObject
      extended by vooga.gameEngine.controller.GameRelatedController
          extended by vooga.gameEngine.controller.NetworkController
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, NetworkListener, INetworkObjectReceiver

public class NetworkController
extends GameRelatedController
implements NetworkListener, INetworkObjectReceiver

NetworkController is a subclass of GameRelatedController and is used to communicate NetworkEvents to GameRelatedObjects both locally and remotely that are interested in those events.

NetworkController will not be enabled in games which do not support networking, but may still exist. This should be taken into account when writing game code and methods such as isANetworkGame() may be helpful.

NetworkController also provides a mechanism for mapping players to GameRelatedObjects with the #mapPlayerToGameObject(int, Collection) method.

Author:
Geoffrey Lawler
See Also:
Serialized Form

Constructor Summary
NetworkController()
          Default constructor for a NetworkController
 
Method Summary
 void disconnect()
           
 java.util.Collection<java.util.EventListener> getLocalPlayerObjects()
          Getter method for the Collection mapped to the local player's player number as given by the getPlayerNumber() method.
 int getPlayerNumber()
          Getter method for this instance's player number.
 java.util.List<java.util.EventObject> getUserInput()
          Simple getter method to obtain the events fired by networked user input.
 boolean isANetworkGame()
           
 void mapBroadCastListener(java.util.EventListener listener)
          Maps EventListeners to a broadcast ID which is consistent across multiple networked machines and thus can be used to have any player send a BroadcastEvent to a specific EventListener
 void mapPlayerToGameObject(int playerNumber, java.util.EventListener listener)
          Maps player numbers to EventListeners so that events fired from a particular player's machine can update the state of any EventListeners that are mapped to that player number.
 void networkEventReceived(NetworkEvent e)
          Responds to a received NetworkEvent either by sending that EventObject across the network or by performing an action locally.
 void receiveObject(java.lang.Object object)
          Performs the appropriate action when an Object has been received from the server.
 void setUp(int playerNumber, HumanController inputControl, VoogaGameClient localClient)
          Called by network instances of games to setup the framework for networked multiplayer games.
 
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

NetworkController

public NetworkController()
Default constructor for a NetworkController

Method Detail

setUp

public void setUp(int playerNumber,
                  HumanController inputControl,
                  VoogaGameClient localClient)
Called by network instances of games to setup the framework for networked multiplayer games. If this method is not called NetworkEvent s will not be processed.

Parameters:
playerNumber - The unique player number for the local instance of the game
inputControl - Used to listen for NetworkEvents generated by user input

getPlayerNumber

public int getPlayerNumber()
Getter method for this instance's player number.

Returns:
The player number for the local machine.

isANetworkGame

public boolean isANetworkGame()
Returns:
true if the current game is networked, false otherwise

mapPlayerToGameObject

public void mapPlayerToGameObject(int playerNumber,
                                  java.util.EventListener listener)
Maps player numbers to EventListeners so that events fired from a particular player's machine can update the state of any EventListeners that are mapped to that player number.

Parameters:
playerNumber - The player number to be mapped to a given EventListener
listener - The EventListener which will receive events from the player with corresponding player number

mapBroadCastListener

public void mapBroadCastListener(java.util.EventListener listener)
Maps EventListeners to a broadcast ID which is consistent across multiple networked machines and thus can be used to have any player send a BroadcastEvent to a specific EventListener

Parameters:
listener - The EventListener which will receive broadcast events.

networkEventReceived

public void networkEventReceived(NetworkEvent e)
Description copied from interface: NetworkListener
Responds to a received NetworkEvent either by sending that EventObject across the network or by performing an action locally.

Specified by:
networkEventReceived in interface NetworkListener
Parameters:
e - The NetworkEvent received

receiveObject

public void receiveObject(java.lang.Object object)
Description copied from interface: INetworkObjectReceiver
Performs the appropriate action when an Object has been received from the server.

Specified by:
receiveObject in interface INetworkObjectReceiver

getUserInput

public java.util.List<java.util.EventObject> getUserInput()
Simple getter method to obtain the events fired by networked user input.

Returns:
the List of fired events

getLocalPlayerObjects

public java.util.Collection<java.util.EventListener> getLocalPlayerObjects()
Getter method for the Collection mapped to the local player's player number as given by the getPlayerNumber() method.

Returns:
the GameRelatedObject representing the local player which also must be an EventListener

disconnect

public void disconnect()