Files
OldThink/Content.Client/Eye/EyeLerpingSystem.cs

177 lines
6.0 KiB
C#
Raw Normal View History

2023-04-23 20:01:15 +10:00
using Content.Shared.Follower.Components;
using Content.Shared.Movement.Components;
2022-08-29 15:05:53 +10:00
using Content.Shared.Movement.Systems;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Physics;
using Robust.Client.Player;
2022-08-29 15:05:53 +10:00
using Robust.Shared.Collections;
using Robust.Shared.Timing;
namespace Content.Client.Eye;
public sealed class EyeLerpingSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
2022-08-29 15:05:53 +10:00
[Dependency] private readonly SharedMoverController _mover = default!;
2022-09-21 22:40:24 +12:00
// Convenience variable for for VV.
[ViewVariables]
private IEnumerable<LerpingEyeComponent> ActiveEyes => EntityQuery<LerpingEyeComponent>();
public override void Initialize()
{
base.Initialize();
2022-08-29 15:05:53 +10:00
SubscribeLocalEvent<EyeComponent, ComponentStartup>(OnEyeStartup);
SubscribeLocalEvent<EyeComponent, ComponentShutdown>(OnEyeShutdown);
2022-09-21 22:40:24 +12:00
SubscribeLocalEvent<EyeComponent, PlayerAttachedEvent>(OnAttached);
2023-04-23 20:01:15 +10:00
SubscribeLocalEvent<LerpingEyeComponent, EntParentChangedMessage>(HandleMapChange);
2022-09-21 22:40:24 +12:00
SubscribeLocalEvent<LerpingEyeComponent, PlayerDetachedEvent>(OnDetached);
UpdatesAfter.Add(typeof(TransformSystem));
UpdatesAfter.Add(typeof(PhysicsSystem));
UpdatesBefore.Add(typeof(EyeUpdateSystem));
UpdatesOutsidePrediction = true;
}
2022-08-29 15:05:53 +10:00
private void OnEyeStartup(EntityUid uid, EyeComponent component, ComponentStartup args)
{
2022-09-21 22:40:24 +12:00
if (_playerManager.LocalPlayer?.ControlledEntity == uid)
AddEye(uid, component, true);
}
private void OnEyeShutdown(EntityUid uid, EyeComponent component, ComponentShutdown args)
{
RemCompDeferred<LerpingEyeComponent>(uid);
}
// TODO replace this with some way of automatically getting and including any eyes that are associated with a viewport / render able thingy.
public void AddEye(EntityUid uid, EyeComponent? component = null, bool automatic = false)
{
if (!Resolve(uid, ref component))
2022-08-29 15:05:53 +10:00
return;
2022-09-21 22:40:24 +12:00
var lerpInfo = EnsureComp<LerpingEyeComponent>(uid);
lerpInfo.TargetRotation = GetRotation(uid);
2022-08-29 15:05:53 +10:00
lerpInfo.LastRotation = lerpInfo.TargetRotation;
2022-09-21 22:40:24 +12:00
lerpInfo.ManuallyAdded |= !automatic;
2022-08-29 15:05:53 +10:00
2022-09-21 22:40:24 +12:00
if (component.Eye != null)
component.Eye.Rotation = lerpInfo.TargetRotation;
}
public void RemoveEye(EntityUid uid)
{
if (!TryComp(uid, out LerpingEyeComponent? lerp))
return;
2022-08-29 15:05:53 +10:00
2022-09-21 22:40:24 +12:00
// If this is the currently controlled entity, we keep the component.
if (_playerManager.LocalPlayer?.ControlledEntity == uid)
lerp.ManuallyAdded = false;
else
RemComp(uid, lerp);
2022-08-29 15:05:53 +10:00
}
2022-09-21 22:40:24 +12:00
private void HandleMapChange(EntityUid uid, LerpingEyeComponent component, ref EntParentChangedMessage args)
{
2022-09-21 22:40:24 +12:00
// Is this actually a map change? If yes, stop any lerps
if (args.OldMapId != args.Transform.MapID)
component.LastRotation = GetRotation(uid, args.Transform);
}
2022-09-21 22:40:24 +12:00
private void OnAttached(EntityUid uid, EyeComponent component, PlayerAttachedEvent args)
{
2022-09-21 22:40:24 +12:00
AddEye(uid, component, true);
}
2022-09-21 22:40:24 +12:00
private void OnDetached(EntityUid uid, LerpingEyeComponent component, PlayerDetachedEvent args)
{
2022-09-21 22:40:24 +12:00
if (!component.ManuallyAdded)
RemCompDeferred(uid, component);
}
2022-08-29 15:05:53 +10:00
public override void Update(float frameTime)
{
2022-08-29 15:05:53 +10:00
base.Update(frameTime);
if (!_gameTiming.IsFirstTimePredicted)
return;
2022-08-29 15:05:53 +10:00
// Set all of our eye rotations to the relevant values.
2023-03-10 18:12:21 +11:00
var query = AllEntityQuery<LerpingEyeComponent, TransformComponent>();
while (query.MoveNext(out var uid, out var lerpInfo, out var xform))
{
2022-09-21 10:12:44 +10:00
lerpInfo.LastRotation = lerpInfo.TargetRotation;
2023-03-10 18:12:21 +11:00
lerpInfo.TargetRotation = GetRotation(uid, xform);
}
}
2022-09-21 10:12:44 +10:00
/// <summary>
/// Does the eye need to lerp or is its rotation matched.
/// </summary>
2022-09-21 22:40:24 +12:00
private bool NeedsLerp(InputMoverComponent? mover)
2022-09-21 10:12:44 +10:00
{
if (mover == null)
2022-09-21 22:40:24 +12:00
return false;
2022-09-21 10:12:44 +10:00
if (mover.RelativeRotation.Equals(mover.TargetRelativeRotation))
return false;
return true;
}
2022-09-21 22:40:24 +12:00
private Angle GetRotation(EntityUid uid, TransformComponent? xform = null, InputMoverComponent? mover = null)
{
2022-09-21 22:40:24 +12:00
if (!Resolve(uid, ref xform))
return Angle.Zero;
2022-08-29 15:05:53 +10:00
// If we can move then tie our eye to our inputs (these also get lerped so it should be fine).
2022-09-21 22:40:24 +12:00
if (Resolve(uid, ref mover, false))
2022-08-29 15:05:53 +10:00
{
return -_mover.GetParentGridAngle(mover);
}
2022-08-29 15:05:53 +10:00
// if not tied to a mover then lock it to map / grid
2022-09-21 22:40:24 +12:00
var relative = xform.GridUid ?? xform.MapUid;
if (relative != null)
return -Transform(relative.Value).WorldRotation;
2022-08-29 15:05:53 +10:00
return Angle.Zero;
}
2022-08-29 15:05:53 +10:00
public override void FrameUpdate(float frameTime)
{
var tickFraction = (float) _gameTiming.TickFraction / ushort.MaxValue;
2022-09-11 07:51:41 +10:00
const double lerpMinimum = 0.00001;
2023-03-10 18:12:21 +11:00
var query = AllEntityQuery<LerpingEyeComponent, EyeComponent, TransformComponent>();
2023-03-10 18:12:21 +11:00
while (query.MoveNext(out var entity, out var lerpInfo, out var eye, out var xform))
2022-08-29 15:05:53 +10:00
{
2022-09-21 22:40:24 +12:00
TryComp<InputMoverComponent>(entity, out var mover);
2022-09-21 22:40:24 +12:00
// This needs to be recomputed every frame, as if this is simply the grid rotation, then we need to account for grid angle lerping.
lerpInfo.TargetRotation = GetRotation(entity, xform, mover);
if (!NeedsLerp(mover))
2022-09-21 10:12:44 +10:00
{
2022-09-21 22:40:24 +12:00
eye.Rotation = lerpInfo.TargetRotation;
2022-09-21 10:12:44 +10:00
continue;
}
2022-08-29 15:05:53 +10:00
var shortest = Angle.ShortestDistance(lerpInfo.LastRotation, lerpInfo.TargetRotation);
if (Math.Abs(shortest.Theta) < lerpMinimum)
{
2022-08-29 15:05:53 +10:00
eye.Rotation = lerpInfo.TargetRotation;
continue;
}
2022-08-29 15:05:53 +10:00
eye.Rotation = shortest * tickFraction + lerpInfo.LastRotation;
}
}
}