Beispiel #1
0
    /// <summary>
    /// Spawns a consist on a track section
    /// </summary>
    /// <param name="startSection"></param>
    /// <returns>The lead car of this consist</returns>
    public TrainController Spawn(TrackSection startSection)
    {
        Driver driverInstance = null;

        if (driver != null)
        {
            driverInstance = GameObject.Instantiate(driver.gameObject).GetComponent <Driver>();
        }

        TrainController controller;
        TrainController previous = null;
        TrainController lead     = null;

        TrackSection currentSection;

        currentSection = startSection;
        // Spawn from the end of the track
        float spawnPosition = currentSection.length - 10.0f;

        // Spawn vehicles front to back
        for (int i = 0; i < vehicles.Length; i++)
        {
            controller = GameObject.Instantiate(vehicles[i].vehicleController.gameObject).GetComponent <TrainController>();
            //TODO: TrackVehicle facing directions works but gets ignored by driving logic, e.g. it's only visual.
            controller.trackVehicle = new TrackVehicle(currentSection, spawnPosition, Direction.Forward, vehicles[i].faceDirection, controller.length, controller.wheelBase);

            if (i == 0)
            {
                lead = controller;
            }

            // Add engines to driver if we have one
            TrainEngineController engine = controller as TrainEngineController;
            if (engine != null && driverInstance != null)
            {
                driverInstance.AddEngine(engine);
            }

            if (previous != null)
            {
                previous.next       = controller;
                controller.previous = previous;

                //Move ourselfs back on the track
                controller.trackVehicle.SetDirection(Direction.Reverse);
                controller.trackVehicle.Move(controller.length * 0.5f + previous.length * 0.5f);
                controller.trackVehicle.SetDirection(Direction.Forward);
                //Change spawn position to our new location
                spawnPosition -= controller.length * 0.5f + previous.length * 0.5f;
            }
            previous = controller;
        }

        if (driverInstance != null)
        {
            driverInstance.RecalculateTrain();
        }

        return(lead);
    }
    /// <summary>
    /// Handle train physics update
    ///
    /// </summary>
    public void FixedUpdate()
    {
        //Call if we are the first in the train
        if (previous == null)
        {
            if (isDerailed)
            {
                var r = GetComponent <Rigidbody>();
                if (r != null)
                {
                    _velocity = r.velocity.magnitude;
                }

                return;
            }

            float trainBrakeForce  = 0;
            float trainMass        = 0;
            float trainEngineForce = 0;
            float trainDragForce   = 0;

            TrainController trailer = this;
            while (trailer != null)
            {
                trainBrakeForce += trailer.GetBrakeForce();
                trainMass       += trailer.GetMass();
                trainDragForce  += trailer.GetDragForce();

                TrainEngineController engine = trailer as TrainEngineController;
                if (engine != null)
                {
                    trainEngineForce += engine.GetTractiveForce();
                }

                trailer = trailer.next;
            }

            //Debug.Log("Train Mass " + trainMass + " kg");
            // Add engine effort
            Velocity += (trainEngineForce / trainMass) * Time.fixedDeltaTime;

            // Calculate and subtract drag and braking
            float brakeValue = -(float)trackVehicle.direction * (trainBrakeForce / trainMass) * Time.fixedDeltaTime;

            trainDragForce *= -(float)trackVehicle.direction;

            // Combine drag and brake into single value
            float dragValue = brakeValue + (trainDragForce / trainMass) * Time.fixedDeltaTime;

            // Stop train from reversing due to brake force...
            if (trackVehicle.direction == Direction.Forward && Velocity + dragValue < 0.0f)
            {
                Velocity = 0.0f;
            }
            else
            {
                Velocity += dragValue;
            }

            //Set the velocity on the entire train
            TrainController t = next;
            while (t != null)
            {
                t.Velocity = Velocity;
                t          = t.next;
            }

            // Update positions
            OnFixedUpdate();
        }
    }
Beispiel #3
0
 /// <summary>
 /// Removes an engine we are controlling
 /// </summary>
 /// <param name="engine"></param>
 public void RemoveEngine(TrainEngineController engine)
 {
     Engines.Remove(engine);
     RecalculateTrain();
 }
Beispiel #4
0
 /// <summary>
 /// Adds an engine that we can control
 /// </summary>
 /// <param name="engine"></param>
 public void AddEngine(TrainEngineController engine)
 {
     Engines.Add(engine);
     RecalculateTrain();
 }