Skip to content

Commit

Permalink
Ambient sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 13, 2022
1 parent 109a0cd commit f26b177
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
22 changes: 21 additions & 1 deletion EndlessClient/Audio/AudioActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
}
42 changes: 28 additions & 14 deletions EndlessClient/Audio/SfxPlayer.cs
Original file line number Diff line number Diff line change
@@ -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<SoundEffectID, SoundEffectInstance> _activeSfx;
private SoundEffectInstance _activeSfx;

public SfxPlayer(IContentProvider contentProvider)
{
_contentProvider = contentProvider;
_activeSfx = new Dictionary<SoundEffectID, SoundEffectInstance>();
}

public void PlaySfx(SoundEffectID id)
{
_contentProvider.SFX[id].Play();
_contentProvider.SFX[id-1].Play();
}

public void PlayHarpNote(int index)
Expand All @@ -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();
}
}
6 changes: 2 additions & 4 deletions EndlessClient/HUD/Panels/SettingsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,15 @@ private void SettingChange(WhichSetting setting)

_soundChanged = true;
_configurationRepository.SoundEnabled = !_configurationRepository.SoundEnabled;
// todo:
// OldWorld.Instance.ActiveMapRenderer.PlayOrStopAmbientNoise();
_audioActions.ToggleSound();
};
dlg.ShowDialog();
}
else
{
_soundChanged = true;
_configurationRepository.SoundEnabled = !_configurationRepository.SoundEnabled;
// todo:
// OldWorld.Instance.ActiveMapRenderer.PlayOrStopAmbientNoise();
_audioActions.ToggleSound();
}
}
break;
Expand Down
20 changes: 18 additions & 2 deletions EndlessClient/Rendering/Map/MapChangedActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -40,7 +40,8 @@ public MapChangedActions(ICharacterStateCache characterStateCache,
ICurrentMapProvider currentMapProvider,
ICurrentMapStateRepository currentMapStateRepository,
IConfigurationProvider configurationProvider,
IMfxPlayer mfxPlayer)
IMfxPlayer mfxPlayer,
ISfxPlayer sfxPlayer)
{
_characterStateCache = characterStateCache;
_npcStateCache = npcStateCache;
Expand All @@ -53,13 +54,15 @@ public MapChangedActions(ICharacterStateCache characterStateCache,
_currentMapStateRepository = currentMapStateRepository;
_configurationProvider = configurationProvider;
_mfxPlayer = mfxPlayer;
_sfxPlayer = sfxPlayer;
}

public void ActiveCharacterEnterMapForLogin()
{
ShowMapNameIfAvailable(true);
ShowMapTransition(true);
PlayBackgroundMusic(differentMapID: true);
PlayAmbientNoise(differentMapID: true);
//todo: show message if map is a PK map
}

Expand All @@ -74,6 +77,7 @@ public void NotifyMapChanged(WarpAnimation warpAnimation, bool differentMapID)
AddSpikeTraps();
ShowWarpBubbles(warpAnimation);
PlayBackgroundMusic(differentMapID);
PlayAmbientNoise(differentMapID);

if (!differentMapID)
RedrawGroundLayer();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f26b177

Please sign in to comment.