Add message and sound managers

main
Marc Riera 2024-10-29 14:56:21 +01:00
parent 2ac6b01cc3
commit 7574de93ab
4 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,41 @@
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
internal static class MessageManager
{
/// <summary>The callback function to show an interface message.</summary>
private static AddInterfaceMessageDelegate MessageDelegate;
/// <summary>The callback function to show a score message.</summary>
private static AddScoreDelegate ScoreDelegate;
/// <summary>Initializes the message manager.</summary>
/// <param name="messageDelegate">The delegate to add interface messages.</param>
/// <param name="scoreDelegate">The delegate to add score messages.</param>
internal static void Initialize(AddInterfaceMessageDelegate messageDelegate, AddScoreDelegate scoreDelegate)
{
MessageDelegate = messageDelegate;
ScoreDelegate = scoreDelegate;
}
/// <summary>Shows a message to the player.</summary>
/// <param name="message">The message to be shown.</param>
/// <param name="color">The color of the message.</param>
/// <param name="time">The time to show the message.</param>
internal static void ShowMessage(string message, OpenBveApi.Colors.MessageColor color, double time)
{
MessageDelegate(message, color, time);
}
/// <summary>Shows a message to the player.</summary>
/// <param name="message">The message to be shown.</param>
/// <param name="color">The color of the message.</param>
/// <param name="time">The time to show the message.</param>
/// <param name="score">The score for the player.</param>
internal static void ShowMessage( string message, OpenBveApi.Colors.MessageColor color, double time, int score)
{
ScoreDelegate(score, message, color, time);
}
}
}

View File

@ -0,0 +1,108 @@
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
internal static class SoundManager
{
/// <summary>An array storing the sound handles.</summary>
private static SoundHandle[] Sounds;
/// <summary>The callback function to play sounds.</summary>
private static PlaySoundDelegate PlaySoundDelegate;
/// <summary>The callback function to play sounds in a specific car.</summary>
private static PlayCarSoundDelegate PlayCarSoundDelegate;
/// <summary>The callback function to play sounds in specific cars.</summary>
private static PlayMultipleCarSoundDelegate PlayMultipleCarSoundDelegate;
/// <summary>Initializes the sound manager.</summary>
/// <param name="numSounds">The number of sounds supported by the plugin.</param>
/// <param name="soundDelegate">The delegate to play sounds.</param>
/// <param name="carSoundDelegate">The delegate to play sounds in a specific car.</param>
/// <param name="multipleCarSoundDelegate">The delegate to play sounds in specific cars.</param>
internal static void Initialize(int numSounds, PlaySoundDelegate soundDelegate, PlayCarSoundDelegate carSoundDelegate, PlayMultipleCarSoundDelegate multipleCarSoundDelegate)
{
Sounds = new SoundHandle[numSounds];
PlaySoundDelegate = soundDelegate;
PlayCarSoundDelegate = carSoundDelegate;
PlayMultipleCarSoundDelegate = multipleCarSoundDelegate;
}
/// <summary>Plays a sound.</summary>
/// <param name="soundIndex">The index of the sound to be played.</param>
/// <param name="volume">The volume of the sound to be played.</param>
/// <param name="pitch">The pitch of the sound to be played.</param>
/// <param name="looped">Whether the sound to be played is looped.</param>
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);
}
}
}
/// <summary>Plays a sound.</summary>
/// <param name="soundIndex">The index of the sound to be played.</param>
/// <param name="volume">The volume of the sound to be played.</param>
/// <param name="pitch">The pitch of the sound to be played.</param>
/// <param name="looped">Whether the sound to be played is looped.</param>
/// <param name="car">The index of the car where the sound will be played.</param>
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);
}
}
}
/// <summary>Stops a sound.</summary>
/// <param name="soundIndex">The index of the sound to be stopped.</param>
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();
}
}
/// <summary>Whether a sound is playing.</summary>
/// <param name="soundIndex">The index of the sound to check.</param>
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;
}
}
}

View File

@ -27,6 +27,8 @@ namespace OpenbveFcmbTrainPlugin
properties.Panel = new int[256]; properties.Panel = new int[256];
Train = new Train(properties.Panel); Train = new Train(properties.Panel);
Route = new Route(); Route = new Route();
MessageManager.Initialize(properties.AddMessage, properties.AddScore);
SoundManager.Initialize(256, properties.PlaySound, properties.PlayCarSound, properties.PlayMultipleCarSound);
return true; return true;
} }

View File

@ -43,11 +43,14 @@
<Compile Include="Devices\Deadman.cs" /> <Compile Include="Devices\Deadman.cs" />
<Compile Include="Route\Route.cs" /> <Compile Include="Route\Route.cs" />
<Compile Include="Devices\AtcBombardier.cs" /> <Compile Include="Devices\AtcBombardier.cs" />
<Compile Include="Managers\SoundManager.cs" />
<Compile Include="Managers\MessageManager.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Devices\" /> <Folder Include="Devices\" />
<Folder Include="Train\" /> <Folder Include="Train\" />
<Folder Include="Route\" /> <Folder Include="Route\" />
<Folder Include="Managers\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project> </Project>