Mouse rotator system (#19267)
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
43
Content.Shared/MouseRotator/MouseRotatorComponent.cs
Normal file
43
Content.Shared/MouseRotator/MouseRotatorComponent.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Numerics;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.MouseRotator;
|
||||
|
||||
/// <summary>
|
||||
/// This component allows overriding an entities local rotation based on the client's mouse movement
|
||||
/// </summary>
|
||||
/// <see cref="SharedMouseRotatorSystem"/>
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||
public sealed partial class MouseRotatorComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// How much the desired angle needs to change before a predictive event is sent
|
||||
/// </summary>
|
||||
[DataField]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public Angle AngleTolerance = Angle.FromDegrees(5.0);
|
||||
|
||||
/// <summary>
|
||||
/// The angle that will be lerped to
|
||||
/// </summary>
|
||||
[AutoNetworkedField, DataField]
|
||||
public Angle? GoalRotation;
|
||||
|
||||
/// <summary>
|
||||
/// Max degrees the entity can rotate per second
|
||||
/// </summary>
|
||||
[DataField]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public double RotationSpeed = float.MaxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised on an entity with <see cref="MouseRotatorComponent"/> as a predictive event on the client
|
||||
/// when mouse rotation changes
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class RequestMouseRotatorRotationEvent : EntityEventArgs
|
||||
{
|
||||
public Angle Rotation;
|
||||
}
|
||||
60
Content.Shared/MouseRotator/SharedMouseRotatorSystem.cs
Normal file
60
Content.Shared/MouseRotator/SharedMouseRotatorSystem.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.MouseRotator;
|
||||
|
||||
/// <summary>
|
||||
/// This handles rotating an entity based on mouse location
|
||||
/// </summary>
|
||||
/// <see cref="MouseRotatorComponent"/>
|
||||
public abstract class SharedMouseRotatorSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly RotateToFaceSystem _rotate = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeAllEvent<RequestMouseRotatorRotationEvent>(OnRequestRotation);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
// TODO maybe `ActiveMouseRotatorComponent` to avoid querying over more entities than we need?
|
||||
// (if this is added to players)
|
||||
// (but arch makes these fast anyway, so)
|
||||
var query = EntityQueryEnumerator<MouseRotatorComponent, TransformComponent>();
|
||||
while (query.MoveNext(out var uid, out var rotator, out var xform))
|
||||
{
|
||||
if (rotator.GoalRotation == null)
|
||||
continue;
|
||||
|
||||
if (_rotate.TryRotateTo(
|
||||
uid,
|
||||
rotator.GoalRotation.Value,
|
||||
frameTime,
|
||||
rotator.AngleTolerance,
|
||||
MathHelper.DegreesToRadians(rotator.RotationSpeed),
|
||||
xform))
|
||||
{
|
||||
// Stop rotating if we finished
|
||||
rotator.GoalRotation = null;
|
||||
Dirty(uid, rotater);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRequestRotation(RequestMouseRotatorRotationEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (args.SenderSession.AttachedEntity is not { } ent || !TryComp<MouseRotatorComponent>(ent, out var rotator))
|
||||
{
|
||||
Log.Error($"User {args.SenderSession.Name} ({args.SenderSession.UserId}) tried setting local rotation without a mouse rotator component attached!");
|
||||
return;
|
||||
}
|
||||
|
||||
rotator.GoalRotation = msg.Rotation;
|
||||
Dirty(ent, rotator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user