Files
RobustToolbox/Robust.Client/GameObjects/EntitySystems/EyeUpdateSystem.cs
T
Pieter-Jan BriersandGitHub 31fb65bcb9 Movement prediction work. (#1144)
* Fix client-side input system not sending sequence numbers for input messages.

This caused message ordering issues and keys getting stuck down.

* Make sure EyeUpdateSystem runs after physics.

* IGameTiming.TickFraction helper.

* Subtick data for input commands.

* Literally a unit test to verify that I wasn't going insane while debugging input message ordering issues.

* More prediction logs behind net.predict.

* Move physics to shared and run it on the client.

* Synchronize grid gravity.

* Fix ResetPredictedEntities crash when entities get deleted server-side.

* Fix CollidableComponent setters not calling Dirty()

* Fix going into space while predicting.

* Watch window uses history line edit.

* Fix unpredicted objects stuck-jittering.

* VV tags for interp vars on transform.

* Fix 0 mass objects on client.

* Fix friction calculations being TPS dependent.

* Reset predict flag on handelComponentState
2020-06-24 20:47:20 +02:00

101 lines
3.4 KiB
C#

using System;
using JetBrains.Annotations;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Physics;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
#nullable enable
namespace Robust.Client.GameObjects.EntitySystems
{
/// <summary>
/// Updates the position of every Eye every frame, so that the camera follows the player around.
/// </summary>
[UsedImplicitly]
internal class EyeUpdateSystem : EntitySystem
{
// How fast the camera rotates in radians
private const float CameraRotateSpeed = MathF.PI;
private const float CameraSnapTolerance = 0.01f;
#pragma warning disable 649, CS8618
// ReSharper disable once NotNullMemberIsNotInitialized
[Dependency] private readonly IEyeManager _eyeManager;
#pragma warning restore 649, CS8618
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
EntityQuery = new TypeEntityQuery(typeof(EyeComponent));
//WARN: Tightly couples this system with InputSystem, and assumes InputSystem exists and is initialized
CommandBinds.Builder
.Bind(EngineKeyFunctions.CameraRotateRight, new NullInputCmdHandler())
.Bind(EngineKeyFunctions.CameraRotateLeft, new NullInputCmdHandler())
.Register<EyeUpdateSystem>();
// Make sure this runs *after* entities have been moved by interpolation and movement.
UpdatesAfter.Add(typeof(TransformSystem));
UpdatesAfter.Add(typeof(PhysicsSystem));
}
/// <inheritdoc />
public override void Shutdown()
{
//WARN: Tightly couples this system with InputSystem, and assumes InputSystem exists and is initialized
CommandBinds.Unregister<EyeUpdateSystem>();
base.Shutdown();
}
/// <inheritdoc />
public override void FrameUpdate(float frameTime)
{
var currentEye = _eyeManager.CurrentEye;
var inputSystem = EntitySystemManager.GetEntitySystem<InputSystem>();
var direction = 0;
if (inputSystem.CmdStates[EngineKeyFunctions.CameraRotateRight] == BoundKeyState.Down)
{
direction += 1;
}
if (inputSystem.CmdStates[EngineKeyFunctions.CameraRotateLeft] == BoundKeyState.Down)
{
direction -= 1;
}
// apply camera rotation
if(direction != 0)
{
currentEye.Rotation += CameraRotateSpeed * frameTime * direction;
currentEye.Rotation = currentEye.Rotation.Reduced();
}
else
{
// snap to cardinal directions
var closestDir = currentEye.Rotation.GetCardinalDir().ToVec();
var currentDir = currentEye.Rotation.ToVec();
var dot = Vector2.Dot(closestDir, currentDir);
if (FloatMath.CloseTo(dot, 1, CameraSnapTolerance))
{
currentEye.Rotation = closestDir.ToAngle();
}
}
foreach (var entity in RelevantEntities)
{
var eyeComp = entity.GetComponent<EyeComponent>();
eyeComp.UpdateEyePosition();
}
}
}
}