Modified sprite event mechanism to used classes derived from SpriteEvent

rather than codes to indicate different types of events. Also modified
name of sprite observer callback as spriteChanged() implies that the
sprite actually changed which it may not have, whereas handleEvent()
doesn't imply anything other than the sprite observer is being asked to
deal with a sprite event.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@338 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-09-13 19:36:20 +00:00
parent 780e39d4e9
commit 00fde3eeb3
6 changed files with 88 additions and 71 deletions
@@ -0,0 +1,32 @@
//
// $Id: CollisionEvent.java,v 1.1 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
/**
* A collision event is dispatched when a sprite collides with another
* sprite.
*/
public class CollisionEvent extends SpriteEvent
{
/**
* Constructs a collision event for the specified sprites.
*/
public CollisionEvent (Sprite sprite, Sprite other)
{
super(sprite);
_other = other;
}
/**
* Returns the other sprite that was involved in this collision (the
* first sprite being available via {@link #getSprite}.
*/
public Sprite getOther ()
{
return _other;
}
/** A reference to the other sprite. */
protected Sprite _other;
}
@@ -0,0 +1,32 @@
//
// $Id: PathCompletedEvent.java,v 1.1 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
/**
* A path completed event is dispatched when a sprite completes a path
* along which it has been requested to move.
*/
public class PathCompletedEvent extends SpriteEvent
{
/**
* Constructs a path completed event for the specified sprite and
* path.
*/
public PathCompletedEvent (Sprite sprite, Path path)
{
super(sprite);
_path = path;
}
/**
* Returns the path that was just completed.
*/
public Path getPath ()
{
return _path;
}
/** A reference to the completed path. */
protected Path _path;
}
@@ -1,5 +1,5 @@
//
// $Id: Sprite.java,v 1.20 2001/09/13 19:10:26 mdb Exp $
// $Id: Sprite.java,v 1.21 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
@@ -323,7 +323,7 @@ public class Sprite
protected void pathCompleted ()
{
// inform observers that we've finished our path
notifyObservers(SpriteEvent.FINISHED_PATH, _path);
notifyObservers(new PathCompletedEvent(this, _path));
// we no longer want to keep a reference to this path
_path = null;
@@ -444,14 +444,14 @@ public class Sprite
* @param eventCode the type of sprite event.
* @param arg the argument associated with the event.
*/
protected void notifyObservers (int eventCode, Object arg)
protected void notifyObservers (SpriteEvent event)
{
if (_observers == null) {
return;
if (_observers != null) {
// we pass this notification off to the sprite manager so that
// it can dispatch all of the notifications at once after all
// ticking has been completed
_spritemgr.notifySpriteObservers(_observers, event);
}
SpriteEvent evt = new SpriteEvent(this, eventCode, arg);
_spritemgr.notifySpriteObservers(_observers, evt);
}
/**
@@ -1,11 +1,11 @@
//
// $Id: SpriteEvent.java,v 1.1 2001/09/07 23:01:53 shaper Exp $
// $Id: SpriteEvent.java,v 1.2 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
/**
* A sprite event is sent to all sprite observers for a sprite
* whenever one of the various possible sprite events takes place.
* A sprite event is sent to all of a sprite's observers whenever one of
* the various possible sprite events takes place.
*/
public class SpriteEvent
{
@@ -13,65 +13,20 @@ public class SpriteEvent
* Create a sprite event.
*
* @param sprite the involved sprite.
* @param eventCode the event code.
* @param arg the event argument.
*/
public SpriteEvent (Sprite sprite, int eventCode, Object arg)
public SpriteEvent (Sprite sprite)
{
_sprite = sprite;
_eventCode = eventCode;
_arg = arg;
}
/**
* Return the sprite involved in the event.
* Returns the sprite involved in the event.
*/
public Sprite getSprite ()
{
return _sprite;
}
/**
* Return the event code for the event.
*/
public int getEventCode ()
{
return _eventCode;
}
/**
* Return the argument associated with the event.
*/
public Object getArgument ()
{
return _arg;
}
/**
* Event code noting that the sprite completed a path node. The
* argument to the event is the {@link PathNode} completed.
*/
public static final int FINISHED_PATH_NODE = 0;
/**
* Event code noting that the sprite completed its entire path.
* The argument to the event is the {@link Path} completed.
*/
public static final int FINISHED_PATH = 1;
/**
* Event code noting that the sprite collided with another
* sprite. The argument to the event is the {@link Sprite} the
* observed sprite collided with.
*/
public static final int COLLIDED_SPRITE = 2;
/** The sprite associated with this event. */
protected Sprite _sprite;
/** The event code. */
protected int _eventCode;
/** The argument associated with this event. */
protected Object _arg;
}
@@ -1,5 +1,5 @@
//
// $Id: SpriteManager.java,v 1.13 2001/09/13 19:10:26 mdb Exp $
// $Id: SpriteManager.java,v 1.14 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
@@ -256,8 +256,7 @@ public class SpriteManager
}
if (obounds.intersects(bounds)) {
sprite.notifyObservers(SpriteEvent.COLLIDED_SPRITE, other);
other.notifyObservers(SpriteEvent.COLLIDED_SPRITE, sprite);
sprite.notifyObservers(new CollisionEvent(sprite, other));
}
}
}
@@ -277,7 +276,7 @@ public class SpriteManager
int osize = observers.size();
for (int jj = 0; jj < osize; jj++) {
SpriteObserver obs = (SpriteObserver)observers.get(jj);
obs.spriteChanged(evt);
obs.handleEvent(evt);
}
}
}
@@ -1,22 +1,21 @@
//
// $Id: SpriteObserver.java,v 1.2 2001/09/07 23:01:53 shaper Exp $
// $Id: SpriteObserver.java,v 1.3 2001/09/13 19:36:20 mdb Exp $
package com.threerings.media.sprite;
/**
* An interface to be implemented by classes that would like to
* observe a sprite and be notified of meaningful events as it, and
* any other sprites, move about.
* An interface to be implemented by classes that would like to observe a
* sprite and be notified of meaningful events as it, and any other
* sprites, move about.
*/
public interface SpriteObserver
{
/**
* This method is called by the {@link SpriteManager} when
* something interesting is accomplished by or happens to the
* sprite.
* This method is called by the {@link SpriteManager} when something
* interesting is accomplished by or happens to the sprite.
*
* @param sprite the involved sprite.
* @param sprite the sprite involved.
* @param event the sprite event.
*/
public void spriteChanged (SpriteEvent event);
public void handleEvent (SpriteEvent event);
}