jfreerails.world.train
Class TrainMotion

java.lang.Object
  extended by jfreerails.world.train.TrainMotion
All Implemented Interfaces:
java.io.Serializable, Activity<TrainPositionOnMap>, FreerailsSerializable

public class TrainMotion
extends java.lang.Object
implements Activity<TrainPositionOnMap>

This immutable class provides methods that return a train's position and speed at any time within an interval. An instance of this class will be stored on the world object for each train rather the trainís position. The reasons for this are as follows.

  1. It decouples the number of game updates per second and number of frames per second shown by the client. If the trainís position were stored on the world object, it would get updated each game tick. But this would mean that if the game was being updated 10 times per second, even if the client was displaying 50 FPS, the trainís motion would still appear jerky since its position would only change 10 times per second.
  2. It makes supporting low bandwidth networks easier since it allows the server to send updates less frequently.
  3. Author:
    Luke
    See Also:
    PathOnTiles, CompositeSpeedAgainstTime, Serialized Form

    Field Summary
    private  SpeedTimeAndStatus.TrainActivity activity
               
    private  double distanceEngineWillTravel
               
    private  double duration
               
    private  double initialPosition
               
    private  PathOnTiles path
               
    private static long serialVersionUID
               
    private  SpeedAgainstTime speeds
               
    private  int trainLength
               
     
    Constructor Summary
    TrainMotion(PathOnTiles path, int trainLength, double duration, SpeedTimeAndStatus.TrainActivity act)
               
    TrainMotion(PathOnTiles path, int engineStep, int trainLength, SpeedAgainstTime speeds)
              Creates a new TrainMotion instance.
     
    Method Summary
    private  double calcOffSet(double t)
               
    (package private)  void checkT(double t)
               
     double duration()
               
     boolean equals(java.lang.Object o)
               
     SpeedTimeAndStatus.TrainActivity getActivity()
               
     double getDistance(double t)
              Returns the train's distance along the track from the point the train was at at time getStart() at the specified time.
     PositionOnTrack getFinalPosition()
               
     double getInitialPosition()
               
     PathOnTiles getPath()
               
     double getSpeedAtEnd()
               
     TrainPositionOnMap getState(double t)
              Returns the train's position at the specified time.
     PathOnTiles getTiles(double t)
              Returns a PathOnTiles object that identifies the tiles the train is on at the specified time.
     int getTrainLength()
               
     int hashCode()
               
    private  void sanityCheck()
              Checks we are not creating an object with an inconsistent state.
     
    Methods inherited from class java.lang.Object
    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    serialVersionUID

    private static final long serialVersionUID
    See Also:
    Constant Field Values

    duration

    private final double duration

    distanceEngineWillTravel

    private final double distanceEngineWillTravel

    initialPosition

    private final double initialPosition

    path

    private final PathOnTiles path

    speeds

    private final SpeedAgainstTime speeds

    trainLength

    private final int trainLength

    activity

    private final SpeedTimeAndStatus.TrainActivity activity
    Constructor Detail

    TrainMotion

    public TrainMotion(PathOnTiles path,
                       int engineStep,
                       int trainLength,
                       SpeedAgainstTime speeds)
    Creates a new TrainMotion instance.

    Parameters:
    path - the path the train will take.
    engineStep - the position measured in tiles that trains engine is along the path
    trainLength - the length of the train, as returned by TrainModel.getLength().
    Throws:
    java.lang.IllegalArgumentException - if trainLength is out the range trainLength > TrainModel.WAGON_LENGTH || trainLength < TrainModel.MAX_TRAIN_LENGTH
    java.lang.IllegalArgumentException - if path.getDistance(engineStep) < trainLength.
    java.lang.IllegalArgumentException - if (path.getLength() - initialPosition) > speeds.getTotalDistance().

    TrainMotion

    public TrainMotion(PathOnTiles path,
                       int trainLength,
                       double duration,
                       SpeedTimeAndStatus.TrainActivity act)
    Method Detail

    sanityCheck

    private void sanityCheck()
    Checks we are not creating an object with an inconsistent state. That is, at the time stored in the field duration, the engine must not have gone off the end of the path.


    calcOffSet

    private double calcOffSet(double t)

    checkT

    void checkT(double t)

    duration

    public double duration()
    Specified by:
    duration in interface Activity<TrainPositionOnMap>

    equals

    public boolean equals(java.lang.Object o)
    Overrides:
    equals in class java.lang.Object

    getDistance

    public double getDistance(double t)
    Returns the train's distance along the track from the point the train was at at time getStart() at the specified time.

    Parameters:
    t - the time.
    Returns:
    the distance
    Throws:
    java.lang.IllegalArgumentException - if t is outside the interval

    getFinalPosition

    public PositionOnTrack getFinalPosition()

    getSpeedAtEnd

    public double getSpeedAtEnd()

    getState

    public TrainPositionOnMap getState(double t)
    Returns the train's position at the specified time.

    Specified by:
    getState in interface Activity<TrainPositionOnMap>
    Parameters:
    t - the time.
    Returns:
    the train's position.
    Throws:
    java.lang.IllegalArgumentException - if t is outside the interval

    getTiles

    public PathOnTiles getTiles(double t)
    Returns a PathOnTiles object that identifies the tiles the train is on at the specified time.

    Parameters:
    t - the time.
    Returns:
    an array of the tiles the train is on
    Throws:
    java.lang.IllegalArgumentException - if t is outside the interval

    getTrainLength

    public int getTrainLength()

    hashCode

    public int hashCode()
    Overrides:
    hashCode in class java.lang.Object

    getPath

    public PathOnTiles getPath()

    getActivity

    public SpeedTimeAndStatus.TrainActivity getActivity()

    getInitialPosition

    public double getInitialPosition()