]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Clown shoes make you waddle, as God intended (#26338)
authorHannah Giovanna Dawson <karakkaraz@gmail.com>
Sun, 14 Apr 2024 12:12:54 +0000 (13:12 +0100)
committerGitHub <noreply@github.com>
Sun, 14 Apr 2024 12:12:54 +0000 (08:12 -0400)
* Clown shoes make you waddle, as God intended

* OOPS

* Toned down, client system name fix

* Tidy namespacing for @deltanedas

* Refactor to handle prediction better, etc.

* Resolve PR comments.

Content.Client/Clothing/Systems/WaddleClothingSystem.cs [new file with mode: 0644]
Content.Client/Movement/Systems/WaddleAnimationSystem.cs [new file with mode: 0644]
Content.Shared/Clothing/Components/WaddleWhenWornComponent.cs [new file with mode: 0644]
Content.Shared/Movement/Components/WaddleAnimationComponent.cs [new file with mode: 0644]
Resources/Prototypes/Entities/Clothing/Shoes/specific.yml

diff --git a/Content.Client/Clothing/Systems/WaddleClothingSystem.cs b/Content.Client/Clothing/Systems/WaddleClothingSystem.cs
new file mode 100644 (file)
index 0000000..b8ac3c2
--- /dev/null
@@ -0,0 +1,31 @@
+using Content.Shared.Clothing.Components;
+using Content.Shared.Movement.Components;
+using Content.Shared.Inventory.Events;
+
+namespace Content.Client.Clothing.Systems;
+
+public sealed class WaddleClothingSystem : EntitySystem
+{
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<WaddleWhenWornComponent, GotEquippedEvent>(OnGotEquipped);
+        SubscribeLocalEvent<WaddleWhenWornComponent, GotUnequippedEvent>(OnGotUnequipped);
+    }
+
+    private void OnGotEquipped(EntityUid entity, WaddleWhenWornComponent comp, GotEquippedEvent args)
+    {
+        var waddleAnimComp = EnsureComp<WaddleAnimationComponent>(args.Equipee);
+
+        waddleAnimComp.AnimationLength = comp.AnimationLength;
+        waddleAnimComp.HopIntensity = comp.HopIntensity;
+        waddleAnimComp.RunAnimationLengthMultiplier = comp.RunAnimationLengthMultiplier;
+        waddleAnimComp.TumbleIntensity = comp.TumbleIntensity;
+    }
+
+    private void OnGotUnequipped(EntityUid entity, WaddleWhenWornComponent comp, GotUnequippedEvent args)
+    {
+        RemComp<WaddleAnimationComponent>(args.Equipee);
+    }
+}
diff --git a/Content.Client/Movement/Systems/WaddleAnimationSystem.cs b/Content.Client/Movement/Systems/WaddleAnimationSystem.cs
new file mode 100644 (file)
index 0000000..83bb697
--- /dev/null
@@ -0,0 +1,135 @@
+using System.Numerics;
+using Content.Client.Gravity;
+using Content.Shared.Movement.Components;
+using Content.Shared.Movement.Events;
+using Robust.Client.Animations;
+using Robust.Client.GameObjects;
+using Robust.Shared.Animations;
+using Robust.Shared.Timing;
+
+namespace Content.Client.Movement.Systems;
+
+public sealed class WaddleAnimationSystem : EntitySystem
+{
+    [Dependency] private readonly AnimationPlayerSystem _animation = default!;
+    [Dependency] private readonly GravitySystem _gravity = default!;
+    [Dependency] private readonly IGameTiming _timing = default!;
+
+    public override void Initialize()
+    {
+        SubscribeLocalEvent<WaddleAnimationComponent, MoveInputEvent>(OnMovementInput);
+        SubscribeLocalEvent<WaddleAnimationComponent, StartedWaddlingEvent>(OnStartedWalking);
+        SubscribeLocalEvent<WaddleAnimationComponent, StoppedWaddlingEvent>(OnStoppedWalking);
+        SubscribeLocalEvent<WaddleAnimationComponent, AnimationCompletedEvent>(OnAnimationCompleted);
+    }
+
+    private void OnMovementInput(EntityUid entity, WaddleAnimationComponent component, MoveInputEvent args)
+    {
+        // Prediction mitigation. Prediction means that MoveInputEvents are spammed repeatedly, even though you'd assume
+        // they're once-only for the user actually doing something. As such do nothing if we're just repeating this FoR.
+        if (!_timing.IsFirstTimePredicted)
+        {
+            return;
+        }
+
+        if (!args.HasDirectionalMovement && component.IsCurrentlyWaddling)
+        {
+            component.IsCurrentlyWaddling = false;
+
+            var stopped = new StoppedWaddlingEvent(entity);
+
+            RaiseLocalEvent(entity, ref stopped);
+
+            return;
+        }
+
+        // Only start waddling if we're not currently AND we're actually moving.
+        if (component.IsCurrentlyWaddling || !args.HasDirectionalMovement)
+            return;
+
+        component.IsCurrentlyWaddling = true;
+
+        var started = new StartedWaddlingEvent(entity);
+
+        RaiseLocalEvent(entity, ref started);
+    }
+
+    private void OnStartedWalking(EntityUid uid, WaddleAnimationComponent component, StartedWaddlingEvent args)
+    {
+        if (_animation.HasRunningAnimation(uid, component.KeyName))
+        {
+            return;
+        }
+
+        if (!TryComp<InputMoverComponent>(uid, out var mover))
+        {
+            return;
+        }
+
+        if (_gravity.IsWeightless(uid))
+        {
+            return;
+        }
+
+        var tumbleIntensity = component.LastStep ? 360 - component.TumbleIntensity : component.TumbleIntensity;
+        var len = mover.Sprinting ? component.AnimationLength * component.RunAnimationLengthMultiplier : component.AnimationLength;
+
+        component.LastStep = !component.LastStep;
+        component.IsCurrentlyWaddling = true;
+
+        var anim = new Animation()
+        {
+            Length = TimeSpan.FromSeconds(len),
+            AnimationTracks =
+            {
+                new AnimationTrackComponentProperty()
+                {
+                    ComponentType = typeof(SpriteComponent),
+                    Property = nameof(SpriteComponent.Rotation),
+                    InterpolationMode = AnimationInterpolationMode.Linear,
+                    KeyFrames =
+                    {
+                        new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(0), 0),
+                        new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(tumbleIntensity), len/2),
+                        new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(0), len/2),
+                    }
+                },
+                new AnimationTrackComponentProperty()
+                {
+                    ComponentType = typeof(SpriteComponent),
+                    Property = nameof(SpriteComponent.Offset),
+                    InterpolationMode = AnimationInterpolationMode.Linear,
+                    KeyFrames =
+                    {
+                        new AnimationTrackProperty.KeyFrame(new Vector2(), 0),
+                        new AnimationTrackProperty.KeyFrame(component.HopIntensity, len/2),
+                        new AnimationTrackProperty.KeyFrame(new Vector2(), len/2),
+                    }
+                }
+            }
+        };
+
+        _animation.Play(uid, anim, component.KeyName);
+    }
+
+    private void OnStoppedWalking(EntityUid uid, WaddleAnimationComponent component, StoppedWaddlingEvent args)
+    {
+        _animation.Stop(uid, component.KeyName);
+
+        if (!TryComp<SpriteComponent>(uid, out var sprite))
+        {
+            return;
+        }
+
+        sprite.Offset = new Vector2();
+        sprite.Rotation = Angle.FromDegrees(0);
+        component.IsCurrentlyWaddling = false;
+    }
+
+    private void OnAnimationCompleted(EntityUid uid, WaddleAnimationComponent component, AnimationCompletedEvent args)
+    {
+        var started = new StartedWaddlingEvent(uid);
+
+        RaiseLocalEvent(uid, ref started);
+    }
+}
diff --git a/Content.Shared/Clothing/Components/WaddleWhenWornComponent.cs b/Content.Shared/Clothing/Components/WaddleWhenWornComponent.cs
new file mode 100644 (file)
index 0000000..5cd7a72
--- /dev/null
@@ -0,0 +1,35 @@
+using System.Numerics;
+
+namespace Content.Shared.Clothing.Components;
+
+/// <summary>
+/// Defines something as causing waddling when worn.
+/// </summary>
+[RegisterComponent]
+public sealed partial class WaddleWhenWornComponent : Component
+{
+    ///<summary>
+    /// How high should they hop during the waddle? Higher hop = more energy.
+    /// </summary>
+    [DataField]
+    public Vector2 HopIntensity = new(0, 0.25f);
+
+    /// <summary>
+    /// How far should they rock backward and forward during the waddle?
+    /// Each step will alternate between this being a positive and negative rotation. More rock = more scary.
+    /// </summary>
+    [DataField]
+    public float TumbleIntensity = 20.0f;
+
+    /// <summary>
+    /// How long should a complete step take? Less time = more chaos.
+    /// </summary>
+    [DataField]
+    public float AnimationLength = 0.66f;
+
+    /// <summary>
+    /// How much shorter should the animation be when running?
+    /// </summary>
+    [DataField]
+    public float RunAnimationLengthMultiplier = 0.568f;
+}
diff --git a/Content.Shared/Movement/Components/WaddleAnimationComponent.cs b/Content.Shared/Movement/Components/WaddleAnimationComponent.cs
new file mode 100644 (file)
index 0000000..c43ef30
--- /dev/null
@@ -0,0 +1,72 @@
+using System.Numerics;
+
+namespace Content.Shared.Movement.Components;
+
+/// <summary>
+/// Declares that an entity has started to waddle like a duck/clown.
+/// </summary>
+/// <param name="Entity">The newly be-waddled.</param>
+[ByRefEvent]
+public record struct StartedWaddlingEvent(EntityUid Entity)
+{
+    public EntityUid Entity = Entity;
+}
+
+/// <summary>
+/// Declares that an entity has stopped waddling like a duck/clown.
+/// </summary>
+/// <param name="Entity">The former waddle-er.</param>
+[ByRefEvent]
+public record struct StoppedWaddlingEvent(EntityUid Entity)
+{
+    public EntityUid Entity = Entity;
+}
+
+/// <summary>
+/// Defines something as having a waddle animation when it moves.
+/// </summary>
+[RegisterComponent]
+public sealed partial class WaddleAnimationComponent : Component
+{
+    /// <summary>
+    /// What's the name of this animation? Make sure it's unique so it can play along side other animations.
+    /// This prevents someone accidentally causing two identical waddling effects to play on someone at the same time.
+    /// </summary>
+    [DataField]
+    public string KeyName = "Waddle";
+
+    ///<summary>
+    /// How high should they hop during the waddle? Higher hop = more energy.
+    /// </summary>
+    [DataField]
+    public Vector2 HopIntensity = new(0, 0.25f);
+
+    /// <summary>
+    /// How far should they rock backward and forward during the waddle?
+    /// Each step will alternate between this being a positive and negative rotation. More rock = more scary.
+    /// </summary>
+    [DataField]
+    public float TumbleIntensity = 20.0f;
+
+    /// <summary>
+    /// How long should a complete step take? Less time = more chaos.
+    /// </summary>
+    [DataField]
+    public float AnimationLength = 0.66f;
+
+    /// <summary>
+    /// How much shorter should the animation be when running?
+    /// </summary>
+    [DataField]
+    public float RunAnimationLengthMultiplier = 0.568f;
+
+    /// <summary>
+    /// Stores which step we made last, so if someone cancels out of the animation mid-step then restarts it looks more natural.
+    /// </summary>
+    public bool LastStep;
+
+    /// <summary>
+    /// Stores if we're currently waddling so we can start/stop as appropriate and can tell other systems our state.
+    /// </summary>
+    public bool IsCurrentlyWaddling;
+}
index 3158e4de53f38bf1593fd46552b3e2194794002d..987eda582e416b4f60fbb95cd8d0b1bc0f6a93ec 100644 (file)
@@ -15,6 +15,7 @@
   parent: [ClothingShoesBaseButcherable, ClothingSlotBase]
   id: ClothingShoesClownBase
   components:
+  - type: WaddleWhenWorn
   - type: ItemSlots
     slots:
       item: