1 using System;
2 using SoundManager;
3 using TrainManager.Trains;
4 
5 namespace TrainManager.Handles
6 {
7 	/// <summary>The basic abstract handle</summary>
8 	public abstract class AbstractHandle
9 	{
10 		/// <summary>The notch set by the driver</summary>
11 		public int Driver;
12 
13 		/// <summary>The notch set by the safety sytem</summary>
14 		public int Safety;
15 
16 		/// <summary>The actual notch, as used by the physics system etc.</summary>
17 		public int Actual;
18 
19 		/// <summary>The maximum notch this handle may be advanced to</summary>
20 		public int MaximumNotch;
21 
22 		/// <summary>The maximum notch the driver may advance this handle to</summary>
23 		public int MaximumDriverNotch;
24 
25 		public HandleChange[] DelayedChanges;
26 
27 		/// <summary>Whether the current handle motion is a continuous motion</summary>
28 		public bool ContinuousMovement;
29 
30 		/// <summary>The sound played when the handle position is increased</summary>
31 		public CarSound Increase;
32 
33 		/// <summary>The sound played when the handle position is increased in a fast motion</summary>
34 		public CarSound IncreaseFast;
35 
36 		/// <summary>The sound played when the handle position is decreased</summary>
37 		public CarSound Decrease;
38 
39 		/// <summary>The sound played when the handle position is decreased in a fast motion</summary>
40 		public CarSound DecreaseFast;
41 
42 		/// <summary>The sound played when the handle is moved to the minimum position</summary>
43 		public CarSound Min;
44 
45 		/// <summary>The sound played when the handles is moved to the maximum position</summary>
46 		public CarSound Max;
47 
48 		/// <summary>Contains the notch descriptions to be displayed on the in-game UI</summary>
49 		public string[] NotchDescriptions;
50 
51 		/// <summary>The max width used in px for the description string</summary>
52 		public double MaxWidth = 48;
53 
54 		internal readonly TrainBase baseTrain;
55 
Update()56 		public abstract void Update();
57 
ApplyState(int newState, bool relativeChange, bool isOverMaxDriverNotch = false)58 		public virtual void ApplyState(int newState, bool relativeChange, bool isOverMaxDriverNotch = false)
59 		{
60 
61 		}
62 
ApplyState(AirBrakeHandleState newState)63 		public virtual void ApplyState(AirBrakeHandleState newState)
64 		{
65 
66 		}
67 
AbstractHandle(TrainBase Train)68 		protected AbstractHandle(TrainBase Train)
69 		{
70 			baseTrain = Train;
71 			Increase = new CarSound();
72 			IncreaseFast = new CarSound();
73 			Decrease = new CarSound();
74 			DecreaseFast = new CarSound();
75 			Min = new CarSound();
76 			Max = new CarSound();
77 		}
78 	}
79 
80 	/// <summary>Represents an abstract handle with a set number of notches</summary>
81 	public abstract class NotchedHandle : AbstractHandle
82 	{
83 
84 		/// <summary>The number of steps this handle must be reduced by for a change to take effect</summary>
85 		public int ReduceSteps;
86 
87 		/// <summary>The list of delay values for each notch increase</summary>
88 		internal double[] DelayUp;
89 
90 		/// <summary>The list of delay values for each notch decrease</summary>
91 		internal double[] DelayDown;
92 
NotchedHandle(TrainBase train)93 		internal NotchedHandle(TrainBase train) : base(train)
94 		{
95 
96 		}
97 
98 		/// <summary>Adds a delayed handle state change</summary>
99 		/// <param name="Value">The value to add or subtract</param>
100 		/// <param name="Delay">The delay in seconds</param>
AddChange(int Value, double Delay)101 		internal void AddChange(int Value, double Delay)
102 		{
103 			int n = DelayedChanges.Length;
104 			Array.Resize(ref DelayedChanges, n + 1);
105 			DelayedChanges[n].Value = Value;
106 			DelayedChanges[n].Time = TrainManagerBase.currentHost.InGameTime + Delay;
107 		}
108 
109 		/// <summary>Removes a specified number of delayed changes</summary>
110 		/// <param name="Count">The number of changes to remove</param>
RemoveChanges(int Count)111 		internal void RemoveChanges(int Count)
112 		{
113 			int n = DelayedChanges.Length;
114 			for (int i = 0; i < n - Count; i++)
115 			{
116 				DelayedChanges[i] = DelayedChanges[i + Count];
117 			}
118 
119 			Array.Resize(ref DelayedChanges, n - Count);
120 		}
121 
122 
123 		/// <summary>Gets the delay value for this handle</summary>
124 		/// <param name="ShouldIncrease">Whether this is an increase or a decrease</param>
125 		/// <returns>The delay value to apply</returns>
GetDelay(bool ShouldIncrease)126 		internal double GetDelay(bool ShouldIncrease)
127 		{
128 			if (ShouldIncrease)
129 			{
130 				if (DelayUp == null || DelayUp.Length == 0)
131 				{
132 					return 0.0;
133 				}
134 
135 				if (Actual < DelayUp.Length)
136 				{
137 					return DelayUp[Actual];
138 				}
139 
140 				return DelayUp[DelayUp.Length - 1];
141 			}
142 
143 			if (DelayDown == null || DelayDown.Length == 0)
144 			{
145 				return 0.0;
146 			}
147 
148 			if (Actual < DelayDown.Length)
149 			{
150 				return DelayDown[Actual];
151 			}
152 
153 			return DelayDown[DelayDown.Length - 1];
154 		}
155 
156 	}
157 }
158