1 using System;
2 
3 namespace TrainManager.Trains
4 {
5 	/// <summary>The passenger loading of a train at any given point in time</summary>
6 	public struct TrainPassengers
7 	{
8 		/// <summary>The current passenger ratio:
9 		/// Must be a number between 0 and 250, where 100 represents a nominally loaded train</summary>
10 		public double PassengerRatio;
11 
12 		/// <summary>The current acceleration as being felt by the passengers</summary>
13 		private double CurrentAcceleration;
14 
15 		/// <summary>The speed difference from that of the last frame</summary>
16 		private double CurrentSpeedDifference;
17 
18 		/// <summary>Whether the passengers have fallen over (Used for scoring purposes)</summary>
19 		public bool FallenOver;
20 
21 		/// <summary>Called once a frame to update the status of the passengers</summary>
22 		/// <param name="Acceleration">The current average acceleration of the train</param>
23 		/// <param name="TimeElapsed">The frame time elapsed</param>
UpdateTrainManager.Trains.TrainPassengers24 		public void Update(double Acceleration, double TimeElapsed)
25 		{
26 			double accelerationDifference = Acceleration - CurrentAcceleration;
27 			double jerk = 0.25 + 0.10 * Math.Abs(accelerationDifference);
28 			double accelerationQuanta = jerk * TimeElapsed;
29 			if (Math.Abs(accelerationDifference) < accelerationQuanta)
30 			{
31 				CurrentAcceleration = Acceleration;
32 				accelerationDifference = 0.0;
33 			}
34 			else
35 			{
36 				CurrentAcceleration += Math.Sign(accelerationDifference) * accelerationQuanta;
37 				accelerationDifference = Acceleration - CurrentAcceleration;
38 			}
39 
40 			CurrentSpeedDifference += accelerationDifference * TimeElapsed;
41 			double acceleration = 0.10 + 0.35 * Math.Abs(CurrentSpeedDifference);
42 			double speedQuanta = acceleration * TimeElapsed;
43 			if (Math.Abs(CurrentSpeedDifference) < speedQuanta)
44 			{
45 				CurrentSpeedDifference = 0.0;
46 			}
47 			else
48 			{
49 				CurrentSpeedDifference -= Math.Sign(CurrentSpeedDifference) * speedQuanta;
50 			}
51 
52 			if (PassengerRatio > 0.0)
53 			{
54 				double threshold = 1.0 / PassengerRatio;
55 				FallenOver = Math.Abs(CurrentSpeedDifference) > threshold;
56 			}
57 			else
58 			{
59 				FallenOver = false;
60 			}
61 		}
62 	}
63 }
64