openbve-fcmb-train-plugin/src/OpenbveFcmbTrainPlugin.cs
2024-11-03 22:47:50 +01:00

138 lines
5.3 KiB
C#

using System;
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
/// <summary>The interface to be implemented by the plugin.</summary>
public class OpenbveFcmbTrainPlugin : IRuntime
{
/// <summary>The train that is simulated by this plugin.</summary>
private Train Train;
/// <summary>The route where the train is running.</summary>
private Route Route;
/// <summary>Whether the train is initializing or reinitializing.</summary>
internal static bool Initializing = true;
/// <summary>Remembers which of the virtual keys are currently pressed down.</summary>
internal static bool[] KeysPressed = new bool[Enum.GetNames(typeof(VirtualKeys)).Length];
/// <summary>Is called when the plugin is loaded.</summary>
/// <param name="properties">The properties supplied to the plugin on loading.</param>
/// <returns>Whether the plugin was loaded successfully.</returns>
public bool Load(LoadProperties properties)
{
properties.AISupport = AISupport.Basic;
properties.Panel = new int[256];
Train = new Train(properties.Panel);
Route = new Route();
MessageManager.Initialize(properties.AddMessage, properties.AddScore);
SoundManager.Initialize(256, properties.PlaySound, properties.PlayCarSound, properties.PlayMultipleCarSound);
return true;
}
/// <summary>Is called when the plugin is unloaded.</summary>
public void Unload()
{
}
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary>
/// <param name="specs">The specifications of the train.</param>
public void SetVehicleSpecs(VehicleSpecs specs)
{
Train.SetVehicleSpecs(specs);
}
/// <summary>Is called when the plugin should initialize or reinitialize.</summary>
/// <param name="mode">The mode of initialization.</param>
public void Initialize(InitializationModes mode)
{
// On initialization, a variable is set so the first call to Elapse() can be used for initialization
Initializing = true;
Train.Initialize(mode);
}
/// <summary>Is called every frame.</summary>
/// <param name="data">The data passed to the plugin.</param>
public void Elapse(ElapseData data)
{
Route.Elapse(data);
Train.Elapse(data, Route);
Initializing = false;
}
/// <summary>Is called when the driver changes the reverser.</summary>
/// <param name="reverser">The new reverser position.</param>
public void SetReverser(int reverser)
{
Train.SetReverser(reverser);
}
/// <summary>Is called when the driver changes the power notch.</summary>
/// <param name="powerNotch">The new power notch.</param>
public void SetPower(int powerNotch)
{
Train.SetPower(powerNotch);
}
/// <summary>Is called when the driver changes the brake notch.</summary>
/// <param name="brakeNotch">The new brake notch.</param>
public void SetBrake(int brakeNotch)
{
Train.SetBrake(brakeNotch);
}
/// <summary>Is called when a virtual key is pressed.</summary>
/// <param name="key">The virtual key that was pressed.</param>
public void KeyDown(VirtualKeys key)
{
Train.KeyDown(key);
KeysPressed[(int)key] = true;
}
/// <summary>Is called when a virtual key is released.</summary>
/// <param name="key">The virtual key that was released.</param>
public void KeyUp(VirtualKeys key)
{
Train.KeyUp(key);
KeysPressed[(int)key] = false;
}
/// <summary>Is called when a horn is played or when the music horn is stopped.</summary>
/// <param name="type">The type of horn.</param>
public void HornBlow(HornTypes type)
{
}
/// <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>
public void DoorChange(DoorStates oldState, DoorStates newState)
{
Route.DoorChange(oldState, newState);
Train.DoorChange(oldState, newState);
}
/// <summary>Is called when the aspect in the current or in any of the upcoming sections changes, or when passing section boundaries.</summary>
/// <remarks>The signal array is guaranteed to have at least one element. When accessing elements other than index 0, you must check the bounds of the array first.</remarks>
public void SetSignal(SignalData[] signal)
{
}
/// <summary>Is called when the train passes a beacon.</summary>
/// <param name="beacon">The beacon data.</param>
public void SetBeacon(BeaconData beacon)
{
Train.SetBeacon(beacon);
}
/// <summary>Is called when the plugin should perform the AI.</summary>
/// <param name="data">The AI data.</param>
public void PerformAI(AIData data)
{
Train.PerformAI(data, Route);
}
}
}