From 78d32af2f924b53ffa764a74ba83e2226e5ce722 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sun, 1 May 2005 23:42:46 +0000 Subject: [PATCH] Separate our scene graph into user interface and geometry, reflect that in the services we make available via the context. Added a sprite and path framework not wildly unlike that in media but based around the JME primitives. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3532 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/jme/JmeApp.java | 34 +++- src/java/com/threerings/jme/JmeContext.java | 22 ++- src/java/com/threerings/jme/sprite/Path.java | 51 ++++++ .../threerings/jme/sprite/PathObserver.java | 44 +++++ .../com/threerings/jme/sprite/Sprite.java | 154 ++++++++++++++++++ .../threerings/jme/sprite/SpriteObserver.java | 29 ++++ 6 files changed, 320 insertions(+), 14 deletions(-) create mode 100644 src/java/com/threerings/jme/sprite/Path.java create mode 100644 src/java/com/threerings/jme/sprite/PathObserver.java create mode 100644 src/java/com/threerings/jme/sprite/Sprite.java create mode 100644 src/java/com/threerings/jme/sprite/SpriteObserver.java diff --git a/src/java/com/threerings/jme/JmeApp.java b/src/java/com/threerings/jme/JmeApp.java index a80c9e18e..390c3ce33 100644 --- a/src/java/com/threerings/jme/JmeApp.java +++ b/src/java/com/threerings/jme/JmeApp.java @@ -130,6 +130,8 @@ public class JmeApp initInterface(); // update everything for the zeroth tick + _iface.updateRenderState(); + _geom.updateRenderState(); _root.updateGeometricState(0f, true); _root.updateRenderState(); @@ -261,11 +263,15 @@ public class JmeApp { _root = new Node("Root"); + // set up a node for our geometry + _geom = new Node("Geometry"); + // set up a zbuffer ZBufferState zbuf = _display.getRenderer().createZBufferState(); zbuf.setEnabled(true); zbuf.setFunction(ZBufferState.CF_LEQUAL); - _root.setRenderState(zbuf); + _geom.setRenderState(zbuf); + _root.attachChild(_geom); } /** @@ -280,10 +286,9 @@ public class JmeApp light.setEnabled(true); _lights = _display.getRenderer().createLightState(); - // _lights.setEnabled(true); - _lights.setEnabled(false); + _lights.setEnabled(true); _lights.attach(light); - _root.setRenderState(_lights); + _geom.setRenderState(_lights); } /** @@ -291,6 +296,9 @@ public class JmeApp */ protected void initInterface () { + // set up a node for our interface + _iface = new Node("Interface"); + _root.attachChild(_iface); } /** @@ -424,12 +432,24 @@ public class JmeApp /** Provides access to various needed bits. */ protected JmeContext _ctx = new JmeContext() { + public DisplaySystem getDisplay () { + return _display; + } + public Renderer getRenderer () { return _display.getRenderer(); } - public Node getRoot () { - return _root; + public Camera getCamera () { + return _camera; + } + + public Node getGeometry () { + return _geom; + } + + public Node getInterface () { + return _iface; } public InputHandler getInputHandler () { @@ -456,7 +476,7 @@ public class JmeApp protected boolean _finished; protected int _failures; - protected Node _root; + protected Node _root, _geom, _iface; protected LightState _lights; protected StatsDisplay _stats; diff --git a/src/java/com/threerings/jme/JmeContext.java b/src/java/com/threerings/jme/JmeContext.java index 71180c45e..98205ea30 100644 --- a/src/java/com/threerings/jme/JmeContext.java +++ b/src/java/com/threerings/jme/JmeContext.java @@ -22,8 +22,10 @@ package com.threerings.jme; import com.jme.input.InputHandler; +import com.jme.renderer.Camera; import com.jme.renderer.Renderer; import com.jme.scene.Node; +import com.jme.system.DisplaySystem; import com.jme.bui.event.InputDispatcher; @@ -33,17 +35,23 @@ import com.jme.bui.event.InputDispatcher; */ public interface JmeContext { - /** Returns the renderer being used to draw everythign. */ + /** Returns the display to which we are rendering. */ + public DisplaySystem getDisplay (); + + /** Returns the renderer being used to draw everything. */ public Renderer getRenderer (); - /** Returns the root of our scene graph. */ - public Node getRoot (); + /** Returns the camera being used to view the scene. */ + public Camera getCamera (); -// /** Returns our main (unbuffered) input handler. */ -// public InputHandler getInputHandler (); + /** Returns the main geometry of our scene graph. */ + public Node getGeometry (); -// /** Returns our buffered input handler. */ -// public InputHandler getBufferedInputHandler (); + /** Returns the main interface node of our scene graph. */ + public Node getInterface (); + + /** Returns our main input handler. */ + public InputHandler getInputHandler (); /** Returns our main input dispatcher. */ public InputDispatcher getInputDispatcher (); diff --git a/src/java/com/threerings/jme/sprite/Path.java b/src/java/com/threerings/jme/sprite/Path.java new file mode 100644 index 000000000..3693d0f5d --- /dev/null +++ b/src/java/com/threerings/jme/sprite/Path.java @@ -0,0 +1,51 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.sprite; + +import com.jme.scene.Controller; + +/** + * Defines a framework for moving sprites around and notifying interested + * parties when the sprite has completed its path or if the path has been + * cancelled. + */ +public abstract class Path extends Controller +{ + /** + * Creates and initializes this path with the sprite it will be + * manipulating. + */ + protected Path (Sprite sprite) + { + _sprite = sprite; + } + + /** + * Called when this path is removed from its sprite (either due to + * completion or cancellation). + */ + public void wasRemoved () + { + } + + protected Sprite _sprite; +} diff --git a/src/java/com/threerings/jme/sprite/PathObserver.java b/src/java/com/threerings/jme/sprite/PathObserver.java new file mode 100644 index 000000000..597bb22ec --- /dev/null +++ b/src/java/com/threerings/jme/sprite/PathObserver.java @@ -0,0 +1,44 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.sprite; + +/** + * Implement this interface to find out when a sprite completes or cancels + * its path. + */ +public interface PathObserver extends SpriteObserver +{ + /** + * Called when a sprite's path is cancelled either because a new path + * was started or the path was explicitly cancelled with {@link + * Sprite#cancelMove}. + */ + public void pathCancelled (Sprite sprite, Path path); + + /** + * Called when a sprite completes its traversal of a path. + * + * @param sprite the sprite that completed its path. + * @param path the path that was completed. + */ + public void pathCompleted (Sprite sprite, Path path); +} diff --git a/src/java/com/threerings/jme/sprite/Sprite.java b/src/java/com/threerings/jme/sprite/Sprite.java new file mode 100644 index 000000000..9cc372906 --- /dev/null +++ b/src/java/com/threerings/jme/sprite/Sprite.java @@ -0,0 +1,154 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.sprite; + +import com.jme.scene.Node; + +import com.samskivert.util.ObserverList; + +/** + * Represents a visual entity that one controls as a single unit. Sprites + * can be made to follow paths which is one of their primary reasons for + * existence. + */ +public class Sprite extends Node +{ + public Sprite () + { + super(""); + setName("sprite:" + hashCode()); + } + + /** + * Adds an observer to this sprite. Observers are notified when path + * related events take place. + */ + public void addObserver (SpriteObserver obs) + { + if (_observers == null) { + _observers = new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); + } + _observers.add(obs); + } + + /** + * Removes the specified observer from this sprite. + */ + public void removeObserver (SpriteObserver obs) + { + if (_observers != null) { + _observers.remove(obs); + } + } + + /** + * Returns true if this sprite is moving along a path, false if not. + */ + public boolean isMoving () + { + return _path != null; + } + + /** + * Instructs this sprite to move along the specified path. Any + * currently executing path will be cancelled. + */ + public void move (Path path) + { + // if there's a previous path, let it know that it's going away + cancelMove(); + + // save off this path + _path = path; + } + + /** + * Cancels any currently executing path. Any registered observers will + * be notified of the cancellation. + */ + public void cancelMove () + { + if (_path != null) { + Path oldpath = _path; + _path = null; + oldpath.wasRemoved(); + if (_observers != null) { + _observers.apply(new CancelledOp(this, oldpath)); + } + } + } + + /** + * Called by the active path when it has completed. Note: + * don't call this method unless you are implementing a {@link Path}. + */ + public void pathCompleted () + { + Path oldpath = _path; + _path = null; + oldpath.wasRemoved(); + if (_observers != null) { + _observers.apply(new CompletedOp(this, oldpath)); + } + } + + /** Used to dispatch {@link PathObserver#pathCancelled}. */ + protected static class CancelledOp implements ObserverList.ObserverOp + { + public CancelledOp (Sprite sprite, Path path) { + _sprite = sprite; + _path = path; + } + + public boolean apply (Object observer) { + if (observer instanceof PathObserver) { + ((PathObserver)observer).pathCancelled(_sprite, _path); + } + return true; + } + + protected Sprite _sprite; + protected Path _path; + } + + /** Used to dispatch {@link PathObserver#pathCompleted}. */ + protected static class CompletedOp implements ObserverList.ObserverOp + { + public CompletedOp (Sprite sprite, Path path) { + _sprite = sprite; + _path = path; + } + + public boolean apply (Object observer) { + if (observer instanceof PathObserver) { + ((PathObserver)observer).pathCompleted(_sprite, _path); + } + return true; + } + + protected Sprite _sprite; + protected Path _path; + } + + protected ObserverList _observers; + protected Path _path; +} diff --git a/src/java/com/threerings/jme/sprite/SpriteObserver.java b/src/java/com/threerings/jme/sprite/SpriteObserver.java new file mode 100644 index 000000000..4b1249e72 --- /dev/null +++ b/src/java/com/threerings/jme/sprite/SpriteObserver.java @@ -0,0 +1,29 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.sprite; + +/** + * The super-interface for all sprite observers. + */ +public interface SpriteObserver +{ +}