Files

218 lines
7.3 KiB
C#
Raw Permalink Normal View History

2023-08-25 14:41:04 +10:00
using System.Numerics;
using Content.Shared.Movement.Components;
2022-08-29 15:05:53 +10:00
using Content.Shared.Movement.Systems;
2023-08-25 14:41:04 +10:00
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Physics;
using Robust.Client.Player;
using Robust.Shared.Player;
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!;
[Dependency] private readonly SharedEyeSystem _eye = default!;
2022-08-29 15:05:53 +10:00
[Dependency] private readonly SharedMoverController _mover = default!;
2023-08-25 14:41:04 +10:00
[Dependency] private readonly SharedTransformSystem _transform = default!;
2022-09-21 22:40:24 +12:00
// Convenience variable for for VV.
2023-08-25 14:41:04 +10:00
[ViewVariables, UsedImplicitly]
2022-09-21 22:40:24 +12:00
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);
SubscribeLocalEvent<EyeAttachedEvent>(OnAttached);
2023-04-23 20:01:15 +10:00
SubscribeLocalEvent<LerpingEyeComponent, EntParentChangedMessage>(HandleMapChange);
SubscribeLocalEvent<LerpingEyeComponent, LocalPlayerDetachedEvent>(OnDetached);
UpdatesAfter.Add(typeof(TransformSystem));
Mob Movement Major Refactor (#36847) * Conveyor optimisations - Optimise movement for moving stuff. Better flags + less resolves + slapped parallelrobustjob on it. - Sleeping for entities getting conveyed into walls. * Blocker version * Finish * Final * Fix conveyor power mispredict * Bagel save * Revert "Bagel save" This reverts commit 1b93fda81fb852d89b89b0beae0b80f8a61165f2. * Conveyor resave * Init Commit * windows yelling at me to update commit * working commit, need prediciton and more dehardcoding * Project 0 warnings * Working Commit (Near Final) * ryder got confused commit * I love Merge Conflicts :) * Working commit, no prediction * Forgot the yaml changes * Comments and typos * Apparently while the reduced launch mult of lube was initialized it was never used so I revered back to default * Fixed an incorrect divisor * bit of cleanup * Prediciton fixed, and puddles now affect all entities * FORGOT TO RENAME A VERY IMPORTANT VARIABLE OOPS * Really big I forgor moment * Even bigger I forgor moment * four more merge conflicts to fix four more oopsies * fixed actual divide by zero moment and also im very dumb * Even bigger I forgor moment * four more merge conflicts to fix four more oopsies * fixed actual divide by zero moment and also im very dumb * Fix all test fails * code cleanup * Webedit whitespace * Code cleaup * whitespace webedit * whitespace webedit * whitespace webedit * whitespace removal * Comments and cleanup * Re-Added 20 warnings as per Ork's request * Cleanups * Spacing fix * bugfixes and cleanup * Small bugfix * Fix prediction * Mob movement rewrite * Bandaid * Working version * Tentatively working * Friction to fix cornering * More fixes * Refactor mob movement Trying to cleanup relay ordering / tryupdaterelative being cooked, purge ToParent, and fix all the eye rotation shenanigans. * Building * Re-implement jetpacks * Reorganise weightless movement * More work * Fix camera * reh * Revert bagel * Revert this * Revert held move buttons * Puddles work but are unpredicted and unoptimized * Fixes * Puddle code... * Actually dirty the slipComp for real * Sliding component done plus an extra suggestion from ArtisticRoomba * Atomized Commit * Added Friction field to Reagent Prototype per design discussion * Cleaned up Working Commit * a * Delete stinkers * Fix this code smell * Reviewed * Funky re-save * Our conveyance * Better conveyor sleeping * Remove this * Revert "Better conveyor sleeping" This reverts commit f5281f64bbae95b7b9feb56295c5cf931f9fb2e1. * Revert that Way too janky * Also this * a * Working Commit - Still a lot to do * Acceleration refactor * Minor jetpack cleanup * frictionnomovement no longer nullable * Shared Mover Feels 99% done * OffGrid/Weightless/Throwing Friction saved * Fix merge conflicts * Fix a debug assert * Final Commit for today * Some fixes * Actually use those CCVars Properly * Need to fix throwing * Second to last Commit for real * Jetpack bug fixed * Jetpack bug fixed * Test fail patch * Small patch * Skates Component cleanup + Bring Accel back to 5 (oops) * Fix test fail oops * yaml cleanup make dragons not fat --------- Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2025-05-02 01:18:08 -07:00
UpdatesAfter.Add(typeof(Robust.Client.Physics.PhysicsSystem));
UpdatesBefore.Add(typeof(SharedEyeSystem));
UpdatesOutsidePrediction = true;
}
2022-08-29 15:05:53 +10:00
private void OnEyeStartup(EntityUid uid, EyeComponent component, ComponentStartup args)
{
if (_playerManager.LocalEntity == uid)
2022-09-21 22:40:24 +12:00
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
2023-08-25 14:41:04 +10:00
lerpInfo.TargetZoom = component.Zoom;
lerpInfo.LastZoom = lerpInfo.TargetZoom;
2022-09-21 22:40:24 +12:00
if (component.Eye != null)
2023-08-25 14:41:04 +10:00
{
_eye.SetRotation(uid, lerpInfo.TargetRotation, component);
_eye.SetZoom(uid, lerpInfo.TargetZoom, component);
2023-08-25 14:41:04 +10:00
}
2022-09-21 22:40:24 +12:00
}
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.LocalEntity == uid)
2022-09-21 22:40:24 +12:00
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.MapUid)
2022-09-21 22:40:24 +12:00
component.LastRotation = GetRotation(uid, args.Transform);
}
private void OnAttached(ref EyeAttachedEvent ev)
{
AddEye(ev.Entity, ev.Component, true);
}
private void OnDetached(EntityUid uid, LerpingEyeComponent component, LocalPlayerDetachedEvent 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);
2023-08-25 14:41:04 +10:00
lerpInfo.LastZoom = lerpInfo.TargetZoom;
lerpInfo.TargetZoom = UpdateZoom(uid, frameTime);
}
}
2023-08-25 14:41:04 +10:00
private Vector2 UpdateZoom(EntityUid uid, float frameTime, EyeComponent? eye = null, ContentEyeComponent? content = null)
{
if (!Resolve(uid, ref content, ref eye, false))
return Vector2.One;
var diff = content.TargetZoom - eye.Zoom;
if (diff.LengthSquared() < 0.00001f)
{
return content.TargetZoom;
}
2023-10-16 16:26:45 +11:00
var change = diff * Math.Min(8f * frameTime, 1);
2023-08-25 14:41:04 +10:00
return eye.Zoom + change;
}
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)
2023-08-25 14:41:04 +10:00
return -_transform.GetWorldRotation(relative.Value);
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
{
2023-08-25 14:41:04 +10:00
// Handle zoom
var zoomDiff = Vector2.Lerp(lerpInfo.LastZoom, lerpInfo.TargetZoom, tickFraction);
if ((zoomDiff - lerpInfo.TargetZoom).Length() < lerpMinimum)
{
_eye.SetZoom(entity, lerpInfo.TargetZoom, eye);
2023-08-25 14:41:04 +10:00
}
else
{
_eye.SetZoom(entity, zoomDiff, eye);
2023-08-25 14:41:04 +10:00
}
// Handle Rotation
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
{
_eye.SetRotation(entity, lerpInfo.TargetRotation, eye);
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)
{
_eye.SetRotation(entity, lerpInfo.TargetRotation, eye);
2022-08-29 15:05:53 +10:00
continue;
}
2022-08-29 15:05:53 +10:00
_eye.SetRotation(entity, shortest * tickFraction + lerpInfo.LastRotation, eye);
}
}
}