Adds space ambience (#8096)
* Adds space ambience * handle that better * Update Content.Client/Audio/BackgroundAudioSystem.cs Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com> * Update Content.Client/Audio/BackgroundAudioSystem.cs Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com> Co-authored-by: ike709 <ike709@github.com> Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com>
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
using System.Threading;
|
||||
using Content.Client.GameTicking.Managers;
|
||||
using Content.Client.Lobby;
|
||||
using Content.Client.Viewport;
|
||||
using Content.Shared;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Maps;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.State;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Client.Audio
|
||||
{
|
||||
@@ -26,8 +31,9 @@ namespace Content.Client.Audio
|
||||
[Dependency] private readonly IStateManager _stateManager = default!;
|
||||
[Dependency] private readonly IBaseClient _client = default!;
|
||||
[Dependency] private readonly ClientGameTicker _gameTicker = default!;
|
||||
|
||||
private SoundCollectionPrototype _ambientCollection = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
|
||||
[Dependency] private readonly IPlayerManager _playMan = default!;
|
||||
|
||||
private readonly AudioParams _ambientParams = new(-10f, 1, "Master", 0, 0, 0, true, 0f);
|
||||
private readonly AudioParams _lobbyParams = new(-5f, 1, "Master", 0, 0, 0, true, 0f);
|
||||
@@ -35,14 +41,26 @@ namespace Content.Client.Audio
|
||||
private IPlayingAudioStream? _ambientStream;
|
||||
private IPlayingAudioStream? _lobbyStream;
|
||||
|
||||
private SoundCollectionPrototype _currentCollection = default!;
|
||||
private CancellationTokenSource _timerCancelTokenSource = new();
|
||||
|
||||
private SoundCollectionPrototype _spaceAmbience = default!;
|
||||
private SoundCollectionPrototype _stationAmbience = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_ambientCollection = _prototypeManager.Index<SoundCollectionPrototype>("AmbienceBase");
|
||||
_stationAmbience = _prototypeManager.Index<SoundCollectionPrototype>("StationAmbienceBase");
|
||||
_spaceAmbience = _prototypeManager.Index<SoundCollectionPrototype>("SpaceAmbienceBase");
|
||||
_currentCollection = _stationAmbience;
|
||||
|
||||
_configManager.OnValueChanged(CCVars.AmbienceVolume, AmbienceCVarChanged);
|
||||
_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
|
||||
_configManager.OnValueChanged(CCVars.StationAmbienceEnabled, StationAmbienceCVarChanged);
|
||||
_configManager.OnValueChanged(CCVars.SpaceAmbienceEnabled, SpaceAmbienceCVarChanged);
|
||||
|
||||
SubscribeLocalEvent<EntParentChangedMessage>(EntParentChanged);
|
||||
|
||||
_stateManager.OnStateChanged += StateManagerOnStateChanged;
|
||||
|
||||
@@ -67,6 +85,36 @@ namespace Content.Client.Audio
|
||||
EndLobbyMusic();
|
||||
}
|
||||
|
||||
private void EntParentChanged(ref EntParentChangedMessage message)
|
||||
{
|
||||
if(_playMan.LocalPlayer is null || _playMan.LocalPlayer.ControlledEntity != message.Entity) return;
|
||||
if (!TryComp<TransformComponent>(message.Entity, out var xform) ||
|
||||
!_mapManager.TryGetGrid(xform.GridID, out var grid)) return;
|
||||
|
||||
var tileDef = (ContentTileDefinition) _tileDefMan[grid.GetTileRef(xform.Coordinates).Tile.TypeId];
|
||||
|
||||
if(_currentCollection.ID == _spaceAmbience.ID)
|
||||
{
|
||||
if (!tileDef.Sturdy) return;
|
||||
ChangeAmbience(_stationAmbience);
|
||||
|
||||
}
|
||||
else // currently station
|
||||
{
|
||||
if (tileDef.Sturdy) return;
|
||||
ChangeAmbience(_spaceAmbience);
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangeAmbience(SoundCollectionPrototype newAmbience)
|
||||
{
|
||||
EndAmbience();
|
||||
_currentCollection = newAmbience;
|
||||
_timerCancelTokenSource.Cancel();
|
||||
_timerCancelTokenSource = new();
|
||||
Timer.Spawn(1500, StartAmbience, _timerCancelTokenSource.Token);
|
||||
}
|
||||
|
||||
private void StateManagerOnStateChanged(StateChangedEventArgs args)
|
||||
{
|
||||
EndAmbience();
|
||||
@@ -122,7 +170,8 @@ namespace Content.Client.Audio
|
||||
private void StartAmbience()
|
||||
{
|
||||
EndAmbience();
|
||||
var file = _robustRandom.Pick(_ambientCollection.PickFiles).ToString();
|
||||
if (!CanPlayCollection(_currentCollection)) return;
|
||||
var file = _robustRandom.Pick(_currentCollection.PickFiles).ToString();
|
||||
_ambientStream = SoundSystem.Play(Filter.Local(), file, _ambientParams.WithVolume(_ambientParams.Volume + _configManager.GetCVar(CCVars.AmbienceVolume)));
|
||||
}
|
||||
|
||||
@@ -132,6 +181,40 @@ namespace Content.Client.Audio
|
||||
_ambientStream = null;
|
||||
}
|
||||
|
||||
private bool CanPlayCollection(SoundCollectionPrototype collection)
|
||||
{
|
||||
if (collection.ID == _spaceAmbience.ID)
|
||||
return _configManager.GetCVar(CCVars.SpaceAmbienceEnabled);
|
||||
if (collection.ID == _stationAmbience.ID)
|
||||
return _configManager.GetCVar(CCVars.StationAmbienceEnabled);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void StationAmbienceCVarChanged(bool enabled)
|
||||
{
|
||||
if (enabled && _stateManager.CurrentState is GameScreen && _currentCollection.ID == _stationAmbience.ID)
|
||||
{
|
||||
StartAmbience();
|
||||
}
|
||||
else if(_currentCollection.ID == _stationAmbience.ID)
|
||||
{
|
||||
EndAmbience();
|
||||
}
|
||||
}
|
||||
|
||||
private void SpaceAmbienceCVarChanged(bool enabled)
|
||||
{
|
||||
if (enabled && _stateManager.CurrentState is GameScreen && _currentCollection.ID == _spaceAmbience.ID)
|
||||
{
|
||||
StartAmbience();
|
||||
}
|
||||
else if(_currentCollection.ID == _spaceAmbience.ID)
|
||||
{
|
||||
EndAmbience();
|
||||
}
|
||||
}
|
||||
|
||||
private void LobbyMusicCVarChanged(bool musicEnabled)
|
||||
{
|
||||
if (!musicEnabled)
|
||||
|
||||
@@ -63,6 +63,8 @@
|
||||
</BoxContainer>
|
||||
<Control MinSize="0 8" />
|
||||
<CheckBox Name="LobbyMusicCheckBox" Text="{Loc 'ui-options-lobby-music'}" />
|
||||
<CheckBox Name="StationAmbienceCheckBox" Text="{Loc 'ui-options-station-ambience'}" />
|
||||
<CheckBox Name="SpaceAmbienceCheckBox" Text="{Loc 'ui-options-space-ambience'}" />
|
||||
</BoxContainer>
|
||||
</BoxContainer>
|
||||
<hudUi:StripeBack HasBottomEdge="False" HasMargins="False">
|
||||
|
||||
@@ -22,6 +22,8 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
LobbyMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
StationAmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.StationAmbienceEnabled);
|
||||
SpaceAmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.SpaceAmbienceEnabled);
|
||||
|
||||
ApplyButton.OnPressed += OnApplyButtonPressed;
|
||||
ResetButton.OnPressed += OnResetButtonPressed;
|
||||
@@ -30,6 +32,8 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
AmbienceVolumeSlider.OnValueChanged += OnAmbienceVolumeSliderChanged;
|
||||
AmbienceSoundsSlider.OnValueChanged += OnAmbienceSoundsSliderChanged;
|
||||
LobbyMusicCheckBox.OnToggled += OnLobbyMusicCheckToggled;
|
||||
StationAmbienceCheckBox.OnToggled += OnStationAmbienceCheckToggled;
|
||||
SpaceAmbienceCheckBox.OnToggled += OnSpaceAmbienceCheckToggled;
|
||||
|
||||
AmbienceSoundsSlider.MinValue = _cfg.GetCVar(CCVars.MinMaxAmbientSourcesConfigured);
|
||||
AmbienceSoundsSlider.MaxValue = _cfg.GetCVar(CCVars.MaxMaxAmbientSourcesConfigured);
|
||||
@@ -73,6 +77,16 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
private void OnStationAmbienceCheckToggled(BaseButton.ButtonEventArgs args)
|
||||
{
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
private void OnSpaceAmbienceCheckToggled(BaseButton.ButtonEventArgs args)
|
||||
{
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
|
||||
{
|
||||
_cfg.SetCVar(CVars.AudioMasterVolume, MasterVolumeSlider.Value / 100);
|
||||
@@ -80,6 +94,8 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
_cfg.SetCVar(CCVars.AmbienceVolume, LV100ToDB(AmbienceVolumeSlider.Value));
|
||||
_cfg.SetCVar(CCVars.MaxAmbientSources, (int)AmbienceSoundsSlider.Value);
|
||||
_cfg.SetCVar(CCVars.LobbyMusicEnabled, LobbyMusicCheckBox.Pressed);
|
||||
_cfg.SetCVar(CCVars.StationAmbienceEnabled, StationAmbienceCheckBox.Pressed);
|
||||
_cfg.SetCVar(CCVars.SpaceAmbienceEnabled, SpaceAmbienceCheckBox.Pressed);
|
||||
_cfg.SaveToFile();
|
||||
UpdateChanges();
|
||||
}
|
||||
@@ -96,6 +112,8 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
AmbienceVolumeSlider.Value = DBToLV100(_cfg.GetCVar(CCVars.AmbienceVolume));
|
||||
AmbienceSoundsSlider.Value = _cfg.GetCVar(CCVars.MaxAmbientSources);
|
||||
LobbyMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
StationAmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.StationAmbienceEnabled);
|
||||
SpaceAmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.SpaceAmbienceEnabled);
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
@@ -122,7 +140,9 @@ namespace Content.Client.EscapeMenu.UI.Tabs
|
||||
Math.Abs(AmbienceVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CCVars.AmbienceVolume))) < 0.01f;
|
||||
var isAmbientSoundsSame = (int)AmbienceSoundsSlider.Value == _cfg.GetCVar(CCVars.MaxAmbientSources);
|
||||
var isLobbySame = LobbyMusicCheckBox.Pressed == _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
var isEverythingSame = isMasterVolumeSame && isMidiVolumeSame && isAmbientVolumeSame && isAmbientSoundsSame && isLobbySame;
|
||||
var isStationAmbienceSame = StationAmbienceCheckBox.Pressed == _cfg.GetCVar(CCVars.StationAmbienceEnabled);
|
||||
var isSpaceAmbienceSame = SpaceAmbienceCheckBox.Pressed == _cfg.GetCVar(CCVars.SpaceAmbienceEnabled);
|
||||
var isEverythingSame = isMasterVolumeSame && isMidiVolumeSame && isAmbientVolumeSame && isAmbientSoundsSame && isLobbySame && isStationAmbienceSame && isSpaceAmbienceSame;
|
||||
ApplyButton.Disabled = isEverythingSame;
|
||||
ResetButton.Disabled = isEverythingSame;
|
||||
MasterVolumeLabel.Text =
|
||||
|
||||
Reference in New Issue
Block a user