Files

79 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-01-31 20:07:06 +06:00
using Content.Shared.Mobs;
using Robust.Shared.Audio;
2024-01-13 12:24:00 +03:00
using Robust.Shared.Audio.Systems;
2024-01-17 19:34:15 +07:00
using Robust.Shared.Player;
namespace Content.Server._White.Other.DeathGasps;
public sealed class OnDeath : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
SubscribeLocalEvent<DeathGaspsComponent, MobStateChangedEvent>(HandleDeathEvent);
SubscribeLocalEvent<DeathGaspsComponent, PlayerDetachedEvent>(OnDetach);
}
2024-01-13 12:24:00 +03:00
private readonly Dictionary<EntityUid, EntityUid> _playingStreams = new();
private void HandleDeathEvent(EntityUid uid, DeathGaspsComponent component, MobStateChangedEvent args)
{
//^.^
switch (args.NewMobState)
{
case MobState.Invalid:
StopPlayingStream(uid);
break;
case MobState.Alive:
StopPlayingStream(uid);
break;
case MobState.Critical:
PlayPlayingStream(uid, component);
break;
case MobState.Dead:
StopPlayingStream(uid);
PlayDeathSound(uid, component);
break;
}
}
private void PlayPlayingStream(EntityUid uid, DeathGaspsComponent component)
{
if (_playingStreams.TryGetValue(uid, out var currentStream))
{
2024-01-13 12:24:00 +03:00
_audio.Stop(currentStream);
}
var newStream = _audio.PlayEntity(component.HeartSounds, uid, uid, AudioParams.Default.WithLoop(true));
if (newStream.HasValue)
{
_playingStreams[uid] = newStream.Value.Entity;
}
}
private void StopPlayingStream(EntityUid uid)
{
2024-01-31 20:07:06 +06:00
if (!_playingStreams.TryGetValue(uid, out var currentStream))
return;
2024-01-31 20:07:06 +06:00
_audio.Stop(currentStream);
_playingStreams.Remove(uid);
2024-01-17 19:34:15 +07:00
}
private void PlayDeathSound(EntityUid uid, DeathGaspsComponent component)
2024-01-17 19:34:15 +07:00
{
if (component.CanOtherHearDeathSound)
_audio.PlayPvs(component.DeathSounds, uid, AudioParams.Default);
else
_audio.PlayEntity(component.DeathSounds, uid, uid, AudioParams.Default);
2024-01-17 19:34:15 +07:00
}
private void OnDetach(EntityUid uid, DeathGaspsComponent component, PlayerDetachedEvent args)
2024-01-17 19:34:15 +07:00
{
StopPlayingStream(args.Entity);
}
}