Files
tbd-station-14/Content.Client/Camera/CameraRecoilSystem.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

53 lines
1.4 KiB
C#

using System.Numerics;
using Content.Shared.Camera;
using Content.Shared.CCVar;
using Robust.Shared.Configuration;
namespace Content.Client.Camera;
public sealed class CameraRecoilSystem : SharedCameraRecoilSystem
{
[Dependency] private readonly IConfigurationManager _configManager = default!;
private float _intensity;
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<CameraKickEvent>(OnCameraKick);
Subs.CVar(_configManager, CCVars.ScreenShakeIntensity, OnCvarChanged, true);
}
private void OnCvarChanged(float value)
{
_intensity = value;
}
private void OnCameraKick(CameraKickEvent ev)
{
KickCamera(GetEntity(ev.NetEntity), ev.Recoil);
}
public override void KickCamera(EntityUid uid, Vector2 recoil, CameraRecoilComponent? component = null)
{
if (_intensity == 0)
return;
if (!Resolve(uid, ref component, false))
return;
recoil *= _intensity;
// Use really bad math to "dampen" kicks when we're already kicked.
var existing = component.CurrentKick.Length();
var dampen = existing / KickMagnitudeMax;
component.CurrentKick += recoil * (1 - dampen);
if (component.CurrentKick.Length() > KickMagnitudeMax)
component.CurrentKick = component.CurrentKick.Normalized() * KickMagnitudeMax;
component.LastKickTime = 0;
}
}