Mouse rotator system (#19267)
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
61
Content.Client/MouseRotator/MouseRotatorSystem.cs
Normal file
61
Content.Client/MouseRotator/MouseRotatorSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Content.Shared.MouseRotator;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.MouseRotator;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public sealed class MouseRotatorSystem : SharedMouseRotatorSystem
|
||||
{
|
||||
[Dependency] private readonly IInputManager _input = default!;
|
||||
[Dependency] private readonly IPlayerManager _player = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly IEyeManager _eye = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!_timing.IsFirstTimePredicted || !_input.MouseScreenPosition.IsValid)
|
||||
return;
|
||||
|
||||
var player = _player.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (player == null || !TryComp<MouseRotatorComponent>(player, out var rotator))
|
||||
return;
|
||||
|
||||
var xform = Transform(player.Value);
|
||||
|
||||
// Get mouse loc and convert to angle based on player location
|
||||
var coords = _input.MouseScreenPosition;
|
||||
var mapPos = _eye.PixelToMap(coords);
|
||||
|
||||
if (mapPos.MapId == MapId.Nullspace)
|
||||
return;
|
||||
|
||||
var angle = (mapPos.Position - xform.MapPosition.Position).ToWorldAngle();
|
||||
|
||||
var curRot = _transform.GetWorldRotation(xform);
|
||||
|
||||
// Don't raise event if mouse ~hasn't moved (or if too close to goal rotation already)
|
||||
var diff = Angle.ShortestDistance(angle, curRot);
|
||||
if (Math.Abs(diff.Theta) < rotator.AngleTolerance.Theta)
|
||||
return;
|
||||
|
||||
if (rotator.GoalRotation != null)
|
||||
{
|
||||
var goalDiff = Angle.ShortestDistance(angle, rotator.GoalRotation.Value);
|
||||
if (Math.Abs(goalDiff.Theta) < rotator.AngleTolerance.Theta)
|
||||
return;
|
||||
}
|
||||
|
||||
RaisePredictiveEvent(new RequestMouseRotatorRotationEvent
|
||||
{
|
||||
Rotation = angle
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user