Headlights: new device

This commit is contained in:
Marc Riera 2025-01-02 17:47:54 +01:00
parent cca085947f
commit b3c35ecf4e
4 changed files with 73 additions and 0 deletions

55
src/Devices/Headlights.cs Normal file
View file

@ -0,0 +1,55 @@
using System;
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A device handling the headlights.</summary>
internal class Headlights : Device
{
/// <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)
{
// Turn off headlights if the reverser is not set to forward
if (train.PhysicalHandles.Reverser < 1)
{
train.HeadlightState = 0;
}
}
/// <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.Headlights:
// Cycle through headlight states
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
if (train.PhysicalHandles.Reverser == 1)
{
if (train.HeadlightState < train.Specs.HeadlightStates)
{
train.HeadlightState++;
}
else
{
train.HeadlightState = 0;
}
}
}
break;
}
}
}
}
}

View file

@ -13,6 +13,7 @@ namespace OpenbveFcmbTrainPlugin
{ {
internal double TrainLength; internal double TrainLength;
internal bool HeadlightsDeviceEnabled;
internal bool DoorSelectionDeviceEnabled; internal bool DoorSelectionDeviceEnabled;
internal bool DoorClosingSoundDeviceEnabled; internal bool DoorClosingSoundDeviceEnabled;
internal bool DoorTractionCutDeviceEnabled; internal bool DoorTractionCutDeviceEnabled;
@ -93,6 +94,14 @@ namespace OpenbveFcmbTrainPlugin
break; break;
} }
break; break;
case "headlights":
switch (Key)
{
case "enabled":
PluginSettings.HeadlightsDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
}
break;
case "doorselection": case "doorselection":
switch (Key) switch (Key)
{ {

View file

@ -49,6 +49,7 @@
<Compile Include="Managers\ConfigManager.cs" /> <Compile Include="Managers\ConfigManager.cs" />
<Compile Include="Devices\DoorSelection.cs" /> <Compile Include="Devices\DoorSelection.cs" />
<Compile Include="Devices\DoorClosingSound.cs" /> <Compile Include="Devices\DoorClosingSound.cs" />
<Compile Include="Devices\Headlights.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Devices\" /> <Folder Include="Devices\" />

View file

@ -33,6 +33,9 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>The previous state of the train doors.</summary> /// <summary>The previous state of the train doors.</summary>
internal DoorStates PreviousDoorState { get; private set; } internal DoorStates PreviousDoorState { get; private set; }
/// <summary>The state of the train's headlights.</summary>
internal int HeadlightState;
/// <summary>The latest initialization mode of the train.</summary> /// <summary>The latest initialization mode of the train.</summary>
internal InitializationModes InitializationMode { get; private set; } internal InitializationModes InitializationMode { get; private set; }
@ -75,6 +78,10 @@ namespace OpenbveFcmbTrainPlugin
// Create list of devices and populate it according to settings // Create list of devices and populate it according to settings
Devices = new List<Device>(); Devices = new List<Device>();
ConfigManager.SettingsCollection settings = ConfigManager.PluginSettings; ConfigManager.SettingsCollection settings = ConfigManager.PluginSettings;
if (settings.HeadlightsDeviceEnabled)
{
Devices.Add(new Headlights());
}
if (settings.DoorSelectionDeviceEnabled) if (settings.DoorSelectionDeviceEnabled)
{ {
Devices.Add(new DoorSelection()); Devices.Add(new DoorSelection());
@ -129,6 +136,7 @@ namespace OpenbveFcmbTrainPlugin
{ {
State = data.Vehicle; State = data.Vehicle;
DoorInterlockState = data.DoorInterlockState; DoorInterlockState = data.DoorInterlockState;
HeadlightState = data.HeadlightState;
// Reset data to be passed to the simulator // Reset data to be passed to the simulator
data.DoorInterlockState = DoorInterlockStates.Unlocked; data.DoorInterlockState = DoorInterlockStates.Unlocked;