com.threerings.media.util
Class LineSegmentPath

java.lang.Object
  extended by com.threerings.media.util.LineSegmentPath
All Implemented Interfaces:
Path, DirectionCodes
Direct Known Subclasses:
TilePath

public class LineSegmentPath
extends Object
implements DirectionCodes, Path

The line segment path is used to cause a pathable to follow a path that is made up of a sequence of line segments. There must be at least two nodes in any worthwhile path. The direction of the first node in the path is meaningless since the pathable begins at that node and will therefore never be heading towards it.


Field Summary
protected  PathNode _dest
          When moving, the pathable's destination path node.
protected  float _fracx
          The distance to move on the straight path line per tick.
protected  float _fracy
          The distance to move on the straight path line per tick.
protected  float _incx
          When moving, the distance to move on each axis per tick.
protected  float _incy
          When moving, the distance to move on each axis per tick.
protected  float _movex
          When moving, the pathable position including fractional pixels.
protected  float _movey
          When moving, the pathable position including fractional pixels.
protected  Iterator<PathNode> _niter
          We use this when moving along this path.
protected  ArrayList<PathNode> _nodes
          The nodes that make up the path.
protected  long _nodestamp
          The time at which we started traversing the current node.
protected  float _seglength
          The length in pixels of the current path segment.
protected  PathNode _src
          When moving, the pathable's source path node.
protected  float _vel
          The path velocity in pixels per millisecond.
protected static float DEFAULT_VELOCITY
          Default pathable velocity.
 
Fields inherited from interface com.threerings.util.DirectionCodes
CARDINAL_DIRECTIONS, CCW, CW, DIRECTION_COUNT, DOWN, EAST, EASTNORTHEAST, EASTSOUTHEAST, FINE_DIRECTION_COUNT, HORIZONTAL, LEFT, NONE, NORTH, NORTHEAST, NORTHNORTHEAST, NORTHNORTHWEST, NORTHWEST, RIGHT, SOUTH, SOUTHEAST, SOUTHSOUTHEAST, SOUTHSOUTHWEST, SOUTHWEST, UP, VERTICAL, WEST, WESTNORTHWEST, WESTSOUTHWEST
 
Constructor Summary
LineSegmentPath()
          Constructs an empty line segment path.
LineSegmentPath(int x1, int y1, int x2, int y2)
          Constructs a line segment path that consists of a single segment connecting the point (x1, y1) with (x2, y2).
LineSegmentPath(List<Point> points)
          Constructs a line segment path with the specified list of points.
LineSegmentPath(Point p1, Point p2, int dir)
          Construct a line segment path between the two nodes with the specified direction.
 
Method Summary
 void addNode(int x, int y, int dir)
          Add a node to the path with the specified destination point and facing direction.
protected  void createPath(List<Point> points)
          Populate the path with the path nodes that lead the pathable from its starting position to the given destination coordinates following the given list of screen coordinates.
 void fastForward(long timeDelta)
          This is called if the pathable is paused for some length of time and then unpaused.
 int getFinalOrientation()
          Returns the orientation the sprite will face at the end of the path.
protected  PathNode getNextNode()
          Gets the next node in the path.
 PathNode getNode(int idx)
          Return the requested node index in the path, or null if no such index exists.
protected  boolean headToNextNode(Pathable pable, long startstamp, long now)
          Place the pathable moving along the path at the end of the previous path node, face it appropriately for the next node, and start it on its way.
 void init(Pathable pable, long timestamp)
          Called once to let the path prepare itself for the process of animating the supplied pathable.
 void paint(Graphics2D gfx)
          Paint this path on the screen (used for debugging purposes only).
 void setDuration(long millis)
          Computes the velocity at which the pathable will need to travel along this path such that it will arrive at the destination in approximately the specified number of milliseconds.
 void setVelocity(float velocity)
          Sets the velocity of this pathable in pixels per millisecond.
 int size()
          Return the number of nodes in the path.
 boolean tick(Pathable pable, long timestamp)
          Called to request that this path update the position of the specified pathable based on the supplied timestamp information.
 String toString()
           
 void wasRemoved(Pathable pable)
          When a path is removed from a pathable, whether that is because the path was completed or because it was replaced by another path, this method will be called to let the path know that it is no longer associated with this pathable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_nodes

protected ArrayList<PathNode> _nodes
The nodes that make up the path.


_niter

protected Iterator<PathNode> _niter
We use this when moving along this path.


_src

protected PathNode _src
When moving, the pathable's source path node.


_dest

protected PathNode _dest
When moving, the pathable's destination path node.


_nodestamp

protected long _nodestamp
The time at which we started traversing the current node.


_seglength

protected float _seglength
The length in pixels of the current path segment.


_vel

protected float _vel
The path velocity in pixels per millisecond.


_movex

protected float _movex
When moving, the pathable position including fractional pixels.


_movey

protected float _movey
When moving, the pathable position including fractional pixels.


_incx

protected float _incx
When moving, the distance to move on each axis per tick.


_incy

protected float _incy
When moving, the distance to move on each axis per tick.


_fracx

protected float _fracx
The distance to move on the straight path line per tick.


_fracy

protected float _fracy
The distance to move on the straight path line per tick.


DEFAULT_VELOCITY

protected static final float DEFAULT_VELOCITY
Default pathable velocity.

See Also:
Constant Field Values
Constructor Detail

LineSegmentPath

public LineSegmentPath()
Constructs an empty line segment path.


LineSegmentPath

public LineSegmentPath(int x1,
                       int y1,
                       int x2,
                       int y2)
Constructs a line segment path that consists of a single segment connecting the point (x1, y1) with (x2, y2). The orientation for the first node is set arbitrarily and the second node is oriented based on the vector between the two nodes (in top-down coordinates).


LineSegmentPath

public LineSegmentPath(Point p1,
                       Point p2,
                       int dir)
Construct a line segment path between the two nodes with the specified direction.


LineSegmentPath

public LineSegmentPath(List<Point> points)
Constructs a line segment path with the specified list of points. An arbitrary direction will be assigned to the starting node.

Method Detail

getFinalOrientation

public int getFinalOrientation()
Returns the orientation the sprite will face at the end of the path.


addNode

public void addNode(int x,
                    int y,
                    int dir)
Add a node to the path with the specified destination point and facing direction.

Parameters:
x - the x-position.
y - the y-position.
dir - the facing direction.

getNode

public PathNode getNode(int idx)
Return the requested node index in the path, or null if no such index exists.

Parameters:
idx - the node index.
Returns:
the path node.

size

public int size()
Return the number of nodes in the path.


setVelocity

public void setVelocity(float velocity)
Sets the velocity of this pathable in pixels per millisecond. The velocity is measured as pixels traversed along the path that the pathable is traveling rather than in the x or y directions individually. Note that the pathable velocity should not be changed while a path is being traversed; doing so may result in the pathable position changing unexpectedly.

Parameters:
velocity - the pathable velocity in pixels per millisecond.

setDuration

public void setDuration(long millis)
Computes the velocity at which the pathable 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 pathable there as close to the desired time as possible, but framerate variation may prevent it from arriving exactly on time.


init

public void init(Pathable pable,
                 long timestamp)
Description copied from interface: Path
Called once to let the path prepare itself for the process of animating the supplied pathable. Path users should also call Path.tick(com.threerings.media.util.Pathable, long) after Path.init(com.threerings.media.util.Pathable, long) with the same initialization timestamp.

Specified by:
init in interface Path

tick

public boolean tick(Pathable pable,
                    long timestamp)
Description copied from interface: Path
Called to request that this path update the position of the specified pathable based on the supplied timestamp information. A path should record its initial timestamp and determine the progress of the pathable along the path based on the time elapsed since the pathable began down the path.

Specified by:
tick in interface Path
Parameters:
pable - the pathable whose position should be updated.
timestamp - the timestamp associated with this frame.
Returns:
true if the pathable's position was updated, false if the path determined that the pathable should not move at this time.

fastForward

public void fastForward(long timeDelta)
Description copied from interface: Path
This is called if the pathable is paused for some length of time and then unpaused. Paths should adjust any time stamps they are maintaining internally by the delta so that time maintains the illusion of flowing smoothly forward.

Specified by:
fastForward in interface Path

wasRemoved

public void wasRemoved(Pathable pable)
Description copied from interface: Path
When a path is removed from a pathable, whether that is because the path was completed or because it was replaced by another path, this method will be called to let the path know that it is no longer associated with this pathable.

Specified by:
wasRemoved in interface Path

headToNextNode

protected boolean headToNextNode(Pathable pable,
                                 long startstamp,
                                 long now)
Place the pathable moving along the path at the end of the previous path node, face it appropriately for the next node, and start it on its way. Returns whether the pathable position moved.


paint

public void paint(Graphics2D gfx)
Description copied from interface: Path
Paint this path on the screen (used for debugging purposes only).

Specified by:
paint in interface Path

toString

public String toString()
Overrides:
toString in class Object

createPath

protected void createPath(List<Point> points)
Populate the path with the path nodes that lead the pathable from its starting position to the given destination coordinates following the given list of screen coordinates.


getNextNode

protected PathNode getNextNode()
Gets the next node in the path.