# HG changeset patch # User Alys Brooks # Date 2022-05-15 07:17:49 # Node ID 36b2118dd380d7b258fd3d76dd994d1aabf831a6 # Parent 5935315e1a82f7b756579004cf0e66e505a712e1 Switch over to SoundEffectsEngine Replace inline calls to play sounds to a dedicated engine. Having an engine will also make it easier to have audio settings. diff --git a/isometric-park-fna/Engines/InputEngine.cs b/isometric-park-fna/Engines/InputEngine.cs --- a/isometric-park-fna/Engines/InputEngine.cs +++ b/isometric-park-fna/Engines/InputEngine.cs @@ -25,8 +25,8 @@ typeof(GameStateMessage), typeof(QuitGameMessage), typeof(SpawnSelection), - typeof(AdjustSelection) - )] + typeof(AdjustSelection), + typeof(PlaySoundMessage))] [Reads(typeof(WindowTypeComponent), typeof(GameStateComponent), typeof(VisibilityComponent), @@ -51,9 +51,7 @@ private SoundEffect endSound; public InputEngine(int menuBarHeight, Camera camera, - GraphicsDeviceManager gdm, int height, int width, - SoundEffect sound, SoundEffect startSound, - SoundEffect endSound) { + GraphicsDeviceManager gdm, int height, int width) { //initialize to blank for now this.keyboardPrev = new KeyboardState(); this.menuBarHeight = menuBarHeight; @@ -279,7 +277,7 @@ if (isPlaying && !io.WantCaptureMouse) { if (mouseCur.RightButton == ButtonState.Pressed && mousePrev.RightButton == ButtonState.Released) { - this.sound.Play(1.0f, 0.0f, 0.0f); + SendMessage(new PlaySoundMessage { SoundName = "Click" }); SendMessage(new JumpCameraMessage {Movement = original_point}); } @@ -316,10 +314,10 @@ } else if (ImGui.IsAnyItemHovered()) { if (mouseCur.LeftButton == ButtonState.Pressed && mousePrev.LeftButton == ButtonState.Released) { - this.startSound.Play(1.0f, 0.0f, 0.0f); + SendMessage(new PlaySoundMessage { SoundName = "ClickPart1" }); } else if (mouseCur.LeftButton == ButtonState.Released && mousePrev.LeftButton == ButtonState.Pressed) { - this.endSound.Play(1.0f, 0.0f, 0.0f); + SendMessage(new PlaySoundMessage { SoundName = "ClickPart2" }); } } diff --git a/isometric-park-fna/Engines/SoundEffectsEngine.cs b/isometric-park-fna/Engines/SoundEffectsEngine.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Engines/SoundEffectsEngine.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; + +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Audio; + +using Encompass; + +using isometricparkfna.Messages; + +namespace isometricparkfna.Engines +{ + + [Receives(typeof(PlaySoundMessage))] + public class SoundEffectEngine : Engine + { + public static Dictionary sounds = new Dictionary(); + public float volume = 1.0f; + public float pitch = 0.0f; + public float pan = 0.0f; + + + public static SoundEffect LoadSound(ContentManager content, string path) { + return LoadSound(content, path, path); + } + + public static SoundEffect LoadSound(ContentManager content, + string path, string name) { + var sfx = content.Load(path); + sounds.Add(name, sfx); + return sfx; + + } + + public static void DisposeSounds() { + + foreach (KeyValuePair entry in sounds ) { + entry.Value.Dispose(); + } + + } + + public override void Update(double dt) { + + foreach(ref readonly var message + in ReadMessages()) { + var sound = sounds[message.SoundName]; + + sound.Play(this.volume, this.pitch, this.pan); + + } + + } + + } +} diff --git a/isometric-park-fna/FNAGame.cs b/isometric-park-fna/FNAGame.cs --- a/isometric-park-fna/FNAGame.cs +++ b/isometric-park-fna/FNAGame.cs @@ -46,9 +46,6 @@ private SpriteBatch tileBatch; // #if DEBUG private SoundEffect sound; - private SoundEffect clickSound; - private SoundEffect clickStartSound; - private SoundEffect clickEndSound; // #endif private SpriteFont monoFont; private SpriteFont largeMonoFont; @@ -197,10 +194,10 @@ this.tileBatch = new SpriteBatch(GraphicsDevice); // #if DEBUG - this.sound = Content.Load("FNASound"); - this.clickSound = Content.Load("Click"); - this.clickStartSound = Content.Load("ClickPart1"); - this.clickEndSound = Content.Load("ClickPart2"); + SoundEffectEngine.LoadSound(Content, "FNASound"); + SoundEffectEngine.LoadSound(Content, "Click"); + SoundEffectEngine.LoadSound(Content, "ClickPart1"); + SoundEffectEngine.LoadSound(Content, "ClickPart2"); // #endif Tile.TileSetTexture = Content.Load(@"merged_tileset"); var texture = Content.Load(@"solid_tileset"); @@ -272,8 +269,7 @@ Logging.Debug(this.Story.ContinueMaximally()); WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm, - this.simulation.map.MapHeight, this.simulation.map.MapWidth, - this.clickSound, this.clickStartSound, this.clickEndSound)); + this.simulation.map.MapHeight, this.simulation.map.MapWidth)); WorldBuilder.AddEngine(new UIEngine(this.Story)); WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar)); @@ -297,6 +293,7 @@ WorldBuilder.AddEngine(new TraceryBridgeEngine(this.grammar)); WorldBuilder.AddEngine(new SimulationGameRateBridgeEngine(this.simulation)); WorldBuilder.AddEngine(new BuildToolEngine(this.simulation.map)); + WorldBuilder.AddEngine(new SoundEffectEngine()); WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1); WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2); @@ -433,9 +430,8 @@ batch.Dispose(); // #if DEBUG sound.Dispose(); - clickSound.Dispose(); - clickStartSound.Dispose(); - clickEndSound.Dispose(); + SoundEffectEngine.DisposeSounds(); + // #endif Tile.TileSetTexture.Dispose(); Logging.Success("Disposed of Tile texture."); diff --git a/isometric-park-fna/Messages/PlaySoundMessage.cs b/isometric-park-fna/Messages/PlaySoundMessage.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Messages/PlaySoundMessage.cs @@ -0,0 +1,15 @@ +using Microsoft.Xna.Framework.Audio; + + +using Encompass; + + +namespace isometricparkfna.Messages +{ + + public struct PlaySoundMessage : IMessage + { + public string SoundName; + + } +} diff --git a/isometric-park-fna/UI/OptionsWindow.cs b/isometric-park-fna/UI/OptionsWindow.cs --- a/isometric-park-fna/UI/OptionsWindow.cs +++ b/isometric-park-fna/UI/OptionsWindow.cs @@ -19,6 +19,8 @@ public static bool newFullscreen; public static Vector2 newResolution; + public static float newSoundEffects; + public static bool newSoundEffectsMute; private static string fontName = "Iosevka"; private static int fontSize = 15; @@ -135,6 +137,21 @@ ImGui.Checkbox("Fullscreen", ref newFullscreen); + + + ImGui.Separator(); + + ImGui.PushFont(italicFont); + ImGui.Text("Audio"); + ImGui.PopFont(); + ImGui.Text("Sound Effects:\t"); + + ImGui.SameLine(); + ImGui.DragFloat("##Sfx", ref newSoundEffects, 0.005f, 0.0f, 1.0f, "%.2f"); + ImGui.SameLine(); + ImGui.Checkbox("Mute", ref newSoundEffectsMute); + + ImGui.Separator(); ImGui.PushFont(italicFont);