Delegation! Since it's clear that extracting services into delegation
classes is only going to become more common, I've gone and created a comprehensive facility for creating and using delegates in the place controller and manager as well as the game controller and manager. With the pattern nicely set, it is also easy to extend to controller/managers further up the hierarchy that might need to delegate special methods of their own. Whee! git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@994 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
//
|
||||
// $Id: TurnGameController.java,v 1.3 2002/02/12 06:57:30 mdb Exp $
|
||||
// $Id: TurnGameController.java,v 1.4 2002/02/13 03:21:28 mdb Exp $
|
||||
|
||||
package com.threerings.parlor.turn;
|
||||
|
||||
import com.threerings.parlor.game.GameController;
|
||||
|
||||
/**
|
||||
* Games that wish to make use of the turn game services should have their
|
||||
* controller implement this interface and create an instance of {@link
|
||||
* TurnGameControllerDelegate}, calling out to it at the appropriate
|
||||
* times.
|
||||
* TurnGameControllerDelegate} which should be passed to {@link
|
||||
* GameController#addDelegate}.
|
||||
*/
|
||||
public interface TurnGameController
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: TurnGameControllerDelegate.java,v 1.1 2002/02/12 06:57:30 mdb Exp $
|
||||
// $Id: TurnGameControllerDelegate.java,v 1.2 2002/02/13 03:21:28 mdb Exp $
|
||||
|
||||
package com.threerings.parlor.turn;
|
||||
|
||||
@@ -7,9 +7,12 @@ import com.threerings.presents.dobj.AttributeChangedEvent;
|
||||
import com.threerings.presents.dobj.AttributeChangeListener;
|
||||
|
||||
import com.threerings.crowd.data.BodyObject;
|
||||
import com.threerings.crowd.data.PlaceConfig;
|
||||
import com.threerings.crowd.data.PlaceObject;
|
||||
import com.threerings.crowd.util.CrowdContext;
|
||||
|
||||
import com.threerings.parlor.game.GameController;
|
||||
import com.threerings.parlor.game.GameControllerDelegate;
|
||||
import com.threerings.parlor.game.GameObject;
|
||||
|
||||
/**
|
||||
@@ -20,7 +23,7 @@ import com.threerings.parlor.game.GameObject;
|
||||
* implement the {@link TurnGameController} interface so that it can be
|
||||
* notified when turn-based game events take place.
|
||||
*/
|
||||
public class TurnGameControllerDelegate
|
||||
public class TurnGameControllerDelegate extends GameControllerDelegate
|
||||
implements AttributeChangeListener
|
||||
{
|
||||
/**
|
||||
@@ -30,15 +33,10 @@ public class TurnGameControllerDelegate
|
||||
*/
|
||||
public TurnGameControllerDelegate (TurnGameController tgctrl)
|
||||
{
|
||||
_tgctrl = tgctrl;
|
||||
}
|
||||
super((GameController)tgctrl);
|
||||
|
||||
/**
|
||||
* This must be called from {@link GameController#init}.
|
||||
*/
|
||||
public void init (CrowdContext ctx)
|
||||
{
|
||||
_ctx = ctx;
|
||||
// keep this around for later
|
||||
_tgctrl = tgctrl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,27 +50,29 @@ public class TurnGameControllerDelegate
|
||||
_turnGame.getTurnHolder().equals(self.username));
|
||||
}
|
||||
|
||||
/**
|
||||
* This must be called from {@link GameController#willEnterPlace}.
|
||||
*/
|
||||
public void willEnterPlace (GameObject gobj)
|
||||
// documentation inherited
|
||||
public void init (CrowdContext ctx, PlaceConfig config)
|
||||
{
|
||||
_ctx = ctx;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void willEnterPlace (PlaceObject plobj)
|
||||
{
|
||||
// get a casted reference to the object
|
||||
_gameObj = gobj;
|
||||
_turnGame = (TurnGameObject)gobj;
|
||||
_gameObj = (GameObject)plobj;
|
||||
_turnGame = (TurnGameObject)plobj;
|
||||
_thfield = _turnGame.getTurnHolderFieldName();
|
||||
|
||||
// and add ourselves as a listener
|
||||
gobj.addListener(this);
|
||||
plobj.addListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* This must be called from {@link GameController#didLeavePlace}.
|
||||
*/
|
||||
public void didLeavePlace (GameObject gobj)
|
||||
// documentation inherited
|
||||
public void didLeavePlace (PlaceObject plobj)
|
||||
{
|
||||
// remove our listenership
|
||||
gobj.removeListener(this);
|
||||
plobj.removeListener(this);
|
||||
|
||||
// clean up
|
||||
_turnGame = null;
|
||||
@@ -87,12 +87,12 @@ public class TurnGameControllerDelegate
|
||||
}
|
||||
}
|
||||
|
||||
/** A reference to our client context. */
|
||||
protected CrowdContext _ctx;
|
||||
|
||||
/** The turn game controller for whom we are delegating. */
|
||||
protected TurnGameController _tgctrl;
|
||||
|
||||
/** A reference to our client context. */
|
||||
protected CrowdContext _ctx;
|
||||
|
||||
/** A reference to our game object. */
|
||||
protected GameObject _gameObj;
|
||||
|
||||
|
||||
@@ -1,31 +1,24 @@
|
||||
//
|
||||
// $Id: TurnGameManagerDelegate.java,v 1.3 2002/02/12 07:17:33 mdb Exp $
|
||||
// $Id: TurnGameManagerDelegate.java,v 1.4 2002/02/13 03:21:28 mdb Exp $
|
||||
|
||||
package com.threerings.parlor.turn;
|
||||
|
||||
import com.threerings.crowd.data.PlaceObject;
|
||||
|
||||
import com.threerings.parlor.Log;
|
||||
import com.threerings.parlor.game.GameManager;
|
||||
import com.threerings.parlor.game.GameManagerDelegate;
|
||||
import com.threerings.parlor.util.MathUtil;
|
||||
|
||||
/**
|
||||
* Performs the server-side turn-based game processing for a turn based
|
||||
* game. Games which wish to make use of the turn services must call the
|
||||
* following methods on the delegate at the appropriate times:
|
||||
*
|
||||
* <pre>
|
||||
* init()
|
||||
* gameDidStart()
|
||||
* startTurn()
|
||||
* endTurn()
|
||||
* </pre>
|
||||
*
|
||||
* They must also implement the {@link TurnGameManager} interface. If the
|
||||
* game requires specialized behavior from its delegate, it can extend the
|
||||
* delegate and override the many methods that have been provided for just
|
||||
* such a purpose (e.g. {@link #setFirstTurnHolder}, {@link
|
||||
* #setNextTurnHolder}).
|
||||
* game. Game managers which wish to make use of the turn services must
|
||||
* implement {@link TurnGameManager} either create an instance of this
|
||||
* class, or an instance of a derivation which customizes the behavior,
|
||||
* either of which would be passed to {@link GameManager#addDelegate} to
|
||||
* be activated.
|
||||
*/
|
||||
public class TurnGameManagerDelegate
|
||||
public class TurnGameManagerDelegate extends GameManagerDelegate
|
||||
{
|
||||
/**
|
||||
* Constructs a delegate that will manage the turn game state and call
|
||||
@@ -34,36 +27,10 @@ public class TurnGameManagerDelegate
|
||||
*/
|
||||
public TurnGameManagerDelegate (TurnGameManager tgmgr)
|
||||
{
|
||||
super((GameManager)tgmgr);
|
||||
_tgmgr = tgmgr;
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be called from {@link GameManager#didStartup} to
|
||||
* initialize the delegate.
|
||||
*/
|
||||
public void init (TurnGameObject turnGame)
|
||||
{
|
||||
_turnGame = turnGame;
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be called from {@link GameManager#gameDidStart} to let
|
||||
* the turn delegate perform start of game processing.
|
||||
*/
|
||||
public void gameDidStart ()
|
||||
{
|
||||
// grab the players array
|
||||
_players = _tgmgr.getPlayers();
|
||||
|
||||
// figure out who will be first
|
||||
setFirstTurnHolder();
|
||||
|
||||
// and start the first turn if we should apparently do so
|
||||
if (_turnIdx != -1) {
|
||||
startTurn();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the current turn holder as configured in the
|
||||
* game object.
|
||||
@@ -82,17 +49,6 @@ public class TurnGameManagerDelegate
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called to determine whichi player will take the first
|
||||
* turn. The default implementation chooses a player at random.
|
||||
*/
|
||||
protected void setFirstTurnHolder ()
|
||||
{
|
||||
// TODO: sort out a better random number generator and make it
|
||||
// available via the parlor services
|
||||
_turnIdx = MathUtil.random(_players.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to start the next turn. It calls {@link
|
||||
* TurnGameManager#turnWillStart} to allow our owning manager to
|
||||
@@ -149,6 +105,41 @@ public class TurnGameManagerDelegate
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void didStartup (PlaceObject plobj)
|
||||
{
|
||||
_turnGame = (TurnGameObject)plobj;
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be called from {@link GameManager#gameDidStart} to let
|
||||
* the turn delegate perform start of game processing.
|
||||
*/
|
||||
public void gameDidStart ()
|
||||
{
|
||||
// grab the players array
|
||||
_players = _tgmgr.getPlayers();
|
||||
|
||||
// figure out who will be first
|
||||
setFirstTurnHolder();
|
||||
|
||||
// and start the first turn if we should apparently do so
|
||||
if (_turnIdx != -1) {
|
||||
startTurn();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called to determine whichi player will take the first
|
||||
* turn. The default implementation chooses a player at random.
|
||||
*/
|
||||
protected void setFirstTurnHolder ()
|
||||
{
|
||||
// TODO: sort out a better random number generator and make it
|
||||
// available via the parlor services
|
||||
_turnIdx = MathUtil.random(_players.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called to determine which player will next hold the turn.
|
||||
* The default implementation simply rotates through the players in
|
||||
|
||||
Reference in New Issue
Block a user