/************************************************************************************
MRailSim - a model railway simulation program - http://mrailsim.sourceforge.net/
Copyright (C) 2004,2007 Bernd Arnold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
************************************************************************************/
package net.sf.mrailsim.trains;
import net.sf.mrailsim.rails.Node;
import net.sf.mrailsim.rails.Position;
import net.sf.mrailsim.rails.Track;
import net.sf.mrailsim.rails.TrackPosition;
public class TrainTermination {
/**
* Holds the current track, the node this termination is looking towards to
* and the distance how far the node is away.
* @see TrackPosition
*/
private TrackPosition position;
/**
* Holds to which train this TrainTermination belongs
* @see Train
*/
private final Train myTrain;
/**
* Class constructor; sets the train it belongs to
* @param myTrain the train this termination belongs to
* @see Train
*/
TrainTermination( Train myTrain ) {
this.myTrain = myTrain;
}
public TrackPosition getPosition() {
return position;
}
public void setPosition( TrackPosition position ) {
this.position = position;
}
/**
* Moves the termination away from its node, so the distance of the termination to
* the node increases.
* @param distanceMoving the amount of distance moving
* @throws Exception when <code>distanceMoving < 0</code>
* @see Position
*/
public void moveBack( int distanceMoving ) throws Exception {
if ( distanceMoving < 0 ) {
throw new Exception( "moveBack(int), train #" + myTrain.getId() + ", distance cannot be negative: " + distanceMoving );
}
position.increaseDistance( distanceMoving );
while ( position.getDistance() > position.getTrack().getLength() ) {
Track nextTrack = null;
Node nextNode = null;
Track oldTrack = position.getTrack();
Node oldNode = position.getNode();
try {
nextNode = position.getTrack().getOutgoingNode( position.getNode() );
nextTrack = nextNode.getOtherTrack( position.getTrack() );
} catch (Exception e) {
e.printStackTrace();
System.exit( 2 );
}
position.reduceDistance( nextTrack.getLength() );
position.setTrack( nextTrack );
position.setNode( nextNode );
myTrain.trackChanged( this, oldTrack, nextTrack, oldNode );
}
}
/**
* Moves the termination towards its node, so the distance of the termination to
* the node decreases.
* @param distanceMoving the amount of distance moving
* @throws Exception when <code>distanceMoving < 0</code>
* @see Position
*/
public void moveAhead( int distanceMoving ) throws Exception {
if ( distanceMoving < 0 ) {
throw new Exception( "moveAhead(int), train #" + myTrain.getId() + ", distance cannot be negative: " + distanceMoving );
}
position.reduceDistance( distanceMoving );
while ( position.getDistance() < 0 ) {
Track nextTrack = null;
Node nextNode = null;
Track oldTrack = position.getTrack();
Node oldNode = position.getNode();
try {
nextTrack = oldNode.getOtherTrack( position.getTrack() );
nextNode = nextTrack.getOutgoingNode( position.getNode() );
} catch (Exception e) {
e.printStackTrace();
System.exit( 2 );
}
position.increaseDistance( nextTrack.getLength() );
position.setTrack( nextTrack );
position.setNode( nextNode );
myTrain.trackChanged( this, oldTrack, nextTrack, oldNode );
}
}
}