Update MoverController.cs to not use Component.Owner (#29965)

* Update MoverController.cs

* Update a bunch of movement code to use Entity<T>

* Last errors

* wow, there were more errors

---------

Co-authored-by: plykiya <plykiya@protonmail.com>
This commit is contained in:
Plykiya
2024-08-01 18:32:32 -07:00
committed by GitHub
parent dceb562be7
commit e6b67540e4
8 changed files with 119 additions and 121 deletions

View File

@@ -28,7 +28,7 @@ public sealed class SpriteMovementSystem : EntitySystem
return;
var oldMoving = (SharedMoverController.GetNormalizedMovement(args.OldMovement) & MoveButtons.AnyDirection) != MoveButtons.None;
var moving = (SharedMoverController.GetNormalizedMovement(args.Component.HeldMoveButtons) & MoveButtons.AnyDirection) != MoveButtons.None;
var moving = (SharedMoverController.GetNormalizedMovement(args.Entity.Comp.HeldMoveButtons) & MoveButtons.AnyDirection) != MoveButtons.None;
if (oldMoving == moving || !_spriteQuery.TryGetComponent(uid, out var sprite))
return;

View File

@@ -28,57 +28,57 @@ namespace Content.Client.Physics.Controllers
SubscribeLocalEvent<PullableComponent, UpdateIsPredictedEvent>(OnUpdatePullablePredicted);
}
private void OnUpdatePredicted(EntityUid uid, InputMoverComponent component, ref UpdateIsPredictedEvent args)
private void OnUpdatePredicted(Entity<InputMoverComponent> entity, ref UpdateIsPredictedEvent args)
{
// Enable prediction if an entity is controlled by the player
if (uid == _playerManager.LocalEntity)
if (entity.Owner == _playerManager.LocalEntity)
args.IsPredicted = true;
}
private void OnUpdateRelayTargetPredicted(EntityUid uid, MovementRelayTargetComponent component, ref UpdateIsPredictedEvent args)
private void OnUpdateRelayTargetPredicted(Entity<MovementRelayTargetComponent> entity, ref UpdateIsPredictedEvent args)
{
if (component.Source == _playerManager.LocalEntity)
if (entity.Comp.Source == _playerManager.LocalEntity)
args.IsPredicted = true;
}
private void OnUpdatePullablePredicted(EntityUid uid, PullableComponent component, ref UpdateIsPredictedEvent args)
private void OnUpdatePullablePredicted(Entity<PullableComponent> entity, ref UpdateIsPredictedEvent args)
{
// Enable prediction if an entity is being pulled by the player.
// Disable prediction if an entity is being pulled by some non-player entity.
if (component.Puller == _playerManager.LocalEntity)
if (entity.Comp.Puller == _playerManager.LocalEntity)
args.IsPredicted = true;
else if (component.Puller != null)
else if (entity.Comp.Puller != null)
args.BlockPrediction = true;
// TODO recursive pulling checks?
// What if the entity is being pulled by a vehicle controlled by the player?
}
private void OnRelayPlayerAttached(EntityUid uid, RelayInputMoverComponent component, LocalPlayerAttachedEvent args)
private void OnRelayPlayerAttached(Entity<RelayInputMoverComponent> entity, ref LocalPlayerAttachedEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.RelayEntity);
if (MoverQuery.TryGetComponent(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
Physics.UpdateIsPredicted(entity.Owner);
Physics.UpdateIsPredicted(entity.Comp.RelayEntity);
if (MoverQuery.TryGetComponent(entity.Comp.RelayEntity, out var inputMover))
SetMoveInput((entity.Owner, inputMover), MoveButtons.None);
}
private void OnRelayPlayerDetached(EntityUid uid, RelayInputMoverComponent component, LocalPlayerDetachedEvent args)
private void OnRelayPlayerDetached(Entity<RelayInputMoverComponent> entity, ref LocalPlayerDetachedEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.RelayEntity);
if (MoverQuery.TryGetComponent(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
Physics.UpdateIsPredicted(entity.Owner);
Physics.UpdateIsPredicted(entity.Comp.RelayEntity);
if (MoverQuery.TryGetComponent(entity.Comp.RelayEntity, out var inputMover))
SetMoveInput((entity.Owner, inputMover), MoveButtons.None);
}
private void OnPlayerAttached(EntityUid uid, InputMoverComponent component, LocalPlayerAttachedEvent args)
private void OnPlayerAttached(Entity<InputMoverComponent> entity, ref LocalPlayerAttachedEvent args)
{
SetMoveInput(component, MoveButtons.None);
SetMoveInput(entity, MoveButtons.None);
}
private void OnPlayerDetached(EntityUid uid, InputMoverComponent component, LocalPlayerDetachedEvent args)
private void OnPlayerDetached(Entity<InputMoverComponent> entity, ref LocalPlayerDetachedEvent args)
{
SetMoveInput(component, MoveButtons.None);
SetMoveInput(entity, MoveButtons.None);
}
public override void UpdateBeforeSolve(bool prediction, float frameTime)

View File

@@ -34,7 +34,7 @@ public sealed class BodySystem : SharedBodySystem
private void OnRelayMoveInput(Entity<BodyComponent> ent, ref MoveInputEvent args)
{
// If they haven't actually moved then ignore it.
if ((args.Component.HeldMoveButtons &
if ((args.Entity.Comp.HeldMoveButtons &
(MoveButtons.Down | MoveButtons.Left | MoveButtons.Up | MoveButtons.Right)) == 0x0)
{
return;

View File

@@ -128,7 +128,7 @@ namespace Content.Server.Ghost
private void OnRelayMoveInput(EntityUid uid, GhostOnMoveComponent component, ref MoveInputEvent args)
{
// If they haven't actually moved then ignore it.
if ((args.Component.HeldMoveButtons &
if ((args.Entity.Comp.HeldMoveButtons &
(MoveButtons.Down | MoveButtons.Left | MoveButtons.Up | MoveButtons.Right)) == 0x0)
{
return;

View File

@@ -30,26 +30,26 @@ namespace Content.Server.Physics.Controllers
SubscribeLocalEvent<InputMoverComponent, PlayerDetachedEvent>(OnPlayerDetached);
}
private void OnRelayPlayerAttached(EntityUid uid, RelayInputMoverComponent component, PlayerAttachedEvent args)
private void OnRelayPlayerAttached(Entity<RelayInputMoverComponent> entity, ref PlayerAttachedEvent args)
{
if (MoverQuery.TryGetComponent(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
if (MoverQuery.TryGetComponent(entity.Comp.RelayEntity, out var inputMover))
SetMoveInput((entity.Owner, inputMover), MoveButtons.None);
}
private void OnRelayPlayerDetached(EntityUid uid, RelayInputMoverComponent component, PlayerDetachedEvent args)
private void OnRelayPlayerDetached(Entity<RelayInputMoverComponent> entity, ref PlayerDetachedEvent args)
{
if (MoverQuery.TryGetComponent(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
if (MoverQuery.TryGetComponent(entity.Comp.RelayEntity, out var inputMover))
SetMoveInput((entity.Owner, inputMover), MoveButtons.None);
}
private void OnPlayerAttached(EntityUid uid, InputMoverComponent component, PlayerAttachedEvent args)
private void OnPlayerAttached(Entity<InputMoverComponent> entity, ref PlayerAttachedEvent args)
{
SetMoveInput(component, MoveButtons.None);
SetMoveInput(entity, MoveButtons.None);
}
private void OnPlayerDetached(EntityUid uid, InputMoverComponent component, PlayerDetachedEvent args)
private void OnPlayerDetached(Entity<InputMoverComponent> entity, ref PlayerDetachedEvent args)
{
SetMoveInput(component, MoveButtons.None);
SetMoveInput(entity, MoveButtons.None);
}
protected override bool CanSound()

View File

@@ -9,16 +9,14 @@ namespace Content.Shared.Movement.Events;
[ByRefEvent]
public readonly struct MoveInputEvent
{
public readonly EntityUid Entity;
public readonly InputMoverComponent Component;
public readonly Entity<InputMoverComponent> Entity;
public readonly MoveButtons OldMovement;
public bool HasDirectionalMovement => (Component.HeldMoveButtons & MoveButtons.AnyDirection) != MoveButtons.None;
public bool HasDirectionalMovement => (Entity.Comp.HeldMoveButtons & MoveButtons.AnyDirection) != MoveButtons.None;
public MoveInputEvent(EntityUid entity, InputMoverComponent component, MoveButtons oldMovement)
public MoveInputEvent(Entity<InputMoverComponent> entity, MoveButtons oldMovement)
{
Entity = entity;
Component = component;
OldMovement = oldMovement;
}
}

View File

@@ -83,53 +83,53 @@ namespace Content.Shared.Movement.Systems
return oldMovement;
}
protected void SetMoveInput(InputMoverComponent component, MoveButtons buttons)
protected void SetMoveInput(Entity<InputMoverComponent> entity, MoveButtons buttons)
{
if (component.HeldMoveButtons == buttons)
if (entity.Comp.HeldMoveButtons == buttons)
return;
// Relay the fact we had any movement event.
// TODO: Ideally we'd do these in a tick instead of out of sim.
var moveEvent = new MoveInputEvent(component.Owner, component, component.HeldMoveButtons);
component.HeldMoveButtons = buttons;
RaiseLocalEvent(component.Owner, ref moveEvent);
Dirty(component.Owner, component);
var moveEvent = new MoveInputEvent(entity, entity.Comp.HeldMoveButtons);
entity.Comp.HeldMoveButtons = buttons;
RaiseLocalEvent(entity, ref moveEvent);
Dirty(entity, entity.Comp);
}
private void OnMoverHandleState(EntityUid uid, InputMoverComponent component, ComponentHandleState args)
private void OnMoverHandleState(Entity<InputMoverComponent> entity, ref ComponentHandleState args)
{
if (args.Current is not InputMoverComponentState state)
return;
// Handle state
component.LerpTarget = state.LerpTarget;
component.RelativeRotation = state.RelativeRotation;
component.TargetRelativeRotation = state.TargetRelativeRotation;
component.CanMove = state.CanMove;
component.RelativeEntity = EnsureEntity<InputMoverComponent>(state.RelativeEntity, uid);
entity.Comp.LerpTarget = state.LerpTarget;
entity.Comp.RelativeRotation = state.RelativeRotation;
entity.Comp.TargetRelativeRotation = state.TargetRelativeRotation;
entity.Comp.CanMove = state.CanMove;
entity.Comp.RelativeEntity = EnsureEntity<InputMoverComponent>(state.RelativeEntity, entity.Owner);
// Reset
component.LastInputTick = GameTick.Zero;
component.LastInputSubTick = 0;
entity.Comp.LastInputTick = GameTick.Zero;
entity.Comp.LastInputSubTick = 0;
if (component.HeldMoveButtons != state.HeldMoveButtons)
if (entity.Comp.HeldMoveButtons != state.HeldMoveButtons)
{
var moveEvent = new MoveInputEvent(uid, component, component.HeldMoveButtons);
component.HeldMoveButtons = state.HeldMoveButtons;
RaiseLocalEvent(uid, ref moveEvent);
var moveEvent = new MoveInputEvent(entity, entity.Comp.HeldMoveButtons);
entity.Comp.HeldMoveButtons = state.HeldMoveButtons;
RaiseLocalEvent(entity.Owner, ref moveEvent);
}
}
private void OnMoverGetState(EntityUid uid, InputMoverComponent component, ref ComponentGetState args)
private void OnMoverGetState(Entity<InputMoverComponent> entity, ref ComponentGetState args)
{
args.State = new InputMoverComponentState()
{
CanMove = component.CanMove,
RelativeEntity = GetNetEntity(component.RelativeEntity),
LerpTarget = component.LerpTarget,
HeldMoveButtons = component.HeldMoveButtons,
RelativeRotation = component.RelativeRotation,
TargetRelativeRotation = component.TargetRelativeRotation,
CanMove = entity.Comp.CanMove,
RelativeEntity = GetNetEntity(entity.Comp.RelativeEntity),
LerpTarget = entity.Comp.LerpTarget,
HeldMoveButtons = entity.Comp.HeldMoveButtons,
RelativeRotation = entity.Comp.RelativeRotation,
TargetRelativeRotation = entity.Comp.TargetRelativeRotation,
};
}
@@ -142,9 +142,9 @@ namespace Content.Shared.Movement.Systems
protected virtual void HandleShuttleInput(EntityUid uid, ShuttleButtons button, ushort subTick, bool state) {}
private void OnAutoParentChange(EntityUid uid, AutoOrientComponent component, ref EntParentChangedMessage args)
private void OnAutoParentChange(Entity<AutoOrientComponent> entity, ref EntParentChangedMessage args)
{
ResetCamera(uid);
ResetCamera(entity.Owner);
}
public void RotateCamera(EntityUid uid, Angle angle)
@@ -233,28 +233,28 @@ namespace Content.Shared.Movement.Systems
return rotation;
}
private void OnFollowedParentChange(EntityUid uid, FollowedComponent component, ref EntParentChangedMessage args)
private void OnFollowedParentChange(Entity<FollowedComponent> entity, ref EntParentChangedMessage args)
{
foreach (var foll in component.Following)
foreach (var foll in entity.Comp.Following)
{
if (!MoverQuery.TryGetComponent(foll, out var mover))
continue;
var ev = new EntParentChangedMessage(foll, null, args.OldMapId, XformQuery.GetComponent(foll));
OnInputParentChange(foll, mover, ref ev);
OnInputParentChange((foll, mover), ref ev);
}
}
private void OnInputParentChange(EntityUid uid, InputMoverComponent component, ref EntParentChangedMessage args)
private void OnInputParentChange(Entity<InputMoverComponent> entity, ref EntParentChangedMessage args)
{
// If we change our grid / map then delay updating our LastGridAngle.
var relative = args.Transform.GridUid;
relative ??= args.Transform.MapUid;
if (component.LifeStage < ComponentLifeStage.Running)
if (entity.Comp.LifeStage < ComponentLifeStage.Running)
{
component.RelativeEntity = relative;
Dirty(uid, component);
entity.Comp.RelativeEntity = relative;
Dirty(entity.Owner, entity.Comp);
return;
}
@@ -264,28 +264,28 @@ namespace Content.Shared.Movement.Systems
// If we change maps then reset eye rotation entirely.
if (oldMapId != mapId)
{
component.RelativeEntity = relative;
component.TargetRelativeRotation = Angle.Zero;
component.RelativeRotation = Angle.Zero;
component.LerpTarget = TimeSpan.Zero;
Dirty(uid, component);
entity.Comp.RelativeEntity = relative;
entity.Comp.TargetRelativeRotation = Angle.Zero;
entity.Comp.RelativeRotation = Angle.Zero;
entity.Comp.LerpTarget = TimeSpan.Zero;
Dirty(entity.Owner, entity.Comp);
return;
}
// If we go on a grid and back off then just reset the accumulator.
if (relative == component.RelativeEntity)
if (relative == entity.Comp.RelativeEntity)
{
if (component.LerpTarget >= Timing.CurTime)
if (entity.Comp.LerpTarget >= Timing.CurTime)
{
component.LerpTarget = TimeSpan.Zero;
Dirty(uid, component);
entity.Comp.LerpTarget = TimeSpan.Zero;
Dirty(entity.Owner, entity.Comp);
}
return;
}
component.LerpTarget = TimeSpan.FromSeconds(InputMoverComponent.LerpTime) + Timing.CurTime;
Dirty(uid, component);
entity.Comp.LerpTarget = TimeSpan.FromSeconds(InputMoverComponent.LerpTime) + Timing.CurTime;
Dirty(entity.Owner, entity.Comp);
}
private void HandleDirChange(EntityUid entity, Direction dir, ushort subTick, bool state)
@@ -299,7 +299,7 @@ namespace Content.Shared.Movement.Systems
DebugTools.AssertNotNull(relayMover.RelayEntity);
if (MoverQuery.TryGetComponent(entity, out var mover))
SetMoveInput(mover, MoveButtons.None);
SetMoveInput((entity, mover), MoveButtons.None);
if (!_mobState.IsIncapacitated(entity))
HandleDirChange(relayMover.RelayEntity, dir, subTick, state);
@@ -321,18 +321,18 @@ namespace Content.Shared.Movement.Systems
RaiseLocalEvent(xform.ParentUid, ref relayMoveEvent);
}
SetVelocityDirection(entity, moverComp, dir, subTick, state);
SetVelocityDirection((entity, moverComp), dir, subTick, state);
}
private void OnInputInit(EntityUid uid, InputMoverComponent component, ComponentInit args)
private void OnInputInit(Entity<InputMoverComponent> entity, ref ComponentInit args)
{
var xform = Transform(uid);
var xform = Transform(entity.Owner);
if (!xform.ParentUid.IsValid())
return;
component.RelativeEntity = xform.GridUid ?? xform.MapUid;
component.TargetRelativeRotation = Angle.Zero;
entity.Comp.RelativeEntity = xform.GridUid ?? xform.MapUid;
entity.Comp.TargetRelativeRotation = Angle.Zero;
}
private void HandleRunChange(EntityUid uid, ushort subTick, bool walking)
@@ -344,7 +344,7 @@ namespace Content.Shared.Movement.Systems
// if we swap to relay then stop our existing input if we ever change back.
if (moverComp != null)
{
SetMoveInput(moverComp, MoveButtons.None);
SetMoveInput((uid, moverComp), MoveButtons.None);
}
HandleRunChange(relayMover.RelayEntity, subTick, walking);
@@ -353,7 +353,7 @@ namespace Content.Shared.Movement.Systems
if (moverComp == null) return;
SetSprinting(uid, moverComp, subTick, walking);
SetSprinting((uid, moverComp), subTick, walking);
}
public (Vector2 Walking, Vector2 Sprinting) GetVelocityInput(InputMoverComponent mover)
@@ -404,7 +404,7 @@ namespace Content.Shared.Movement.Systems
/// composed into a single direction vector, <see cref="VelocityDir"/>. Enabling
/// opposite directions will cancel each other out, resulting in no direction.
/// </summary>
public void SetVelocityDirection(EntityUid entity, InputMoverComponent component, Direction direction, ushort subTick, bool enabled)
public void SetVelocityDirection(Entity<InputMoverComponent> entity, Direction direction, ushort subTick, bool enabled)
{
// Logger.Info($"[{_gameTiming.CurTick}/{subTick}] {direction}: {enabled}");
@@ -417,26 +417,26 @@ namespace Content.Shared.Movement.Systems
_ => throw new ArgumentException(nameof(direction))
};
SetMoveInput(entity, component, subTick, enabled, bit);
SetMoveInput(entity, subTick, enabled, bit);
}
private void SetMoveInput(EntityUid entity, InputMoverComponent component, ushort subTick, bool enabled, MoveButtons bit)
private void SetMoveInput(Entity<InputMoverComponent> entity, ushort subTick, bool enabled, MoveButtons bit)
{
// Modifies held state of a movement button at a certain sub tick and updates current tick movement vectors.
ResetSubtick(component);
ResetSubtick(entity.Comp);
if (subTick >= component.LastInputSubTick)
if (subTick >= entity.Comp.LastInputSubTick)
{
var fraction = (subTick - component.LastInputSubTick) / (float) ushort.MaxValue;
var fraction = (subTick - entity.Comp.LastInputSubTick) / (float) ushort.MaxValue;
ref var lastMoveAmount = ref component.Sprinting ? ref component.CurTickSprintMovement : ref component.CurTickWalkMovement;
ref var lastMoveAmount = ref entity.Comp.Sprinting ? ref entity.Comp.CurTickSprintMovement : ref entity.Comp.CurTickWalkMovement;
lastMoveAmount += DirVecForButtons(component.HeldMoveButtons) * fraction;
lastMoveAmount += DirVecForButtons(entity.Comp.HeldMoveButtons) * fraction;
component.LastInputSubTick = subTick;
entity.Comp.LastInputSubTick = subTick;
}
var buttons = component.HeldMoveButtons;
var buttons = entity.Comp.HeldMoveButtons;
if (enabled)
{
@@ -447,7 +447,7 @@ namespace Content.Shared.Movement.Systems
buttons &= ~bit;
}
SetMoveInput(component, buttons);
SetMoveInput(entity, buttons);
}
private void ResetSubtick(InputMoverComponent component)
@@ -460,11 +460,11 @@ namespace Content.Shared.Movement.Systems
component.LastInputSubTick = 0;
}
public void SetSprinting(EntityUid entity, InputMoverComponent component, ushort subTick, bool walking)
public void SetSprinting(Entity<InputMoverComponent> entity, ushort subTick, bool walking)
{
// Logger.Info($"[{_gameTiming.CurTick}/{subTick}] Sprint: {enabled}");
SetMoveInput(entity, component, subTick, walking, MoveButtons.Walk);
SetMoveInput(entity, subTick, walking, MoveButtons.Walk);
}
/// <summary>

View File

@@ -12,14 +12,14 @@ public abstract partial class SharedMoverController
SubscribeLocalEvent<RelayInputMoverComponent, AfterAutoHandleStateEvent>(OnAfterRelayState);
}
private void OnAfterRelayTargetState(EntityUid uid, MovementRelayTargetComponent component, ref AfterAutoHandleStateEvent args)
private void OnAfterRelayTargetState(Entity<MovementRelayTargetComponent> entity, ref AfterAutoHandleStateEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(entity.Owner);
}
private void OnAfterRelayState(EntityUid uid, RelayInputMoverComponent component, ref AfterAutoHandleStateEvent args)
private void OnAfterRelayState(Entity<RelayInputMoverComponent> entity, ref AfterAutoHandleStateEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(entity.Owner);
}
/// <summary>
@@ -61,30 +61,30 @@ public abstract partial class SharedMoverController
Dirty(relayEntity, targetComp);
}
private void OnRelayShutdown(EntityUid uid, RelayInputMoverComponent component, ComponentShutdown args)
private void OnRelayShutdown(Entity<RelayInputMoverComponent> entity, ref ComponentShutdown args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.RelayEntity);
Physics.UpdateIsPredicted(entity.Owner);
Physics.UpdateIsPredicted(entity.Comp.RelayEntity);
if (TryComp<InputMoverComponent>(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
if (TryComp<InputMoverComponent>(entity.Comp.RelayEntity, out var inputMover))
SetMoveInput((entity.Comp.RelayEntity, inputMover), MoveButtons.None);
if (Timing.ApplyingState)
return;
if (TryComp(component.RelayEntity, out MovementRelayTargetComponent? target) && target.LifeStage <= ComponentLifeStage.Running)
RemComp(component.RelayEntity, target);
if (TryComp(entity.Comp.RelayEntity, out MovementRelayTargetComponent? target) && target.LifeStage <= ComponentLifeStage.Running)
RemComp(entity.Comp.RelayEntity, target);
}
private void OnTargetRelayShutdown(EntityUid uid, MovementRelayTargetComponent component, ComponentShutdown args)
private void OnTargetRelayShutdown(Entity<MovementRelayTargetComponent> entity, ref ComponentShutdown args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.Source);
Physics.UpdateIsPredicted(entity.Owner);
Physics.UpdateIsPredicted(entity.Comp.Source);
if (Timing.ApplyingState)
return;
if (TryComp(component.Source, out RelayInputMoverComponent? relay) && relay.LifeStage <= ComponentLifeStage.Running)
RemComp(component.Source, relay);
if (TryComp(entity.Comp.Source, out RelayInputMoverComponent? relay) && relay.LifeStage <= ComponentLifeStage.Running)
RemComp(entity.Comp.Source, relay);
}
}