Adds a very basic perpetual background ambient hum. (#3150)

This commit is contained in:
tmtmtl30
2021-02-21 05:02:23 -08:00
committed by GitHub
parent 4637bdf60e
commit b314c3f040
6 changed files with 86 additions and 2 deletions

View File

@@ -0,0 +1,54 @@
#nullable enable
using Content.Shared.Audio;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Content.Shared;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Client.GameObjects.EntitySystems
{
[UsedImplicitly]
public class AmbienceSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
private AudioSystem _audioSystem = default!;
private SoundCollectionPrototype _ambientCollection = default!;
private AudioParams _ambientParams = new(-10f, 1, "Master", 0, 0, AudioMixTarget.Stereo, true, 0f);
private IPlayingAudioStream? _ambientStream;
public override void Initialize()
{
base.Initialize();
_audioSystem = EntitySystemManager.GetEntitySystem<AudioSystem>();
_ambientCollection = _prototypeManager.Index<SoundCollectionPrototype>("AmbienceBase");
_configManager.OnValueChanged(CCVars.AmbienceBasicEnabled, HandleAmbience, true);
}
private void HandleAmbience(bool ambienceEnabled)
{
if (ambienceEnabled)
{
var file = _robustRandom.Pick(_ambientCollection.PickFiles);
_ambientStream = _audioSystem.Play(file, _ambientParams);
}
else if (_ambientStream != null)
{
_ambientStream.Stop();
_ambientStream = null;
}
}
}
}

View File

@@ -1,4 +1,5 @@
using Content.Client.UserInterface.Stylesheets;
using Content.Client.UserInterface.Stylesheets;
using Content.Shared;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
@@ -19,6 +20,7 @@ namespace Content.Client.UserInterface
private readonly Button ApplyButton;
private readonly Label MasterVolumeLabel;
private readonly Slider MasterVolumeSlider;
private readonly CheckBox AmbienceCheckBox;
private readonly Button ResetButton;
public AudioControl(IConfigurationManager cfg, IClydeAudio clydeAudio)
@@ -63,6 +65,11 @@ namespace Content.Client.UserInterface
}
});
// sets up ambience checkbox. i am sorry for not fixing the rest of this code.
AmbienceCheckBox = new CheckBox { Text = Loc.GetString("Ambient Hum") };
contents.AddChild(AmbienceCheckBox);
AmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.AmbienceBasicEnabled);
ApplyButton = new Button
{
Text = Loc.GetString("Apply"), TextAlign = Label.AlignMode.Center,
@@ -112,6 +119,7 @@ namespace Content.Client.UserInterface
ApplyButton.OnPressed += OnApplyButtonPressed;
ResetButton.OnPressed += OnResetButtonPressed;
MasterVolumeSlider.OnValueChanged += OnMasterVolumeSliderChanged;
AmbienceCheckBox.OnToggled += OnAmbienceCheckToggled;
AddChild(vBox);
UpdateChanges();
@@ -122,6 +130,7 @@ namespace Content.Client.UserInterface
ApplyButton.OnPressed -= OnApplyButtonPressed;
ResetButton.OnPressed -= OnResetButtonPressed;
MasterVolumeSlider.OnValueChanged -= OnMasterVolumeSliderChanged;
AmbienceCheckBox.OnToggled -= OnAmbienceCheckToggled;
base.Dispose(disposing);
}
@@ -132,9 +141,15 @@ namespace Content.Client.UserInterface
UpdateChanges();
}
private void OnAmbienceCheckToggled(BaseButton.ButtonEventArgs args)
{
UpdateChanges();
}
private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
{
_cfg.SetCVar(CVars.AudioMasterVolume, MasterVolumeSlider.Value / 100.0f);
_cfg.SetCVar(CCVars.AmbienceBasicEnabled, AmbienceCheckBox.Pressed);
_cfg.SaveToFile();
UpdateChanges();
}
@@ -143,13 +158,15 @@ namespace Content.Client.UserInterface
{
MasterVolumeSlider.Value = _cfg.GetCVar(CVars.AudioMasterVolume) * 100.0f;
MasterVolumeLabel.Text = string.Format(Loc.GetString("{0:0}%"), MasterVolumeSlider.Value);
AmbienceCheckBox.Pressed = _cfg.GetCVar(CCVars.AmbienceBasicEnabled);
UpdateChanges();
}
private void UpdateChanges()
{
var isMasterVolumeSame = System.Math.Abs(MasterVolumeSlider.Value - _cfg.GetCVar(CVars.AudioMasterVolume) * 100.0f) < 0.01f;
var isEverythingSame = isMasterVolumeSame;
var isAmbienceSame = AmbienceCheckBox.Pressed == _cfg.GetCVar(CCVars.AmbienceBasicEnabled);
var isEverythingSame = isMasterVolumeSame && isAmbienceSame;
ApplyButton.Disabled = isEverythingSame;
ResetButton.Disabled = isEverythingSame;
}

View File

@@ -166,6 +166,14 @@ namespace Content.Shared
CVarDef.Create("parallax.debug", false, CVar.CLIENTONLY);
/*
* Ambience
*/
public static readonly CVarDef<bool> AmbienceBasicEnabled =
CVarDef.Create("ambience.basicenabled", true, CVar.ARCHIVE | CVar.CLIENTONLY);
/*
* AI
*/

View File

@@ -0,0 +1 @@
shipambience.ogg from /tg/station (commit https://github.com/tgstation/tgstation/blob/66a625e6df15eaa97e599248a2281c74238ce26e/sound/ambience/shipambience.ogg), which took it from CEV Eris (commit https://github.com/discordia-space/CEV-Eris/blob/e4e40d38424afe88c8a81cf0e3857d8af4ef077f/sound/ambience/shipambience.ogg). Licensed under CC-BY-SA.

Binary file not shown.

View File

@@ -0,0 +1,4 @@
- type: soundCollection
id: AmbienceBase
files:
- /Audio/Ambience/shipambience.ogg