Renamed MobileSprite to AmbulatorySprite and fixed up to face the

proper direction while following a path.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@153 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-08-02 21:02:57 +00:00
parent c41c097cd4
commit 254433f5fb
4 changed files with 51 additions and 56 deletions
@@ -1,5 +1,5 @@
//
// $Id: MobileSprite.java,v 1.3 2001/08/02 00:42:02 shaper Exp $
// $Id: CharacterSprite.java,v 1.1 2001/08/02 21:02:56 shaper Exp $
package com.threerings.miso.sprite;
@@ -8,31 +8,31 @@ import com.threerings.miso.tile.Tile;
import com.threerings.miso.tile.TileManager;
/**
* A MobileSprite is a sprite that can face in one of eight compass
* directions and that can be animated moving from one location to
* another (e.g., a human's legs move and arms swing.)
* A <code>AmbulatorySprite</code> is a sprite that can face in one of
* eight compass directions and animate itself walking along any
* chosen path.
*/
public class MobileSprite extends Sprite
public class AmbulatorySprite extends Sprite
{
/**
* Construct a MobileSprite object, loading the tiles used to
* display the sprite from specified tileset via the given tile
* manager.
* Construct a <code>AmbulatorySprite</code>, loading the tiles
* used to display the sprite from the given tileset via the given
* tile manager.
*
* @param x the sprite x-position in pixels.
* @param y the sprite y-position in pixels.
* @param tilemgr the tile manager to retrieve tiles from.
* @param tsid the tileset id containing the sprite tiles.
*/
public MobileSprite (SpriteManager spritemgr, int x, int y,
TileManager tilemgr, int tsid)
public AmbulatorySprite (SpriteManager spritemgr, int x, int y,
TileManager tilemgr, int tsid)
{
super(spritemgr, x, y);
_charTiles = getTiles(tilemgr, tsid);
_dirTiles = getTiles(tilemgr, tsid);
_dir = Path.DIR_SOUTH;
setTiles(_charTiles[0]);
setTiles(_dirTiles[0]);
}
/**
@@ -70,37 +70,23 @@ public class MobileSprite extends Sprite
* @param x the destination x-position.
* @param y the destination y-position.
*/
public void setDestination (int x, int y)
protected void moveAlongPath ()
{
// select the new path node
super.moveAlongPath();
// bail if we're at the end of the path
if (_dest == null) {
// stop any walking animation
setAnimationDelay(ANIM_NONE);
return;
}
// update the sprite tiles to reflect the direction
setTiles(_charTiles[_dir = getDirection(x, y)]);
setTiles(_dirTiles[_dir = _dest.dir]);
// call superclass to effect the beginnings of the move
super.setDestination(x, y);
if (_state == STATE_MOVING) {
setAnimationDelay(0);
}
}
/**
* Return the directional constant corresponding to the direction
* the specified point is in from the sprite.
*/
protected int getDirection (int x, int y)
{
if (x >= this.x - DIR_BUFFER && x <= this.x + DIR_BUFFER) {
return (y < this.y) ? Path.DIR_NORTH : Path.DIR_SOUTH;
} else if (y >= this.y - DIR_BUFFER && y <= this.y + DIR_BUFFER) {
return (x >= this.x) ? Path.DIR_EAST : Path.DIR_WEST;
} else if (x > this.x) {
return (y < this.y) ? Path.DIR_NORTHEAST : Path.DIR_SOUTHEAST;
} else {
return (y < this.y) ? Path.DIR_NORTHWEST : Path.DIR_SOUTHWEST;
}
// start tile animation to show movement
setAnimationDelay(0);
}
/** The number of frames of animation for each direction. */
@@ -113,7 +99,7 @@ public class MobileSprite extends Sprite
protected static final int DIR_BUFFER = 20;
/** The animation frames for the sprite facing each direction. */
protected Tile[][] _charTiles;
protected Tile[][] _dirTiles;
/** The direction the sprite is currently facing. */
protected int _dir;
@@ -1,5 +1,5 @@
//
// $Id: LineSegmentPath.java,v 1.2 2001/08/02 20:43:03 shaper Exp $
// $Id: LineSegmentPath.java,v 1.3 2001/08/02 21:02:56 shaper Exp $
package com.threerings.miso.sprite;
@@ -7,11 +7,11 @@ import java.util.ArrayList;
import java.util.Enumeration;
/**
* The Path class represents the path a sprite follows while
* meandering about the screen. There must be at least two nodes in
* any worthwhile path. The direction of the first node in the path
* is meaningless since the sprite begins at that node and will
* therefore never be heading towards it.
* The <code>Path</code> class represents the path a sprite follows
* while meandering about the screen. There must be at least two
* nodes in any worthwhile path. The direction of the first node in
* the path is meaningless since the sprite begins at that node and
* will therefore never be heading towards it.
*/
public class Path
{
@@ -29,13 +29,21 @@ public class Path
public static final int DIR_SOUTHEAST = 7;
/**
* Construct a Path object.
* Construct a <code>Path</code> object.
*/
public Path ()
{
_nodes = new ArrayList();
}
/**
* Construct a <code>Path</code> object with the specified
* starting node coordinates. An arbitrary direction will be
* assigned to the starting node.
*
* @param x the starting node x-position.
* @param y the starting node y-position.
*/
public Path (int x, int y)
{
_nodes = new ArrayList();
@@ -1,12 +1,13 @@
//
// $Id: PathNode.java,v 1.2 2001/08/02 20:43:03 shaper Exp $
// $Id: PathNode.java,v 1.3 2001/08/02 21:02:56 shaper Exp $
package com.threerings.miso.sprite;
import java.awt.Point;
/**
* The PathNode object is a single destination point in a Path.
* The <code>PathNode</code> is a single destination point in a
* <code>Path</code>.
*/
public class PathNode
{
@@ -17,7 +18,7 @@ public class PathNode
public int dir;
/**
* Construct a PathNode object.
* Construct a <code>PathNode</code> object.
*
* @param x the node x-position.
* @param y the node y-position.
@@ -1,5 +1,5 @@
//
// $Id: ViewerFrame.java,v 1.5 2001/08/02 00:42:02 shaper Exp $
// $Id: ViewerFrame.java,v 1.6 2001/08/02 21:02:57 shaper Exp $
package com.threerings.miso.viewer;
@@ -41,12 +41,12 @@ class ViewerFrame extends JFrame implements WindowListener
TileManager tilemgr = _ctx.getTileManager();
// add the test character sprite to the sprite manager
MobileSprite ms =
new MobileSprite(spritemgr, 300, 300, tilemgr, TSID_CHAR);
spritemgr.addSprite(ms);
AmbulatorySprite sprite =
new AmbulatorySprite(spritemgr, 300, 300, tilemgr, TSID_CHAR);
spritemgr.addSprite(sprite);
// set up the scene view panel with a default scene
SceneViewPanel svpanel = new SceneViewPanel(_ctx, spritemgr, ms);
SceneViewPanel svpanel = new SceneViewPanel(_ctx, spritemgr, sprite);
// add the scene view panel
getContentPane().add(svpanel, BorderLayout.CENTER);