Package net.sf.mrailsim.trains

Source Code of net.sf.mrailsim.trains.TrainManager

/************************************************************************************

    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 java.awt.Graphics;
import java.util.ArrayList;

import net.sf.mrailsim.main.Game;
import net.sf.mrailsim.rails.Node;
import net.sf.mrailsim.rails.Track;
import net.sf.mrailsim.rails.TrackPosition;

public class TrainManager {

  private ArrayList<Train> trainList;
  private ArrayList<TrainGroup> trainGroupList;
 
 
  /**
   * Class constructor; allocates the lists for the trains
   * and the train groups.
   */
  public TrainManager() {
    trainList = new ArrayList<Train>();
    trainGroupList = new ArrayList<TrainGroup>();
  }
 
  /**
   * Allocates a new train group and assigns a new id. The ids of the already
   * included train groups are iterated to get the highest already assigned id.
   * The new train group is assigned an id one greater than the highest id.
   * @return  the new train group
   */
  TrainGroup allocateNewTrainGroup() {
    long lastId = 0;
   
    for ( TrainGroup item : trainGroupList ) {
      if ( item.getId() > lastId ) {
        lastId = item.getId();
      }
    }
   
    lastId++;
    TrainGroup tg = new TrainGroup( lastId );
    trainGroupList.add( tg );
   
    return tg;
  }
 
  /**
   * Adds a train to the train list
   * @param t  train to add
   */
  public void add( Train t ) {
    trainList.add( t );
  }
 
  /**
   * Puts the train onto the specified track, with the highest possible distance
   * away from the given node id. The train mustn't be in a train group already.
   * The length of the train mustn't be greater than the length of the specified
   * track.
   * @param train         train to put on
   * @param trackId       id of the track where the train should be put on
   * @param nodeIdAtBack  node id the back of the train is looking at
   */
  // TODO: change nodeIdAtBack by nodeItAtFront - where the front looks at
  public void putTrain( Train train, long trackId, long nodeIdAtBack ) {
    Track track = Game.trackManager.getTrack( trackId );
    Node node = Game.nodeManager.getNode( nodeIdAtBack );
   
    try {
      // TODO: remove this in final release and handle correct
      if ( train.getLength() > track.getLength() ) {
        throw new Exception( "train too long" );
      }
      // TODO: remove this in final release and handle correct
      if ( train.getTrainGroup() != null ) {
        throw new Exception( "train already in traingroup" );
      }
    } catch( Exception e ) {
      e.printStackTrace();
      System.exit( 2 );
    }
   
    TrainGroup trainGroup = allocateNewTrainGroup();
   
    trainGroup.add( train );
   
    Node headsNode = null;
    try {
      headsNode = track.getOutgoingNode( node );
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit( 2 );
    }
   
    train.setPositions(
        new TrackPosition( track, headsNode, 0 ),
        new TrackPosition( track, node, track.getLength() - train.getLength() ) );

    track.trainEnters( train, null );
  }
 
  /**
   * Returns the train object with the given id
   * @param id  the id of the train
   * @return    the train object or <code>null</code>, if not found
   */
  public Train getTrain( long id ) {
    for ( Train item : trainList ) {
      if ( item.getId() == id ) {
        return item;
      }
    }
    return null;
  }

  /**
   * Draw all trains into the given graphic context.
   * @param g  The graphic context.
   */
  public void draw( Graphics g ) {
    for ( Train item : trainList ) {
      item.draw( g );
    }
  }

  public void moveTrains() {
    for ( TrainGroup tg : trainGroupList ) {
      tg.move( tg.getCurrentSpeed() );
    }
  }
}
TOP

Related Classes of net.sf.mrailsim.trains.TrainManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.