Revamped sprite observation and added support for being notified if a path
is cancelled. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2504 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
//
|
||||
// $Id: CollisionEvent.java,v 1.2 2002/12/04 02:45:09 shaper 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, long when, Sprite other)
|
||||
{
|
||||
super(sprite, when);
|
||||
_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,22 @@
|
||||
//
|
||||
// $Id: PathAdapter.java,v 1.1 2003/04/30 00:44:36 mdb Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
import com.threerings.media.util.Path;
|
||||
|
||||
/**
|
||||
* An adapter class for {@link PathObserver}.
|
||||
*/
|
||||
public class PathAdapter implements PathObserver
|
||||
{
|
||||
// documentation inherited from interface
|
||||
public void pathCancelled (Sprite sprite, Path path)
|
||||
{
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public void pathCompleted (Sprite sprite, Path path, long when)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
//
|
||||
// $Id: PathCompletedEvent.java,v 1.3 2002/12/04 02:45:09 shaper Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
import com.threerings.media.util.Path;
|
||||
|
||||
/**
|
||||
* 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, long when, Path path)
|
||||
{
|
||||
super(sprite, when);
|
||||
_path = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path that was just completed.
|
||||
*/
|
||||
public Path getPath ()
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
/** A reference to the completed path. */
|
||||
protected Path _path;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// $Id: PathObserver.java,v 1.1 2003/04/30 00:44:36 mdb Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
import com.threerings.media.util.Path;
|
||||
|
||||
/**
|
||||
* An interface to be implemented by classes that would like to be
|
||||
* notified when a sprite completes or cancels its path.
|
||||
*/
|
||||
public interface PathObserver
|
||||
{
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public void pathCompleted (Sprite sprite, Path path, long when);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: Sprite.java,v 1.63 2003/04/20 04:52:33 mdb Exp $
|
||||
// $Id: Sprite.java,v 1.64 2003/04/30 00:44:36 mdb Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
|
||||
import com.samskivert.util.ObserverList;
|
||||
import com.threerings.util.DirectionCodes;
|
||||
|
||||
import com.threerings.media.AbstractMedia;
|
||||
@@ -224,9 +225,7 @@ public abstract class Sprite extends AbstractMedia
|
||||
public void move (Path path)
|
||||
{
|
||||
// if there's a previous path, let it know that it's going away
|
||||
if (_path != null) {
|
||||
_path.wasRemoved(this);
|
||||
}
|
||||
cancelMove();
|
||||
|
||||
// save off this path
|
||||
_path = path;
|
||||
@@ -240,12 +239,13 @@ public abstract class Sprite extends AbstractMedia
|
||||
*/
|
||||
public void cancelMove ()
|
||||
{
|
||||
// TODO: make sure we come to a stop on a full coordinate,
|
||||
// even in the case where we aborted a path mid-traversal.
|
||||
|
||||
if (_path != null) {
|
||||
_path.wasRemoved(this);
|
||||
Path oldpath = _path;
|
||||
_path = null;
|
||||
oldpath.wasRemoved(this);
|
||||
if (_observers != null) {
|
||||
_observers.apply(new CancelledOp(this, oldpath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,14 +271,12 @@ public abstract class Sprite extends AbstractMedia
|
||||
*/
|
||||
public void pathCompleted (long timestamp)
|
||||
{
|
||||
// keep a reference to the path just completed
|
||||
Path oldpath = _path;
|
||||
// let the path know that it's audi
|
||||
_path.wasRemoved(this);
|
||||
// clear out the path we've now finished
|
||||
_path = null;
|
||||
// inform observers that we've finished our path
|
||||
notifyObservers(new PathCompletedEvent(this, timestamp, oldpath));
|
||||
oldpath.wasRemoved(this);
|
||||
if (_observers != null) {
|
||||
_observers.apply(new CompletedOp(this, oldpath, timestamp));
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
@@ -335,7 +333,7 @@ public abstract class Sprite extends AbstractMedia
|
||||
*
|
||||
* @param obs the sprite observer.
|
||||
*/
|
||||
public void addSpriteObserver (SpriteObserver obs)
|
||||
public void addSpriteObserver (Object obs)
|
||||
{
|
||||
addObserver(obs);
|
||||
}
|
||||
@@ -343,23 +341,11 @@ public abstract class Sprite extends AbstractMedia
|
||||
/**
|
||||
* Remove a sprite observer.
|
||||
*/
|
||||
public void removeSpriteObserver (SpriteObserver obs)
|
||||
public void removeSpriteObserver (Object obs)
|
||||
{
|
||||
removeObserver(obs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inform all sprite observers of a sprite event.
|
||||
*
|
||||
* @param event the sprite event.
|
||||
*/
|
||||
protected void notifyObservers (SpriteEvent event)
|
||||
{
|
||||
if (_observers != null) {
|
||||
((SpriteManager)_mgr).dispatchEvent(_observers, event);
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void toString (StringBuffer buf)
|
||||
{
|
||||
@@ -370,6 +356,46 @@ public abstract class Sprite extends AbstractMedia
|
||||
buf.append(", oyoff=").append(_oyoff);
|
||||
}
|
||||
|
||||
/** 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, long when) {
|
||||
_sprite = sprite;
|
||||
_path = path;
|
||||
_when = when;
|
||||
}
|
||||
|
||||
public boolean apply (Object observer) {
|
||||
if (observer instanceof PathObserver) {
|
||||
((PathObserver)observer).pathCompleted(_sprite, _path, _when);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Sprite _sprite;
|
||||
protected Path _path;
|
||||
protected long _when;
|
||||
}
|
||||
|
||||
/** The location of the sprite's origin in pixel coordinates. If the
|
||||
* sprite positions itself via a hotspot that is not the upper left
|
||||
* coordinate of the sprite's bounds, the offset to the hotspot should
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
//
|
||||
// $Id: SpriteEvent.java,v 1.3 2002/12/04 02:45:09 shaper Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/**
|
||||
* Create a sprite event.
|
||||
*
|
||||
* @param sprite the involved sprite.
|
||||
* @param when the time at which this event took place.
|
||||
*/
|
||||
public SpriteEvent (Sprite sprite, long when)
|
||||
{
|
||||
_sprite = sprite;
|
||||
_when = when;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sprite involved in the event.
|
||||
*/
|
||||
public Sprite getSprite ()
|
||||
{
|
||||
return _sprite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time associated with this event.
|
||||
*/
|
||||
public long getWhen ()
|
||||
{
|
||||
return _when;
|
||||
}
|
||||
|
||||
/** The sprite associated with this event. */
|
||||
protected Sprite _sprite;
|
||||
|
||||
/** The time associated with this event. */
|
||||
protected long _when;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: SpriteManager.java,v 1.40 2002/12/15 23:12:37 shaper Exp $
|
||||
// $Id: SpriteManager.java,v 1.41 2003/04/30 00:44:36 mdb Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
@@ -11,8 +11,6 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.samskivert.util.ObserverList;
|
||||
|
||||
import com.threerings.media.AbstractMediaManager;
|
||||
import com.threerings.media.Log;
|
||||
import com.threerings.media.RegionManager;
|
||||
@@ -122,13 +120,6 @@ public class SpriteManager extends AbstractMediaManager
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void dispatchEvent (ObserverList observers, Object event)
|
||||
{
|
||||
_dispatchOp.init((SpriteEvent)event);
|
||||
observers.apply(_dispatchOp);
|
||||
}
|
||||
|
||||
// NOTE- collision handling code is turned off for now. To re-implement,
|
||||
// a new array should be kept with sprites sorted in some sort of x/y order
|
||||
//
|
||||
@@ -201,23 +192,4 @@ public class SpriteManager extends AbstractMediaManager
|
||||
// return (s2.getX() - s1.getX());
|
||||
// }
|
||||
// }
|
||||
|
||||
/** Used by {@link #dispatchEvent}. */
|
||||
protected static class DispatchOp implements ObserverList.ObserverOp
|
||||
{
|
||||
public void init (SpriteEvent event)
|
||||
{
|
||||
_event = event;
|
||||
}
|
||||
|
||||
public boolean apply (Object observer)
|
||||
{
|
||||
((SpriteObserver)observer).handleEvent(_event);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected SpriteEvent _event;
|
||||
};
|
||||
|
||||
protected DispatchOp _dispatchOp = new DispatchOp();
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
//
|
||||
// $Id: SpriteObserver.java,v 1.4 2002/01/11 16:17:33 shaper 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.
|
||||
*/
|
||||
public interface SpriteObserver
|
||||
{
|
||||
/**
|
||||
* This method is called by the {@link SpriteManager} when something
|
||||
* interesting is accomplished by or happens to the sprite.
|
||||
*
|
||||
* @param event the sprite event.
|
||||
*/
|
||||
public void handleEvent (SpriteEvent event);
|
||||
}
|
||||
Reference in New Issue
Block a user