diff --git a/src/java/com/threerings/whirled/client/SceneController.java b/src/java/com/threerings/whirled/client/SceneController.java index 9756a3fc1..6f4524b9f 100644 --- a/src/java/com/threerings/whirled/client/SceneController.java +++ b/src/java/com/threerings/whirled/client/SceneController.java @@ -1,18 +1,73 @@ // -// $Id: SceneController.java,v 1.1 2001/11/12 20:56:55 mdb Exp $ +// $Id: SceneController.java,v 1.2 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.client; +import com.threerings.presents.dobj.MessageEvent; +import com.threerings.presents.dobj.MessageListener; + import com.threerings.crowd.client.PlaceController; +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.crowd.data.PlaceObject; +import com.threerings.crowd.util.CrowdContext; + +import com.threerings.whirled.data.SceneCodes; +import com.threerings.whirled.data.SceneUpdate; +import com.threerings.whirled.util.WhirledContext; /** * The base scene controller class. It is expected that users of the * Whirled services will extend this controller class when creating - * specialized controllers for their scenes. Presently there are no basic - * scene services provided by this controller, but its existence affords - * the addition of such services should they become necessary in the - * future. + * specialized controllers for their scenes. */ public abstract class SceneController extends PlaceController { + // documentation inherited + public void init (CrowdContext ctx, PlaceConfig config) + { + super.init(ctx, config); + _ctx = (WhirledContext)ctx; + } + + // documentation inherited + public void willEnterPlace (PlaceObject plobj) + { + super.willEnterPlace(plobj); + plobj.addListener(_updateListener); + } + + // documentation inherited + public void didLeavePlace (PlaceObject plobj) + { + super.didLeavePlace(plobj); + plobj.removeListener(_updateListener); + } + + /** + * This method is called if a scene update is recorded while we + * currently occupy a scene. The default implementation will update + * our local scene and scene model, but derived classes will likely + * want to ensure that the update is properly displayed. + */ + protected void sceneUpdated (SceneUpdate update) + { + // apply the update to the scene + _ctx.getSceneDirector().getScene().updateReceived(update); + + // we don't persistify these updates in this circumstance, but + // next time we come to this scene we'll redownload the update and + // apply it to the repository; as the updates are meant to be very + // small, this shouldn't be horribly less efficient + } + + /** Used to listen for scene updates. */ + protected MessageListener _updateListener = new MessageListener() { + public void messageReceived (MessageEvent event) { + if (event.getName().equals(SceneCodes.SCENE_UPDATE)) { + sceneUpdated((SceneUpdate)event.getArgs()[0]); + } + } + }; + + protected WhirledContext _ctx; } diff --git a/src/java/com/threerings/whirled/data/Scene.java b/src/java/com/threerings/whirled/data/Scene.java index fb89834df..e218affea 100644 --- a/src/java/com/threerings/whirled/data/Scene.java +++ b/src/java/com/threerings/whirled/data/Scene.java @@ -1,5 +1,5 @@ // -// $Id: Scene.java,v 1.8 2003/02/12 07:23:31 mdb Exp $ +// $Id: Scene.java,v 1.9 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.data; @@ -50,6 +50,14 @@ public interface Scene */ public void setVersion (int version); + /** + * Called to inform the scene that an update has been received while + * the scene was resolved and active. The update should be applied to + * the underlying scene model and any derivative data should be + * appropriately updated. + */ + public void updateReceived (SceneUpdate update); + /** * Returns the scene model from which this scene was created. */ diff --git a/src/java/com/threerings/whirled/data/SceneCodes.java b/src/java/com/threerings/whirled/data/SceneCodes.java index f60d5353a..0d688a6e0 100644 --- a/src/java/com/threerings/whirled/data/SceneCodes.java +++ b/src/java/com/threerings/whirled/data/SceneCodes.java @@ -1,5 +1,5 @@ // -// $Id: SceneCodes.java,v 1.3 2002/08/14 19:07:57 mdb Exp $ +// $Id: SceneCodes.java,v 1.4 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.data; @@ -11,4 +11,6 @@ import com.threerings.whirled.client.SceneDirector; */ public interface SceneCodes extends LocationCodes { + /** The message identifier for scene update messages. */ + public static final String SCENE_UPDATE = "scene_update"; } diff --git a/src/java/com/threerings/whirled/data/SceneImpl.java b/src/java/com/threerings/whirled/data/SceneImpl.java index 22f7b1702..d3734351d 100644 --- a/src/java/com/threerings/whirled/data/SceneImpl.java +++ b/src/java/com/threerings/whirled/data/SceneImpl.java @@ -1,10 +1,11 @@ // -// $Id: SceneImpl.java,v 1.1 2003/02/12 07:23:31 mdb Exp $ +// $Id: SceneImpl.java,v 1.2 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.data; import com.threerings.crowd.data.PlaceConfig; -import com.threerings.whirled.data.SceneModel; + +import com.threerings.whirled.Log; /** * An implementation of the {@link Scene} interface. @@ -72,6 +73,20 @@ public class SceneImpl implements Scene _model.version = version; } + // documentation inherited from interface + public void updateReceived (SceneUpdate update) + { + try { + // validate and apply the update + update.validate(_model); + update.apply(_model); + } catch (Exception e) { + Log.warning("Error applying update [scene=" + this + + ", update=" + update + "]."); + Log.logStackTrace(e); + } + } + // documentation inherited from interface public SceneModel getSceneModel () { diff --git a/src/java/com/threerings/whirled/server/SceneManager.java b/src/java/com/threerings/whirled/server/SceneManager.java index 6c729dba7..2af5c6f73 100644 --- a/src/java/com/threerings/whirled/server/SceneManager.java +++ b/src/java/com/threerings/whirled/server/SceneManager.java @@ -1,5 +1,5 @@ // -// $Id: SceneManager.java,v 1.13 2003/06/11 02:48:07 mdb Exp $ +// $Id: SceneManager.java,v 1.14 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.server; @@ -10,6 +10,7 @@ import com.threerings.presents.util.Invoker; import com.threerings.whirled.Log; import com.threerings.whirled.data.Scene; +import com.threerings.whirled.data.SceneCodes; import com.threerings.whirled.data.SceneModel; import com.threerings.whirled.data.SceneUpdate; import com.threerings.whirled.server.WhirledServer; @@ -134,7 +135,8 @@ public class SceneManager extends PlaceManager } }); - // TODO: broadcast the update to all occupants of the scene + // broadcast the update to all occupants of the scene + _plobj.postMessage(SceneCodes.SCENE_UPDATE, new Object[] { update }); } // documentation inherited diff --git a/src/java/com/threerings/whirled/spot/data/SpotSceneImpl.java b/src/java/com/threerings/whirled/spot/data/SpotSceneImpl.java index c0983d162..47ea2d932 100644 --- a/src/java/com/threerings/whirled/spot/data/SpotSceneImpl.java +++ b/src/java/com/threerings/whirled/spot/data/SpotSceneImpl.java @@ -1,5 +1,5 @@ // -// $Id: SpotSceneImpl.java,v 1.1 2003/02/12 07:23:31 mdb Exp $ +// $Id: SpotSceneImpl.java,v 1.2 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.spot.data; @@ -23,7 +23,12 @@ public class SpotSceneImpl public SpotSceneImpl (SpotSceneModel smodel) { _smodel = smodel; + readPortals(); + } + protected void readPortals () + { + _portals.clear(); for (int ii = 0, ll = _smodel.portals.length; ii < ll; ii++) { Portal port = _smodel.portals[ii]; _portals.put(port.portalId, port); @@ -110,6 +115,15 @@ public class SpotSceneImpl _smodel.defaultEntranceId = (portal == null) ? -1 : portal.portalId; } + /** + * This should be called if a scene update was received that caused + * our underlying scene model to change. + */ + public void updateReceived () + { + readPortals(); + } + /** A casted reference to our scene model. */ protected SpotSceneModel _smodel; diff --git a/src/java/com/threerings/whirled/util/UpdateList.java b/src/java/com/threerings/whirled/util/UpdateList.java index 74198cf7b..557c90b95 100644 --- a/src/java/com/threerings/whirled/util/UpdateList.java +++ b/src/java/com/threerings/whirled/util/UpdateList.java @@ -1,5 +1,5 @@ // -// $Id: UpdateList.java,v 1.1 2003/02/12 07:23:32 mdb Exp $ +// $Id: UpdateList.java,v 1.2 2003/06/11 04:14:11 mdb Exp $ package com.threerings.whirled.util; @@ -61,7 +61,7 @@ public class UpdateList */ public SceneUpdate[] getUpdates (int fromVersion) { - if (fromVersion < _minVersion) { + if (_minVersion == -1 || fromVersion < _minVersion) { return null; }