Files
tbd-station-14/Content.Client/Traits/ParacusiaSystem.cs
Pieter-Jan Briers 68ce53ae17 Random spontaneous cleanup PR (#25131)
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: 0x6273 <0x40@keemail.me>
2024-02-13 16:48:39 -05:00

76 lines
2.5 KiB
C#

using System.Numerics;
using Content.Shared.Traits.Assorted;
using Robust.Shared.Random;
using Robust.Client.Player;
using Robust.Shared.Player;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Timing;
namespace Content.Client.Traits;
public sealed class ParacusiaSystem : SharedParacusiaSystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ParacusiaComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<ParacusiaComponent, LocalPlayerDetachedEvent>(OnPlayerDetach);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!_timing.IsFirstTimePredicted)
return;
if (_player.LocalEntity is not EntityUid localPlayer)
return;
PlayParacusiaSounds(localPlayer);
}
private void OnComponentStartup(EntityUid uid, ParacusiaComponent component, ComponentStartup args)
{
component.NextIncidentTime = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(component.MinTimeBetweenIncidents, component.MaxTimeBetweenIncidents));
}
private void OnPlayerDetach(EntityUid uid, ParacusiaComponent component, LocalPlayerDetachedEvent args)
{
component.Stream = _audio.Stop(component.Stream);
}
private void PlayParacusiaSounds(EntityUid uid)
{
if (!TryComp<ParacusiaComponent>(uid, out var paracusia))
return;
if (_timing.CurTime <= paracusia.NextIncidentTime)
return;
// Set the new time.
var timeInterval = _random.NextFloat(paracusia.MinTimeBetweenIncidents, paracusia.MaxTimeBetweenIncidents);
paracusia.NextIncidentTime += TimeSpan.FromSeconds(timeInterval);
// Offset position where the sound is played
var randomOffset =
new Vector2
(
_random.NextFloat(-paracusia.MaxSoundDistance, paracusia.MaxSoundDistance),
_random.NextFloat(-paracusia.MaxSoundDistance, paracusia.MaxSoundDistance)
);
var newCoords = Transform(uid).Coordinates.Offset(randomOffset);
// Play the sound
paracusia.Stream = _audio.PlayStatic(paracusia.Sounds, uid, newCoords).Value.Entity;
}
}