Add Ability to stop sound when MobState is Dead (#26905)

* Add stopsWhenEntityDead to sound components

* Convert component

* Review

* Fix dupe sub

---------

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
GreaseMonk
2024-04-14 05:12:38 +02:00
committed by GitHub
parent dc19964d84
commit da618d791a
5 changed files with 32 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems; using Content.Server.Power.EntitySystems;
using Content.Shared.Audio; using Content.Shared.Audio;
using Content.Shared.Mobs;
namespace Content.Server.Audio; namespace Content.Server.Audio;

View File

@@ -40,7 +40,6 @@ public sealed class EmitSoundSystem : SharedEmitSoundSystem
SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger); SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger);
SubscribeLocalEvent<EmitSoundOnUIOpenComponent, AfterActivatableUIOpenEvent>(HandleEmitSoundOnUIOpen); SubscribeLocalEvent<EmitSoundOnUIOpenComponent, AfterActivatableUIOpenEvent>(HandleEmitSoundOnUIOpen);
SubscribeLocalEvent<SpamEmitSoundComponent, MapInitEvent>(HandleSpamEmitSoundMapInit); SubscribeLocalEvent<SpamEmitSoundComponent, MapInitEvent>(HandleSpamEmitSoundMapInit);
} }

View File

@@ -1,3 +1,4 @@
using Content.Shared.Mobs;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
@@ -12,6 +13,7 @@ public abstract class SharedAmbientSoundSystem : EntitySystem
base.Initialize(); base.Initialize();
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState); SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState); SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
_query = GetEntityQuery<AmbientSoundComponent>(); _query = GetEntityQuery<AmbientSoundComponent>();
} }

View File

@@ -0,0 +1,10 @@
using Content.Shared.Sound.Components;
using Robust.Shared.GameStates;
namespace Content.Shared.Audio;
/// <summary>
/// Toggles <see cref="AmbientSoundComponent"/> and <see cref="SpamEmitSoundComponent"/> off when this entity's MobState isn't Alive.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class SoundWhileAliveComponent : Component;

View File

@@ -1,7 +1,10 @@
using Content.Shared.Audio;
using Content.Shared.Hands; using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Interaction.Events; using Content.Shared.Interaction.Events;
using Content.Shared.Maps; using Content.Shared.Maps;
using Content.Shared.Mobs;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Sound.Components; using Content.Shared.Sound.Components;
using Content.Shared.Throwing; using Content.Shared.Throwing;
@@ -28,6 +31,7 @@ public abstract class SharedEmitSoundSystem : EntitySystem
[Dependency] private readonly INetManager _netMan = default!; [Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!; [Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
[Dependency] protected readonly IRobustRandom Random = default!; [Dependency] protected readonly IRobustRandom Random = default!;
[Dependency] private readonly SharedAmbientSoundSystem _ambient = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!; [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!; [Dependency] protected readonly SharedPopupSystem Popup = default!;
@@ -44,6 +48,20 @@ public abstract class SharedEmitSoundSystem : EntitySystem
SubscribeLocalEvent<EmitSoundOnInteractUsingComponent, InteractUsingEvent>(OnEmitSoundOnInteractUsing); SubscribeLocalEvent<EmitSoundOnInteractUsingComponent, InteractUsingEvent>(OnEmitSoundOnInteractUsing);
SubscribeLocalEvent<EmitSoundOnCollideComponent, StartCollideEvent>(OnEmitSoundOnCollide); SubscribeLocalEvent<EmitSoundOnCollideComponent, StartCollideEvent>(OnEmitSoundOnCollide);
SubscribeLocalEvent<SoundWhileAliveComponent, MobStateChangedEvent>(OnMobState);
}
private void OnMobState(Entity<SoundWhileAliveComponent> entity, ref MobStateChangedEvent args)
{
// Disable this component rather than removing it because it can be brought back to life.
if (TryComp<SpamEmitSoundComponent>(entity, out var comp))
{
comp.Enabled = args.NewMobState == MobState.Alive;
Dirty(entity.Owner, comp);
}
_ambient.SetAmbience(entity.Owner, args.NewMobState != MobState.Dead);
} }
private void OnEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, MapInitEvent args) private void OnEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, MapInitEvent args)