diff --git a/EndlessClient/Audio/AudioActions.cs b/EndlessClient/Audio/AudioActions.cs index 015ad83d5..80a744a11 100644 --- a/EndlessClient/Audio/AudioActions.cs +++ b/EndlessClient/Audio/AudioActions.cs @@ -10,14 +10,17 @@ public class AudioActions : IAudioActions private readonly IConfigurationProvider _configurationProvider; private readonly ICurrentMapProvider _currentMapProvider; private readonly IMfxPlayer _mfxPlayer; + private readonly ISfxPlayer _sfxPlayer; public AudioActions(IConfigurationProvider configurationProvider, ICurrentMapProvider currentMapProvider, - IMfxPlayer mfxPlayer) + IMfxPlayer mfxPlayer, + ISfxPlayer sfxPlayer) { _configurationProvider = configurationProvider; _currentMapProvider = currentMapProvider; _mfxPlayer = mfxPlayer; + _sfxPlayer = sfxPlayer; } public void ToggleBackgroundMusic() @@ -35,10 +38,27 @@ public void ToggleBackgroundMusic() else _mfxPlayer.StopBackgroundMusic(); } + + public void ToggleSound() + { + if (!_configurationProvider.SoundEnabled) + { + _sfxPlayer.StopLoopingSfx(); + return; + } + + var noise = _currentMapProvider.CurrentMap.Properties.AmbientNoise; + if (noise > 0) + _sfxPlayer.PlayLoopingSfx((SoundEffectID)noise); + else + _sfxPlayer.StopLoopingSfx(); + } } public interface IAudioActions { void ToggleBackgroundMusic(); + + void ToggleSound(); } } diff --git a/EndlessClient/Audio/SfxPlayer.cs b/EndlessClient/Audio/SfxPlayer.cs index a33da05e2..6b5f433e7 100644 --- a/EndlessClient/Audio/SfxPlayer.cs +++ b/EndlessClient/Audio/SfxPlayer.cs @@ -1,25 +1,24 @@ using AutomaticTypeMapper; using EndlessClient.Content; using Microsoft.Xna.Framework.Audio; -using System.Collections.Generic; +using System; namespace EndlessClient.Audio { [AutoMappedType(IsSingleton = true)] - public class SfxPlayer : ISfxPlayer + public sealed class SfxPlayer : ISfxPlayer { private readonly IContentProvider _contentProvider; - private readonly Dictionary _activeSfx; + private SoundEffectInstance _activeSfx; public SfxPlayer(IContentProvider contentProvider) { _contentProvider = contentProvider; - _activeSfx = new Dictionary(); } public void PlaySfx(SoundEffectID id) { - _contentProvider.SFX[id].Play(); + _contentProvider.SFX[id-1].Play(); } public void PlayHarpNote(int index) @@ -40,23 +39,38 @@ public void PlayGuitarNote(int index) public void PlayLoopingSfx(SoundEffectID id) { - // todo: SFX + if (_activeSfx != null && _activeSfx.State != SoundState.Stopped) + return; + + StopLoopingSfx(); - //var res = _activeSfx.TryGetValue(id, out var sfxInstance); - //if (res && sfxInstance.State != SoundState.Stopped) - // return; + _activeSfx = _contentProvider.SFX[id-1].CreateInstance(); + _activeSfx.IsLooped = true; + _activeSfx.Play(); + } + + public void StopLoopingSfx() + { + _activeSfx?.Stop(); + _activeSfx?.Dispose(); + } - //if (res) - // _activeSfx[id].Dispose(); - //_activeSfx[id] = _contentProvider.SFX[id].CreateInstance(); - //_activeSfx[id] + public void Dispose() + { + StopLoopingSfx(); } } - public interface ISfxPlayer + public interface ISfxPlayer : IDisposable { + void PlaySfx(SoundEffectID id); + void PlayHarpNote(int index); void PlayGuitarNote(int index); + + void PlayLoopingSfx(SoundEffectID id); + + void StopLoopingSfx(); } } diff --git a/EndlessClient/HUD/Panels/SettingsPanel.cs b/EndlessClient/HUD/Panels/SettingsPanel.cs index dc9a7485c..377aafdd5 100644 --- a/EndlessClient/HUD/Panels/SettingsPanel.cs +++ b/EndlessClient/HUD/Panels/SettingsPanel.cs @@ -138,8 +138,7 @@ private void SettingChange(WhichSetting setting) _soundChanged = true; _configurationRepository.SoundEnabled = !_configurationRepository.SoundEnabled; - // todo: - // OldWorld.Instance.ActiveMapRenderer.PlayOrStopAmbientNoise(); + _audioActions.ToggleSound(); }; dlg.ShowDialog(); } @@ -147,8 +146,7 @@ private void SettingChange(WhichSetting setting) { _soundChanged = true; _configurationRepository.SoundEnabled = !_configurationRepository.SoundEnabled; - // todo: - // OldWorld.Instance.ActiveMapRenderer.PlayOrStopAmbientNoise(); + _audioActions.ToggleSound(); } } break; diff --git a/EndlessClient/Rendering/Map/MapChangedActions.cs b/EndlessClient/Rendering/Map/MapChangedActions.cs index 605a3b663..73d182638 100644 --- a/EndlessClient/Rendering/Map/MapChangedActions.cs +++ b/EndlessClient/Rendering/Map/MapChangedActions.cs @@ -2,7 +2,6 @@ using EndlessClient.Audio; using EndlessClient.ControlSets; using EndlessClient.HUD.Controls; -using EndlessClient.Input; using EndlessClient.Rendering.Character; using EndlessClient.Rendering.NPC; using EOLib.Config; @@ -29,6 +28,7 @@ public class MapChangedActions : IMapChangedNotifier, IMapChangedActions private readonly ICurrentMapStateRepository _currentMapStateRepository; private readonly IConfigurationProvider _configurationProvider; private readonly IMfxPlayer _mfxPlayer; + private readonly ISfxPlayer _sfxPlayer; public MapChangedActions(ICharacterStateCache characterStateCache, INPCStateCache npcStateCache, @@ -40,7 +40,8 @@ public MapChangedActions(ICharacterStateCache characterStateCache, ICurrentMapProvider currentMapProvider, ICurrentMapStateRepository currentMapStateRepository, IConfigurationProvider configurationProvider, - IMfxPlayer mfxPlayer) + IMfxPlayer mfxPlayer, + ISfxPlayer sfxPlayer) { _characterStateCache = characterStateCache; _npcStateCache = npcStateCache; @@ -53,6 +54,7 @@ public MapChangedActions(ICharacterStateCache characterStateCache, _currentMapStateRepository = currentMapStateRepository; _configurationProvider = configurationProvider; _mfxPlayer = mfxPlayer; + _sfxPlayer = sfxPlayer; } public void ActiveCharacterEnterMapForLogin() @@ -60,6 +62,7 @@ public void ActiveCharacterEnterMapForLogin() ShowMapNameIfAvailable(true); ShowMapTransition(true); PlayBackgroundMusic(differentMapID: true); + PlayAmbientNoise(differentMapID: true); //todo: show message if map is a PK map } @@ -74,6 +77,7 @@ public void NotifyMapChanged(WarpAnimation warpAnimation, bool differentMapID) AddSpikeTraps(); ShowWarpBubbles(warpAnimation); PlayBackgroundMusic(differentMapID); + PlayAmbientNoise(differentMapID); if (!differentMapID) RedrawGroundLayer(); @@ -163,6 +167,18 @@ private void PlayBackgroundMusic(bool differentMapID) else _mfxPlayer.StopBackgroundMusic(); } + + private void PlayAmbientNoise(bool differentMapID) + { + if (!_configurationProvider.SoundEnabled || !differentMapID) + return; + + var noise = _currentMapProvider.CurrentMap.Properties.AmbientNoise; + if (noise > 0) + _sfxPlayer.PlayLoopingSfx((SoundEffectID)noise); + else + _sfxPlayer.StopLoopingSfx(); + } } public interface IMapChangedActions