Skip to content

Commit

Permalink
Sprite Movement working with AI movement (space-wizards#33494)
Browse files Browse the repository at this point in the history
* FINALLY

* Update animals.yml
  • Loading branch information
TheShuEd authored Dec 18, 2024
1 parent b649517 commit 18fe8b9
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 114 deletions.
46 changes: 46 additions & 0 deletions Content.Client/Movement/Systems/ClientSpriteMovementSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Robust.Client.GameObjects;
using Robust.Shared.Timing;

namespace Content.Client.Movement.Systems;

/// <summary>
/// Controls the switching of motion and standing still animation
/// </summary>
public sealed class ClientSpriteMovementSystem : SharedSpriteMovementSystem
{
[Dependency] private readonly IGameTiming _timing = default!;

private EntityQuery<SpriteComponent> _spriteQuery;

public override void Initialize()
{
base.Initialize();

_spriteQuery = GetEntityQuery<SpriteComponent>();

SubscribeLocalEvent<SpriteMovementComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
}

private void OnAfterAutoHandleState(Entity<SpriteMovementComponent> ent, ref AfterAutoHandleStateEvent args)
{
if (!_spriteQuery.TryGetComponent(ent, out var sprite))
return;

if (ent.Comp.IsMoving)
{
foreach (var (layer, state) in ent.Comp.MovementLayers)
{
sprite.LayerSetData(layer, state);
}
}
else
{
foreach (var (layer, state) in ent.Comp.NoMovementLayers)
{
sprite.LayerSetData(layer, state);
}
}
}
}
51 changes: 0 additions & 51 deletions Content.Client/Movement/Systems/SpriteMovementSystem.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Content.Server/Movement/Systems/SpriteMovementSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Content.Shared.Movement.Systems;

namespace Content.Server.Movement.Systems;

public sealed class SpriteMovementSystem : SharedSpriteMovementSystem
{
}
4 changes: 2 additions & 2 deletions Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private bool TrySeek(

if (!targetCoordinates.IsValid(EntityManager))
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return false;
}
Expand All @@ -263,7 +263,7 @@ private bool TrySeek(
// Can't make it again.
if (ourMap.MapId != targetMap.MapId)
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return false;
}
Expand Down
21 changes: 14 additions & 7 deletions Content.Server/NPC/Systems/NPCSteeringSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Content.Shared.CombatMode;
using Content.Shared.Interaction;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.NPC;
using Content.Shared.NPC.Components;
Expand Down Expand Up @@ -207,6 +208,9 @@ public void Unregister(EntityUid uid, NPCSteeringComponent? component = null)
if (EntityManager.TryGetComponent(uid, out InputMoverComponent? controller))
{
controller.CurTickSprintMovement = Vector2.Zero;

var ev = new SpriteMoveEvent(false);
RaiseLocalEvent(uid, ref ev);
}

component.PathfindToken?.Cancel();
Expand Down Expand Up @@ -270,7 +274,7 @@ public override void Update(float frameTime)
}
}

private void SetDirection(InputMoverComponent component, NPCSteeringComponent steering, Vector2 value, bool clear = true)
private void SetDirection(EntityUid uid, InputMoverComponent component, NPCSteeringComponent steering, Vector2 value, bool clear = true)
{
if (clear && value.Equals(Vector2.Zero))
{
Expand All @@ -282,6 +286,9 @@ private void SetDirection(InputMoverComponent component, NPCSteeringComponent st
component.CurTickSprintMovement = value;
component.LastInputTick = _timing.CurTick;
component.LastInputSubTick = ushort.MaxValue;

var ev = new SpriteMoveEvent(true);
RaiseLocalEvent(uid, ref ev);
}

/// <summary>
Expand All @@ -297,22 +304,22 @@ private void Steer(
{
if (Deleted(steering.Coordinates.EntityId))
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return;
}

// No path set from pathfinding or the likes.
if (steering.Status == SteeringStatus.NoPath)
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
return;
}

// Can't move at all, just noop input.
if (!mover.CanMove)
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return;
}
Expand Down Expand Up @@ -341,7 +348,7 @@ private void Steer(

if (steering.CanSeek && !TrySeek(uid, mover, steering, body, xform, offsetRot, moveSpeed, interest, frameTime, ref forceSteer))
{
SetDirection(mover, steering, Vector2.Zero);
SetDirection(uid, mover, steering, Vector2.Zero);
return;
}

Expand All @@ -354,7 +361,7 @@ private void Steer(

if (!forceSteer)
{
SetDirection(mover, steering, steering.LastSteerDirection, false);
SetDirection(uid, mover, steering, steering.LastSteerDirection, false);
return;
}

Expand Down Expand Up @@ -391,7 +398,7 @@ private void Steer(

steering.LastSteerDirection = resultDirection;
DebugTools.Assert(!float.IsNaN(resultDirection.X));
SetDirection(mover, steering, resultDirection, false);
SetDirection(uid, mover, steering, resultDirection, false);
}

private EntityCoordinates GetCoordinates(PathPoly poly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Content.Shared.Movement.Components;
/// <summary>
/// Updates a sprite layer based on whether an entity is moving via input or not.
/// </summary>
[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class SpriteMovementComponent : Component
{
/// <summary>
Expand All @@ -19,4 +19,7 @@ public sealed partial class SpriteMovementComponent : Component
/// </summary>
[DataField]
public Dictionary<string, PrototypeLayerData> NoMovementLayers = new();

[DataField, AutoNetworkedField]
public bool IsMoving;
}
15 changes: 15 additions & 0 deletions Content.Shared/Movement/Events/SpriteMoveEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Content.Shared.Movement.Events;

/// <summary>
/// Raised on an entity whenever it should change movement sprite
/// </summary>
[ByRefEvent]
public readonly struct SpriteMoveEvent
{
public readonly bool IsMoving = false;

public SpriteMoveEvent(bool isMoving)
{
IsMoving = isMoving;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ protected void SetMoveInput(Entity<InputMoverComponent> entity, MoveButtons butt
entity.Comp.HeldMoveButtons = buttons;
RaiseLocalEvent(entity, ref moveEvent);
Dirty(entity, entity.Comp);

var ev = new SpriteMoveEvent(entity.Comp.HeldMoveButtons != MoveButtons.None);
RaiseLocalEvent(entity, ref ev);
}

private void OnMoverHandleState(Entity<InputMoverComponent> entity, ref ComponentHandleState args)
Expand All @@ -119,6 +122,9 @@ private void OnMoverHandleState(Entity<InputMoverComponent> entity, ref Componen
var moveEvent = new MoveInputEvent(entity, entity.Comp.HeldMoveButtons);
entity.Comp.HeldMoveButtons = state.HeldMoveButtons;
RaiseLocalEvent(entity.Owner, ref moveEvent);

var ev = new SpriteMoveEvent(entity.Comp.HeldMoveButtons != MoveButtons.None);
RaiseLocalEvent(entity, ref ev);
}
}

Expand Down
23 changes: 23 additions & 0 deletions Content.Shared/Movement/Systems/SharedSpriteMovementSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;

namespace Content.Shared.Movement.Systems;

public abstract class SharedSpriteMovementSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SpriteMovementComponent, SpriteMoveEvent>(OnSpriteMoveInput);
}

private void OnSpriteMoveInput(Entity<SpriteMovementComponent> ent, ref SpriteMoveEvent args)
{
if (ent.Comp.IsMoving == args.IsMoving)
return;

ent.Comp.IsMoving = args.IsMoving;
Dirty(ent);
}
}
Loading

0 comments on commit 18fe8b9

Please sign in to comment.