Modified sprite to use iterator rather than enumerator. Made x and y

members protected and provided accessors. Added toString() to Path.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@300 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-08-21 21:18:42 +00:00
parent 7f49cfb35a
commit 8aed4795e1
3 changed files with 64 additions and 67 deletions
@@ -1,10 +1,10 @@
// //
// $Id: Sprite.java,v 1.14 2001/08/21 19:40:30 mdb Exp $ // $Id: Sprite.java,v 1.15 2001/08/21 21:18:42 mdb Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
import java.awt.*; import java.awt.*;
import java.util.Enumeration; import java.util.Iterator;
import com.threerings.media.Log; import com.threerings.media.Log;
import com.threerings.media.util.MathUtil; import com.threerings.media.util.MathUtil;
@@ -16,12 +16,6 @@ import com.threerings.media.util.MathUtil;
*/ */
public class Sprite public class Sprite
{ {
/** The sprite's x-position in pixel coordinates. */
public int x;
/** The sprite's y-position in pixel coordinates. */
public int y;
/** /**
* Construct a sprite object. * Construct a sprite object.
* *
@@ -47,6 +41,22 @@ public class Sprite
init(x, y, null); init(x, y, null);
} }
/**
* Returns the sprite's x position in screen coordinates.
*/
public int getX ()
{
return _x;
}
/**
* Returns the sprite's y position in screen coordinates.
*/
public int getY ()
{
return _y;
}
/** /**
* Moves the sprite to the specified location. The location specified * Moves the sprite to the specified location. The location specified
* will be used as the center of the bottom edge of the sprite. * will be used as the center of the bottom edge of the sprite.
@@ -56,8 +66,8 @@ public class Sprite
// invalidate our current position // invalidate our current position
invalidate(); invalidate();
// move ourselves // move ourselves
this.x = x; _x = x;
this.y = y; _y = y;
// we need to update our draw position which is based on the size // we need to update our draw position which is based on the size
// of our current frame // of our current frame
updateDrawPosition(); updateDrawPosition();
@@ -70,8 +80,8 @@ public class Sprite
*/ */
protected void init (int x, int y, MultiFrameImage frames) protected void init (int x, int y, MultiFrameImage frames)
{ {
this.x = x; _x = x;
this.y = y; _y = y;
updateDrawPosition(); updateDrawPosition();
@@ -137,7 +147,7 @@ public class Sprite
*/ */
public boolean inside (Polygon bounds) public boolean inside (Polygon bounds)
{ {
return bounds.contains(this.x, this.y); return bounds.contains(_x, _y);
} }
/** /**
@@ -216,7 +226,7 @@ public class Sprite
// skip the first node since it's our starting position. // skip the first node since it's our starting position.
// perhaps someday we'll do something with this. // perhaps someday we'll do something with this.
_path.nextElement(); _path.next();
// start our meandering // start our meandering
moveAlongPath(); moveAlongPath();
@@ -227,29 +237,29 @@ public class Sprite
*/ */
protected void moveAlongPath () protected void moveAlongPath ()
{ {
// grab the next node in our path
_dest = (PathNode)_path.nextElement();
// if no more nodes remain, clear out our path and bail // if no more nodes remain, clear out our path and bail
if (_dest == null) { if (!_path.hasNext()) {
stop(); stop();
return; return;
} }
// grab the next node in our path
_dest = (PathNode)_path.next();
// if we're already here, move on to the next node // if we're already here, move on to the next node
if (x == _dest.loc.x && y == _dest.loc.y) { if (_x == _dest.loc.x && _y == _dest.loc.y) {
moveAlongPath(); moveAlongPath();
return; return;
} }
// determine the horizontal/vertical move increments // determine the horizontal/vertical move increments
float dist = MathUtil.distance(x, y, _dest.loc.x, _dest.loc.y); float dist = MathUtil.distance(_x, _y, _dest.loc.x, _dest.loc.y);
_incx = (float)(_dest.loc.x - x) / (dist / _vel.x); _incx = (float)(_dest.loc.x - _x) / (dist / _vel.x);
_incy = (float)(_dest.loc.y - y) / (dist / _vel.y); _incy = (float)(_dest.loc.y - _y) / (dist / _vel.y);
// init position data used to track fractional pixels // init position data used to track fractional pixels
_movex = x; _movex = _x;
_movey = y; _movey = _y;
} }
/** /**
@@ -303,16 +313,18 @@ public class Sprite
invalidate(); invalidate();
// move the sprite incrementally toward its goal // move the sprite incrementally toward its goal
x = (int)(_movex += _incx); _x = (int)(_movex += _incx);
y = (int)(_movey += _incy); _y = (int)(_movey += _incy);
// stop moving once we've reached our destination // stop moving once we've reached our destination
if (_incx > 0 && x > _dest.loc.x || _incx < 0 && x < _dest.loc.x || if (_incx > 0 && _x > _dest.loc.x ||
_incy > 0 && y > _dest.loc.y || _incy < 0 && y < _dest.loc.y) { _incx < 0 && _x < _dest.loc.x ||
_incy > 0 && _y > _dest.loc.y ||
_incy < 0 && _y < _dest.loc.y) {
// make sure we stop exactly where desired // make sure we stop exactly where desired
x = _dest.loc.x; _x = _dest.loc.x;
y = _dest.loc.y; _y = _dest.loc.y;
// move further along the path if necessary // move further along the path if necessary
moveAlongPath(); moveAlongPath();
@@ -332,12 +344,12 @@ public class Sprite
protected void updateDrawPosition () protected void updateDrawPosition ()
{ {
if (_frame == null) { if (_frame == null) {
_drawx = x; _drawx = _x;
_drawy = y; _drawy = _y;
} else { } else {
_drawx = x - (_frame.getWidth(null) / 2); _drawx = _x - (_frame.getWidth(null) / 2);
_drawy = y - _frame.getHeight(null); _drawy = _y - _frame.getHeight(null);
} }
} }
@@ -347,8 +359,8 @@ public class Sprite
public String toString () public String toString ()
{ {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
buf.append("[x=").append(x); buf.append("[x=").append(_x);
buf.append(", y=").append(y); buf.append(", y=").append(_y);
buf.append(", fidx=").append(_frameIdx); buf.append(", fidx=").append(_frameIdx);
return buf.append("]").toString(); return buf.append("]").toString();
} }
@@ -365,11 +377,14 @@ public class Sprite
/** The current frame index to render. */ /** The current frame index to render. */
protected int _frameIdx; protected int _frameIdx;
/** The location of the sprite in pixel coordinates. */
protected int _x, _y;
/** The coordinates at which the frame image is drawn. */ /** The coordinates at which the frame image is drawn. */
protected int _drawx, _drawy; protected int _drawx, _drawy;
/** The PathNode objects describing the path the sprite is following. */ /** The PathNode objects describing the path the sprite is following. */
protected Enumeration _path; protected Iterator _path;
/** When moving, the sprite's destination path node. */ /** When moving, the sprite's destination path node. */
protected PathNode _dest; protected PathNode _dest;
@@ -1,10 +1,12 @@
// //
// $Id: LineSegmentPath.java,v 1.6 2001/08/14 22:54:45 mdb Exp $ // $Id: LineSegmentPath.java,v 1.7 2001/08/21 21:18:42 mdb Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Iterator;
import com.samskivert.util.StringUtil;
/** /**
* The <code>Path</code> class represents the path a sprite follows * The <code>Path</code> class represents the path a sprite follows
@@ -89,9 +91,9 @@ public class Path
/** /**
* Return an enumeration of the PathNode objects in this path. * Return an enumeration of the PathNode objects in this path.
*/ */
public Enumeration elements () public Iterator elements ()
{ {
return new Enumerator(_nodes); return _nodes.iterator();
} }
/** /**
@@ -102,29 +104,9 @@ public class Path
return _nodes.size(); return _nodes.size();
} }
/** public String toString ()
* Internal class that provides enumeration functionality for the path.
*/
class Enumerator implements Enumeration
{ {
public Enumerator (ArrayList nodes) return StringUtil.toString(_nodes.iterator());
{
_nodes = nodes;
_idx = 0;
}
public boolean hasMoreElements()
{
return (_idx < _nodes.size());
}
public Object nextElement ()
{
return (_idx >= _nodes.size()) ? null : _nodes.get(_idx++);
}
protected ArrayList _nodes;
protected int _idx;
} }
/** The nodes that make up the path. */ /** The nodes that make up the path. */
@@ -1,5 +1,5 @@
// //
// $Id: IsoSceneView.java,v 1.50 2001/08/16 23:14:21 mdb Exp $ // $Id: IsoSceneView.java,v 1.51 2001/08/21 21:18:42 mdb Exp $
package com.threerings.miso.scene; package com.threerings.miso.scene;
@@ -582,7 +582,7 @@ public class IsoSceneView implements EditableSceneView
// calculate tile coordinates for start and end position // calculate tile coordinates for start and end position
Point stpos = new Point(); Point stpos = new Point();
IsoUtil.screenToTile(_model, sprite.x, sprite.y, stpos); IsoUtil.screenToTile(_model, sprite.getX(), sprite.getY(), stpos);
int tbx = IsoUtil.fullToTile(fpos.x); int tbx = IsoUtil.fullToTile(fpos.x);
int tby = IsoUtil.fullToTile(fpos.y); int tby = IsoUtil.fullToTile(fpos.y);
@@ -599,7 +599,7 @@ public class IsoSceneView implements EditableSceneView
// to second tile, and penultimate to ultimate tile. // to second tile, and penultimate to ultimate tile.
// construct path with starting screen position // construct path with starting screen position
Path path = new Path(sprite.x, sprite.y); Path path = new Path(sprite.getX(), sprite.getY());
// add all nodes on the calculated path // add all nodes on the calculated path
Point nspos = new Point(); Point nspos = new Point();
@@ -633,7 +633,7 @@ public class IsoSceneView implements EditableSceneView
if (prev == stpos) { if (prev == stpos) {
// if our destination is within our origination tile, // if our destination is within our origination tile,
// direction is based on fine coordinates // direction is based on fine coordinates
dir = IsoUtil.getDirection(_model, sprite.x, sprite.y, dir = IsoUtil.getDirection(_model, sprite.getX(), sprite.getY(),
spos.x, spos.y); spos.x, spos.y);
} else { } else {
// else it's based on the last tile we traversed // else it's based on the last tile we traversed