From 7574de93ab644505eace6b2c2b7847d6d6d07c8c Mon Sep 17 00:00:00 2001 From: Marc Riera Date: Tue, 29 Oct 2024 14:56:21 +0100 Subject: [PATCH] Add message and sound managers --- src/Managers/MessageManager.cs | 41 ++++++++++++ src/Managers/SoundManager.cs | 108 ++++++++++++++++++++++++++++++ src/OpenbveFcmbTrainPlugin.cs | 2 + src/OpenbveFcmbTrainPlugin.csproj | 3 + 4 files changed, 154 insertions(+) create mode 100644 src/Managers/MessageManager.cs create mode 100644 src/Managers/SoundManager.cs diff --git a/src/Managers/MessageManager.cs b/src/Managers/MessageManager.cs new file mode 100644 index 0000000..6024b30 --- /dev/null +++ b/src/Managers/MessageManager.cs @@ -0,0 +1,41 @@ +using OpenBveApi.Runtime; + +namespace OpenbveFcmbTrainPlugin +{ + internal static class MessageManager + { + /// The callback function to show an interface message. + private static AddInterfaceMessageDelegate MessageDelegate; + + /// The callback function to show a score message. + private static AddScoreDelegate ScoreDelegate; + + /// Initializes the message manager. + /// The delegate to add interface messages. + /// The delegate to add score messages. + internal static void Initialize(AddInterfaceMessageDelegate messageDelegate, AddScoreDelegate scoreDelegate) + { + MessageDelegate = messageDelegate; + ScoreDelegate = scoreDelegate; + } + + /// Shows a message to the player. + /// The message to be shown. + /// The color of the message. + /// The time to show the message. + internal static void ShowMessage(string message, OpenBveApi.Colors.MessageColor color, double time) + { + MessageDelegate(message, color, time); + } + + /// Shows a message to the player. + /// The message to be shown. + /// The color of the message. + /// The time to show the message. + /// The score for the player. + internal static void ShowMessage( string message, OpenBveApi.Colors.MessageColor color, double time, int score) + { + ScoreDelegate(score, message, color, time); + } + } +} diff --git a/src/Managers/SoundManager.cs b/src/Managers/SoundManager.cs new file mode 100644 index 0000000..ba1dc03 --- /dev/null +++ b/src/Managers/SoundManager.cs @@ -0,0 +1,108 @@ +using OpenBveApi.Runtime; + +namespace OpenbveFcmbTrainPlugin +{ + internal static class SoundManager + { + /// An array storing the sound handles. + private static SoundHandle[] Sounds; + + /// The callback function to play sounds. + private static PlaySoundDelegate PlaySoundDelegate; + + /// The callback function to play sounds in a specific car. + private static PlayCarSoundDelegate PlayCarSoundDelegate; + + /// The callback function to play sounds in specific cars. + private static PlayMultipleCarSoundDelegate PlayMultipleCarSoundDelegate; + + /// Initializes the sound manager. + /// The number of sounds supported by the plugin. + /// The delegate to play sounds. + /// The delegate to play sounds in a specific car. + /// The delegate to play sounds in specific cars. + internal static void Initialize(int numSounds, PlaySoundDelegate soundDelegate, PlayCarSoundDelegate carSoundDelegate, PlayMultipleCarSoundDelegate multipleCarSoundDelegate) + { + Sounds = new SoundHandle[numSounds]; + PlaySoundDelegate = soundDelegate; + PlayCarSoundDelegate = carSoundDelegate; + PlayMultipleCarSoundDelegate = multipleCarSoundDelegate; + } + + /// Plays a sound. + /// The index of the sound to be played. + /// The volume of the sound to be played. + /// The pitch of the sound to be played. + /// Whether the sound to be played is looped. + internal static void Play(int soundIndex, double volume, double pitch, bool looped) + { + // Validate index before doing anything + if (soundIndex < Sounds.Length && soundIndex >= 0) + { + if (Sounds[soundIndex].Playing) + { + // The sound is still playing, update volume and pitch + Sounds[soundIndex].Volume = volume; + Sounds[soundIndex].Pitch = pitch; + } + else + { + // The sound is not playing, play it now + Sounds[soundIndex] = PlaySoundDelegate(soundIndex, volume, pitch, looped); + } + } + } + + /// Plays a sound. + /// The index of the sound to be played. + /// The volume of the sound to be played. + /// The pitch of the sound to be played. + /// Whether the sound to be played is looped. + /// The index of the car where the sound will be played. + internal static void Play(int soundIndex, double volume, double pitch, bool looped, int car) + { + // Validate index before doing anything + if (soundIndex < Sounds.Length && soundIndex >= 0) + { + if (Sounds[soundIndex].Playing) + { + // The sound is still playing, update volume and pitch + Sounds[soundIndex].Volume = volume; + Sounds[soundIndex].Pitch = pitch; + } + else + { + // The sound is not playing, play it now + Sounds[soundIndex] = PlayCarSoundDelegate(soundIndex, volume, pitch, looped, car); + } + } + } + + /// Stops a sound. + /// The index of the sound to be stopped. + internal static void Stop(int soundIndex) + { + // Validate index before doing anything + if (soundIndex < Sounds.Length && soundIndex >= 0 && Sounds[soundIndex] != null) + { + // Stop sound + Sounds[soundIndex].Stop(); + } + } + + /// Whether a sound is playing. + /// The index of the sound to check. + internal static bool Playing(int soundIndex) + { + // Validate index before doing anything + if (soundIndex < Sounds.Length && soundIndex >= 0 && Sounds[soundIndex] != null) + { + if (Sounds[soundIndex].Playing) + { + return true; + } + } + return false; + } + } +} diff --git a/src/OpenbveFcmbTrainPlugin.cs b/src/OpenbveFcmbTrainPlugin.cs index a1e805b..951de53 100644 --- a/src/OpenbveFcmbTrainPlugin.cs +++ b/src/OpenbveFcmbTrainPlugin.cs @@ -27,6 +27,8 @@ namespace OpenbveFcmbTrainPlugin 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; } diff --git a/src/OpenbveFcmbTrainPlugin.csproj b/src/OpenbveFcmbTrainPlugin.csproj index 7fd8c69..c15573a 100644 --- a/src/OpenbveFcmbTrainPlugin.csproj +++ b/src/OpenbveFcmbTrainPlugin.csproj @@ -43,11 +43,14 @@ + + +