Skip to content

Commit

Permalink
Clown shoes make you waddle, as God intended (#26338)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
FairlySadPanda authored Apr 14, 2024
1 parent a7fad5d commit ef42fb3
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Content.Client/Clothing/Systems/WaddleClothingSystem.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
135 changes: 135 additions & 0 deletions Content.Client/Movement/Systems/WaddleAnimationSystem.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions Content.Shared/Clothing/Components/WaddleWhenWornComponent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
72 changes: 72 additions & 0 deletions Content.Shared/Movement/Components/WaddleAnimationComponent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Clothing/Shoes/specific.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
parent: [ClothingShoesBaseButcherable, ClothingSlotBase]
id: ClothingShoesClownBase
components:
- type: WaddleWhenWorn
- type: ItemSlots
slots:
item:
Expand Down

0 comments on commit ef42fb3

Please sign in to comment.