Add message and sound managers
This commit is contained in:
parent
2ac6b01cc3
commit
7574de93ab
4 changed files with 154 additions and 0 deletions
41
src/Managers/MessageManager.cs
Normal file
41
src/Managers/MessageManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
108
src/Managers/SoundManager.cs
Normal file
108
src/Managers/SoundManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue