Skip to content

Commit

Permalink
Merge pull request #4 from Evgencheg/wizden-mass-pick-2
Browse files Browse the repository at this point in the history
Fix MouseRotator on rotated grids (#29663)
  • Loading branch information
DEATHB4DEFEAT authored Sep 22, 2024
2 parents cb4ca29 + d392e20 commit 351b62e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 32 deletions.
14 changes: 10 additions & 4 deletions Content.Client/MouseRotator/MouseRotatorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ public override void Update(float frameTime)
// only raise event if the cardinal direction has changed
if (rotator.Simple4DirMode)
{
var angleDir = angle.GetCardinalDir();
if (angleDir == curRot.GetCardinalDir())
var eyeRot = _eye.CurrentEye.Rotation; // camera rotation
var angleDir = (angle + eyeRot).GetCardinalDir(); // apply GetCardinalDir in the camera frame, not in the world frame
if (angleDir == (curRot + eyeRot).GetCardinalDir())
return;

RaisePredictiveEvent(new RequestMouseRotatorRotationSimpleEvent()
var rotation = angleDir.ToAngle() - eyeRot; // convert back to world frame
if (rotation >= Math.PI) // convert to [-PI, +PI)
rotation -= 2 * Math.PI;
else if (rotation < -Math.PI)
rotation += 2 * Math.PI;
RaisePredictiveEvent(new RequestMouseRotatorRotationEvent
{
Direction = angleDir,
Rotation = rotation
});

return;
Expand Down
13 changes: 1 addition & 12 deletions Content.Shared/MouseRotator/MouseRotatorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public sealed partial class MouseRotatorComponent : Component
public double RotationSpeed = float.MaxValue;

/// <summary>
/// This one is important. If this is true, <see cref="AngleTolerance"/> does not apply, and the system will
/// use <see cref="RequestMouseRotatorRotationSimpleEvent"/> instead. In this mode, the client will only send
/// This one is important. If this is true, <see cref="AngleTolerance"/> does not apply. In this mode, the client will only send
/// events when an entity should snap to a different cardinal direction, rather than for every angle change.
///
/// This is useful for cases like humans, where what really matters is the visual sprite direction, as opposed to something
Expand All @@ -50,13 +49,3 @@ public sealed class RequestMouseRotatorRotationEvent : EntityEventArgs
{
public Angle Rotation;
}

/// <summary>
/// Simpler version of <see cref="RequestMouseRotatorRotationEvent"/> for implementations
/// that only require snapping to 4-dir and not full angle rotation.
/// </summary>
[Serializable, NetSerializable]
public sealed class RequestMouseRotatorRotationSimpleEvent : EntityEventArgs
{
public Direction Direction;
}
17 changes: 1 addition & 16 deletions Content.Shared/MouseRotator/SharedMouseRotatorSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Content.Shared.Interaction;
using Robust.Shared.Timing;

namespace Content.Shared.MouseRotator;

Expand All @@ -16,7 +15,6 @@ public override void Initialize()
base.Initialize();

SubscribeAllEvent<RequestMouseRotatorRotationEvent>(OnRequestRotation);
SubscribeAllEvent<RequestMouseRotatorRotationSimpleEvent>(OnRequestSimpleRotation);
}

public override void Update(float frameTime)
Expand Down Expand Up @@ -50,7 +48,7 @@ public override void Update(float frameTime)
private void OnRequestRotation(RequestMouseRotatorRotationEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not { } ent
|| !TryComp<MouseRotatorComponent>(ent, out var rotator) || rotator.Simple4DirMode)
|| !TryComp<MouseRotatorComponent>(ent, out var rotator))
{
Log.Error($"User {args.SenderSession.Name} ({args.SenderSession.UserId}) tried setting local rotation directly without a valid mouse rotator component attached!");
return;
Expand All @@ -59,17 +57,4 @@ private void OnRequestRotation(RequestMouseRotatorRotationEvent msg, EntitySessi
rotator.GoalRotation = msg.Rotation;
Dirty(ent, rotator);
}

private void OnRequestSimpleRotation(RequestMouseRotatorRotationSimpleEvent ev, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not { } ent
|| !TryComp<MouseRotatorComponent>(ent, out var rotator) || !rotator.Simple4DirMode)
{
Log.Error($"User {args.SenderSession.Name} ({args.SenderSession.UserId}) tried setting 4-dir rotation directly without a valid mouse rotator component attached!");
return;
}

rotator.GoalRotation = ev.Direction.ToAngle();
Dirty(ent, rotator);
}
}

0 comments on commit 351b62e

Please sign in to comment.