CatStation/Content.Shared/Audio/SharedAmbientSoundSystem.cs
ArtisticRoomba 376f16504c
Content.Shared EntityQuery<T> dependency injection refactor (#43498)
* part 1

* second pass

* final fix

* last fix i swear

* an even bigger genius

* regex missed this

* should be all of em?

* oops

* yeah

* Revert "should be all of em?"

This reverts commit 4dd54d1b9a76977b4cf8d6008358e4dacdee6eb1.

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2026-04-11 21:30:36 +00:00

81 lines
2.6 KiB
C#

using Content.Shared.Mobs;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Audio;
public abstract class SharedAmbientSoundSystem : EntitySystem
{
[Dependency] private readonly EntityQuery<AmbientSoundComponent> _query = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
}
public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
{
if (!_query.Resolve(uid, ref ambience, false) || ambience.Enabled == value)
return;
ambience.Enabled = value;
QueueUpdate(uid, ambience);
Dirty(uid, ambience);
}
public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
return;
ambience.Range = value;
QueueUpdate(uid, ambience);
Dirty(uid, ambience);
}
protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
{
// client side tree
}
public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
return;
ambience.Volume = value;
Dirty(uid, ambience);
}
public virtual void SetSound(EntityUid uid, SoundSpecifier sound, AmbientSoundComponent? ambience = null)
{
if (!_query.Resolve(uid, ref ambience, false) || ambience.Sound == sound)
return;
ambience.Sound = sound;
QueueUpdate(uid, ambience);
Dirty(uid, ambience);
}
private void HandleCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentHandleState args)
{
if (args.Current is not AmbientSoundComponentState state) return;
SetAmbience(uid, state.Enabled, component);
SetRange(uid, state.Range, component);
SetVolume(uid, state.Volume, component);
SetSound(uid, state.Sound, component);
}
private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)
{
args.State = new AmbientSoundComponentState
{
Enabled = component.Enabled,
Range = component.Range,
Volume = component.Volume,
Sound = component.Sound,
};
}
}