openbve-fcmb-train-plugin/src/Devices/DoorClosingSound.cs

198 lines
8.9 KiB
C#

using System;
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A device controlling the door operation.</summary>
internal class DoorClosingSound : Device
{
/// <summary>Whether the door closing sound is required to be played to close the doors.</summary>
private readonly bool RequireDoorClosingSound;
/// <summary>The duration of the door closing sound, in seconds.</summary>
private readonly double DoorClosingSoundDuration;
/// <summary>The timeout after which the door closing sound needs to be played again, in seconds.</summary>
private readonly double DoorClosingSoundTimeout;
/// <summary>The counter for the door closing sound.</summary>
private double DoorClosingSoundCounter;
/// <summary>Whether the AI should trigger the door closing sound.</summary>
private bool AiTriggerDoorClosingSound;
/// <summary>The index of the door closing sound.</summary>
private readonly int DoorClosingSoundIndex;
/// <summary>The current time.</summary>
private Time CurrentTime = new Time(0);
/// <summary>The time when the doors last opened.</summary>
private Time DoorOpenTime = new Time(0);
/// <summary>Whether the door closing sound is required to be played to close the doors.</summary>
/// <param name="requireClosingSound">The delay before the brakes are applied, in seconds.</param>
/// <param name="closingSoundDuration">The duration of the door closing sound, in seconds.</param>
/// <param name="closingSoundTimeout">The timeout after which the door closing sound needs to be played again, in seconds.</param>
/// <param name="closingSoundIndex">The index of the door closing sound.</param>
internal DoorClosingSound(bool requireClosingSound, double closingSoundDuration, double closingSoundTimeout, int closingSoundIndex)
{
RequireDoorClosingSound = requireClosingSound;
DoorClosingSoundDuration = closingSoundDuration;
DoorClosingSoundTimeout = closingSoundTimeout;
DoorClosingSoundIndex = closingSoundIndex;
}
/// <summary>Is called when the device state should be updated.</summary>
/// <param name="train">The current train.</param>
/// <param name="route">The current route.</param>
/// <param name="init">Whether the device should initialize.</param>
/// <param name="elapsedTime">The time elapsed since the previous call.</param>
internal override void Update(Train train, Route route, bool init, Time elapsedTime)
{
// Update current time
CurrentTime = route.CurrentTime;
if (init)
{
// Set door closing counter to value higher than maximum where doors are allowed to close
DoorClosingSoundCounter = DoorClosingSoundDuration + DoorClosingSoundTimeout;
}
// Update door closing sound counter
DoorClosingSoundCounter += elapsedTime.Seconds;
// Lock and unlock doors depending on whether the door closing sound has been played
if (RequireDoorClosingSound && train.DoorState != DoorStates.None)
{
if (DoorClosingSoundCounter > DoorClosingSoundDuration + DoorClosingSoundTimeout)
{
switch (train.DoorState)
{
case DoorStates.Both:
RequestedDoorInterlock = DoorInterlockStates.Locked;
break;
case DoorStates.Left:
RequestedDoorInterlock = DoorInterlockStates.Right;
break;
case DoorStates.Right:
RequestedDoorInterlock = DoorInterlockStates.Left;
break;
}
}
else if (DoorClosingSoundCounter > DoorClosingSoundDuration)
{
switch (train.DoorState)
{
case DoorStates.Both:
RequestedDoorInterlock = DoorInterlockStates.Unlocked;
break;
case DoorStates.Left:
RequestedDoorInterlock = DoorInterlockStates.Left;
break;
case DoorStates.Right:
RequestedDoorInterlock = DoorInterlockStates.Right;
break;
}
}
}
else
{
RequestedDoorInterlock = DoorInterlockStates.Unlocked;
}
// Check if AI needs to trigger the door closing sound
if (train.DoorState != DoorStates.None && DoorClosingSoundCounter > DoorClosingSoundDuration + DoorClosingSoundTimeout)
{
if (route.CurrentStation.Type == StationType.Normal || route.CurrentStation.Type == StationType.RequestStop)
{
// The current station is an intermediate stop
if (route.CurrentStation.DepartureTime < 0)
{
// The current station has dwell time, not a specific departure time
// Calculate departure time from the time when the doors opened
AiTriggerDoorClosingSound |= CurrentTime.Seconds >= DoorOpenTime.Seconds + (route.CurrentStation.StopTime - DoorClosingSoundDuration);
}
else
{
// Use departure time - door closing sound duration
AiTriggerDoorClosingSound |= CurrentTime.Seconds >= route.CurrentStation.DepartureTime - DoorClosingSoundDuration;
}
}
else
{
// The doors have been opened between stations
if (route.CurrentStation.StopPosition - route.CurrentStation.BackwardTolerance > train.State.Location)
{
AiTriggerDoorClosingSound = true;
}
else
{
AiTriggerDoorClosingSound = false;
}
}
}
else
{
AiTriggerDoorClosingSound = false;
}
}
/// <summary>Is called when the state of a key changes.</summary>
/// <param name="key">The key.</param>
/// <param name="pressed">Whether the key is pressed or released.</param>
/// <param name="train">The current train.</param>
internal override void KeyChange(VirtualKeys key, bool pressed, Train train)
{
if (pressed)
{
switch (key)
{
case VirtualKeys.F:
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
// Play the door closing sound and reset timeout
DoorClosingSoundCounter = 0;
if (!SoundManager.Playing(DoorClosingSoundIndex))
{
SoundManager.Play(DoorClosingSoundIndex, 1, 1, false);
}
}
break;
}
}
}
/// <summary>Is called when the state of the doors changes.</summary>
/// <param name="oldState">The old state of the doors.</param>
/// <param name="newState">The new state of the doors.</param>
internal override void DoorChange(DoorStates oldState, DoorStates newState)
{
// Set arrival time when doors open
if (oldState == DoorStates.None && newState != DoorStates.None)
{
DoorOpenTime = CurrentTime;
}
// Set door closing counter to value higher than maximum where doors are allowed to close
// This effectively locks the doors when they open
DoorClosingSoundCounter = DoorClosingSoundDuration + DoorClosingSoundTimeout;
}
/// <summary>Is called when the device should perform the AI.</summary>
/// <param name="data">The AI data.</param>
/// <param name="train">The current train.</param>
/// <param name="route">The current route.</param>
internal override void PerformAI(AIData data, Train train, Route route)
{
if (AiTriggerDoorClosingSound)
{
KeyChange(VirtualKeys.F, true, train);
data.Response = AIResponse.Short;
KeyChange(VirtualKeys.F, false, train);
AiTriggerDoorClosingSound = false;
}
}
}
}