1
0

SharedAmbientSoundSystem.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Content.Shared.Mobs;
  2. using Robust.Shared.Audio;
  3. using Robust.Shared.GameStates;
  4. namespace Content.Shared.Audio;
  5. public abstract class SharedAmbientSoundSystem : EntitySystem
  6. {
  7. private EntityQuery<AmbientSoundComponent> _query;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
  12. SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
  13. _query = GetEntityQuery<AmbientSoundComponent>();
  14. }
  15. public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
  16. {
  17. if (!_query.Resolve(uid, ref ambience, false) || ambience.Enabled == value)
  18. return;
  19. ambience.Enabled = value;
  20. QueueUpdate(uid, ambience);
  21. Dirty(uid, ambience);
  22. }
  23. public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
  24. {
  25. if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
  26. return;
  27. ambience.Range = value;
  28. QueueUpdate(uid, ambience);
  29. Dirty(uid, ambience);
  30. }
  31. protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
  32. {
  33. // client side tree
  34. }
  35. public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
  36. {
  37. if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
  38. return;
  39. ambience.Volume = value;
  40. Dirty(uid, ambience);
  41. }
  42. public virtual void SetSound(EntityUid uid, SoundSpecifier sound, AmbientSoundComponent? ambience = null)
  43. {
  44. if (!_query.Resolve(uid, ref ambience, false) || ambience.Sound == sound)
  45. return;
  46. ambience.Sound = sound;
  47. QueueUpdate(uid, ambience);
  48. Dirty(uid, ambience);
  49. }
  50. private void HandleCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentHandleState args)
  51. {
  52. if (args.Current is not AmbientSoundComponentState state) return;
  53. SetAmbience(uid, state.Enabled, component);
  54. SetRange(uid, state.Range, component);
  55. SetVolume(uid, state.Volume, component);
  56. SetSound(uid, state.Sound, component);
  57. }
  58. private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)
  59. {
  60. args.State = new AmbientSoundComponentState
  61. {
  62. Enabled = component.Enabled,
  63. Range = component.Range,
  64. Volume = component.Volume,
  65. Sound = component.Sound,
  66. };
  67. }
  68. }