diff --git a/src/java/com/threerings/media/util/LineSegmentPath.java b/src/java/com/threerings/media/util/LineSegmentPath.java index e8913e5ce..57fcf5520 100644 --- a/src/java/com/threerings/media/util/LineSegmentPath.java +++ b/src/java/com/threerings/media/util/LineSegmentPath.java @@ -1,5 +1,5 @@ // -// $Id: LineSegmentPath.java,v 1.15 2002/03/16 03:12:12 shaper Exp $ +// $Id: LineSegmentPath.java,v 1.16 2002/04/15 23:09:10 mdb Exp $ package com.threerings.media.sprite; @@ -15,6 +15,7 @@ import com.samskivert.util.StringUtil; import com.threerings.util.DirectionCodes; +import com.threerings.media.Log; import com.threerings.media.util.MathUtil; /** @@ -38,14 +39,18 @@ public class LineSegmentPath /** * Constructs a line segment path that consists of a single segment * connecting the point (x1, y1) with (x2, - * y2). The orientation for both nodes in the path is - * NONE. + * y2). The orientation for the first node is set arbitrarily + * and the second node is oriented based on the vector between the two + * nodes. */ public LineSegmentPath (int x1, int y1, int x2, int y2) { _nodes = new ArrayList(); - _nodes.add(new PathNode(x1, y1, NONE)); - _nodes.add(new PathNode(x2, y2, NONE)); + _nodes.add(new PathNode(x1, y1, NORTH)); + Point p1 = new Point(x1, y1), p2 = new Point(x2, y2); + int dir = getDirection(p1, p2); + Log.info("orient " + dir + " " + p1 + " " + p2 + "."); + _nodes.add(new PathNode(x2, y2, dir)); } /** @@ -89,6 +94,52 @@ public class LineSegmentPath _vel = velocity; } + /** + * Computes the velocity at which the sprite will need to travel along + * this path such that it will arrive at the destination in + * approximately the specified number of milliseconds. Efforts are + * taken to get the sprite there as close to the desired time as + * possible, but framerate variation may prevent it from arriving + * exactly on time. + */ + public void setDuration (long millis) + { + // if we have only zero or one nodes, we don't have enough + // information to compute our velocity + int ncount = _nodes.size(); + if (ncount < 2) { + Log.warning("Requested to set duration of bogus path " + + "[path=" + this + ", duration=" + millis + "]."); + return; + } + + // compute the total distance along our path + float distance = 0; + PathNode start = (PathNode)_nodes.get(0); + for (int ii = 1; ii < ncount; ii++) { + PathNode end = (PathNode)_nodes.get(ii); + distance += MathUtil.distance( + start.loc.x, start.loc.y, end.loc.x, end.loc.y); + start = end; + } + + Log.info("Set velocity [millis=" + millis + ", dist=" + distance + + ", path=" + this + "]."); + // set the velocity accordingly + setVelocity(distance/millis); + } + + // documentation inherited from interface + public void viewWillScroll (int dx, int dy) + { + // adjust the coordinates of our path nodes + int ncount = _nodes.size(); + for (int ii = 0; ii < ncount; ii++) { + PathNode node = (PathNode)_nodes.get(ii); + node.loc.translate(dx, dy); + } + } + // documentation inherited public void init (Sprite sprite, long timestamp) { @@ -261,21 +312,26 @@ public class LineSegmentPath { if (a.x == b.x && a.y > b.y) { return NORTH; - } else if (a.x < b.x && a.y > b.y) { - return NORTHEAST; - } else if (a.x > b.x && a.y == b.y) { - return EAST; - } else if (a.x > b.x && a.y < b.y) { - return SOUTHEAST; } else if (a.x == b.x && a.y < b.y) { return SOUTH; + + } else if (a.x < b.x && a.y > b.y) { + return NORTHEAST; + } else if (a.x < b.x && a.y == b.y) { + return EAST; + } else if (a.x < b.x && a.y < b.y) { + return SOUTHEAST; + } else if (a.x > b.x && a.y < b.y) { return SOUTHWEST; } else if (a.x > b.x && a.y == b.y) { return WEST; } else if (a.x > b.x && a.y > b.y) { return NORTHWEST; + } else { + Log.warning("Can't sort out direction between " + a + + " and " + b + "."); return NONE; } } diff --git a/src/java/com/threerings/media/util/Path.java b/src/java/com/threerings/media/util/Path.java index 9061b3a8b..c7597b22a 100644 --- a/src/java/com/threerings/media/util/Path.java +++ b/src/java/com/threerings/media/util/Path.java @@ -1,5 +1,5 @@ // -// $Id: Path.java,v 1.2 2001/12/16 08:05:46 mdb Exp $ +// $Id: Path.java,v 1.3 2002/04/15 23:09:10 mdb Exp $ package com.threerings.media.sprite; @@ -50,6 +50,13 @@ public interface Path */ public void setVelocity (float velocity); + /** + * Called when the view that contains the sprite following this path + * is scrolling by the specified amount. Gives the path an opportunity + * to adjust its internal coordinates by the scrolled amount. + */ + public void viewWillScroll (int dx, int dy); + /** * Paint this path on the screen (used for debugging purposes only). */