OpenBve.TrainManager.UpdateTrainDoors C# (CSharp) Method

UpdateTrainDoors() private static method

Is called once a frame, to update the door states of the specified train
private static UpdateTrainDoors ( Train Train, double TimeElapsed ) : void
Train Train The train
TimeElapsed double The frame time elapsed
return void
		private static void UpdateTrainDoors(Train Train, double TimeElapsed)
		{
			OpenBveApi.Runtime.DoorStates oldState = OpenBveApi.Runtime.DoorStates.None;
			OpenBveApi.Runtime.DoorStates newState = OpenBveApi.Runtime.DoorStates.None;
			for (int i = 0; i < Train.Cars.Length; i++)
			{
				bool ld = Train.Cars[i].Specs.AnticipatedLeftDoorsOpened;
				bool rd = Train.Cars[i].Specs.AnticipatedRightDoorsOpened;
				double os = Train.Cars[i].Specs.DoorOpenFrequency;
				double cs = Train.Cars[i].Specs.DoorCloseFrequency;
				for (int j = 0; j < Train.Cars[i].Specs.Doors.Length; j++)
				{
					if (Train.Cars[i].Specs.Doors[j].Direction == -1 | Train.Cars[i].Specs.Doors[j].Direction == 1)
					{
						bool shouldBeOpen = Train.Cars[i].Specs.Doors[j].Direction == -1 ? ld : rd;
						if (Train.Cars[i].Specs.Doors[j].State > 0.0)
						{
							if (Train.Cars[i].Specs.Doors[j].Direction == -1)
							{
								oldState |= OpenBveApi.Runtime.DoorStates.Left;
							}
							else
							{
								oldState |= OpenBveApi.Runtime.DoorStates.Right;
							}
						}
						if (shouldBeOpen)
						{
							// open
							Train.Cars[i].Specs.Doors[j].State += os * TimeElapsed;
							if (Train.Cars[i].Specs.Doors[j].State > 1.0)
							{
								Train.Cars[i].Specs.Doors[j].State = 1.0;
							}
						}
						else
						{
							// close
							if (Train.Cars[i].Specs.Doors[j].DoorLockDuration > 0.0)
							{
								if (Train.Cars[i].Specs.Doors[j].State > Train.Cars[i].Specs.Doors[j].DoorLockState)
								{
									Train.Cars[i].Specs.Doors[j].State -= cs * TimeElapsed;
								}
								if (Train.Cars[i].Specs.Doors[j].State < Train.Cars[i].Specs.Doors[j].DoorLockState)
								{
									Train.Cars[i].Specs.Doors[j].State = Train.Cars[i].Specs.Doors[j].DoorLockState;
								}
								Train.Cars[i].Specs.Doors[j].DoorLockDuration -= TimeElapsed;
								if (Train.Cars[i].Specs.Doors[j].DoorLockDuration < 0.0)
								{
									Train.Cars[i].Specs.Doors[j].DoorLockDuration = 0.0;
								}
							}
							else
							{
								Train.Cars[i].Specs.Doors[j].State -= cs * TimeElapsed;
							}
							if (Train.Cars[i].Specs.Doors[j].State < 0.0)
							{
								Train.Cars[i].Specs.Doors[j].State = 0.0;
							}
						}
						if (Train.Cars[i].Specs.Doors[j].State > 0.0)
						{
							if (Train.Cars[i].Specs.Doors[j].Direction == -1)
							{
								newState |= OpenBveApi.Runtime.DoorStates.Left;
							}
							else
							{
								newState |= OpenBveApi.Runtime.DoorStates.Right;
							}
						}
					}
				}
			}

			if (oldState != OpenBveApi.Runtime.DoorStates.None & newState == OpenBveApi.Runtime.DoorStates.None)
			{
				Sounds.SoundBuffer buffer = Train.Cars[Train.DriverCar].Sounds.PilotLampOn.Buffer;
				if (buffer != null)
				{
					OpenBveApi.Math.Vector3 pos = Train.Cars[Train.DriverCar].Sounds.PilotLampOn.Position;
					Sounds.PlaySound(buffer, 1.0, 1.0, pos, Train, Train.DriverCar, false);
				}
			}
			else if (oldState == OpenBveApi.Runtime.DoorStates.None & newState != OpenBveApi.Runtime.DoorStates.None)
			{
				Sounds.SoundBuffer buffer = Train.Cars[Train.DriverCar].Sounds.PilotLampOff.Buffer;
				if (buffer != null)
				{
					OpenBveApi.Math.Vector3 pos = Train.Cars[Train.DriverCar].Sounds.PilotLampOff.Position;
					Sounds.PlaySound(buffer, 1.0, 1.0, pos, Train, Train.DriverCar, false);
				}
			}
			if (oldState != newState)
			{
				if (Train.Plugin != null)
				{
					Train.Plugin.DoorChange(oldState, newState);
				}
			}
		}