Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into blank1
Browse files Browse the repository at this point in the history
  • Loading branch information
PyotrIgn committed Apr 28, 2024
2 parents db99630 + 4cd1836 commit 9c39745
Show file tree
Hide file tree
Showing 77 changed files with 651 additions and 221 deletions.
144 changes: 144 additions & 0 deletions Content.Client/Movement/Systems/ClientWaddleAnimationSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Content.Client.Buckle;
using Content.Client.Gravity;
using Content.Shared.ActionBlocker;
using Content.Shared.Mobs.Systems;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;

namespace Content.Client.Movement.Systems;

public sealed class ClientWaddleAnimationSystem : SharedWaddleAnimationSystem
{
[Dependency] private readonly AnimationPlayerSystem _animation = default!;
[Dependency] private readonly GravitySystem _gravity = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly BuckleSystem _buckle = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;

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

// Start waddling
SubscribeAllEvent<StartedWaddlingEvent>((msg, args) => StartWaddling(args.SenderSession.AttachedEntity));

// Handle concluding animations
SubscribeLocalEvent<WaddleAnimationComponent, AnimationCompletedEvent>(OnAnimationCompleted);

// Stop waddling
SubscribeAllEvent<StoppedWaddlingEvent>((msg, args) => StopWaddling(args.SenderSession.AttachedEntity));
}

private void StartWaddling(EntityUid? uid)
{
if (
!EntityIsValid(uid, out var entity, out var component) ||
_animation.HasRunningAnimation(entity.Value, component.KeyName) ||
_gravity.IsWeightless(entity.Value) ||
_buckle.IsBuckled(entity.Value) ||
_mobState.IsIncapacitated(entity.Value) ||
!TryComp<InputMoverComponent>(entity, out var mover) ||
!_actionBlocker.CanMove(entity.Value, mover)
)
return;

PlayWaddleAnimationUsing(entity.Value, component, CalculateAnimationLength(component, mover), CalculateTumbleIntensity(component));
}

private static float CalculateTumbleIntensity(WaddleAnimationComponent component)
{
return component.LastStep ? 360 - component.TumbleIntensity : component.TumbleIntensity;
}

private static float CalculateAnimationLength(WaddleAnimationComponent component, InputMoverComponent mover)
{
return mover.Sprinting ? component.AnimationLength * component.RunAnimationLengthMultiplier : component.AnimationLength;
}

private void OnAnimationCompleted(EntityUid uid, WaddleAnimationComponent component, AnimationCompletedEvent args)
{
if (args.Key != component.KeyName)
return;

if (!TryComp<InputMoverComponent>(uid, out var mover))
return;

PlayWaddleAnimationUsing(uid, component, CalculateAnimationLength(component, mover), CalculateTumbleIntensity(component));
}

private void StopWaddling(EntityUid? uid)
{
if (
!EntityIsValid(uid, out var entity, out var component) ||
!_animation.HasRunningAnimation(entity.Value, component.KeyName)
)
return;

_animation.Stop(entity.Value, component.KeyName);

if (!TryComp<SpriteComponent>(entity.Value, out var sprite))
return;

// Note that this is a hard-write to this sprite, not some layer-based operation. If this is called whilst a sprite
// is lying down, it will make the sprite stand up, which usually looks wrong.
sprite.Offset = new Vector2();
sprite.Rotation = Angle.FromDegrees(0);
}

private bool EntityIsValid(EntityUid? uid, [NotNullWhen(true)] out EntityUid? entity, [NotNullWhen(true)] out WaddleAnimationComponent? component)
{
entity = null;
component = null;

if (!uid.HasValue)
return false;

entity = uid.Value;

return TryComp(entity, out component);
}

private void PlayWaddleAnimationUsing(EntityUid uid, WaddleAnimationComponent component, float len, float tumbleIntensity)
{
component.LastStep = !component.LastStep;

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);
}
}
135 changes: 0 additions & 135 deletions Content.Client/Movement/Systems/WaddleAnimationSystem.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Content.Shared.Movement.Systems;

namespace Content.Server.Movement.Systems;

public sealed class ServerWaddleAnimationSystem : SharedWaddleAnimationSystem;
2 changes: 1 addition & 1 deletion Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ public static readonly CVarDef<int>
CVarDef.Create("chat.max_message_length", 1000, CVar.SERVER | CVar.REPLICATED);

public static readonly CVarDef<int> ChatMaxAnnouncementLength =
CVarDef.Create("chat.max_announcement_length", 256, CVar.SERVER | CVar.REPLICATED);
CVarDef.Create("chat.max_announcement_length", 512, CVar.SERVER | CVar.REPLICATED);

public static readonly CVarDef<bool> ChatSanitizerEnabled =
CVarDef.Create("chat.chat_sanitizer_enabled", true, CVar.SERVERONLY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Content.Shared.Clothing.Components;
using Content.Shared.Movement.Components;
using Content.Shared.Inventory.Events;
using Content.Shared.Movement.Components;

namespace Content.Client.Clothing.Systems;
namespace Content.Shared.Clothing.EntitySystems;

public sealed class WaddleClothingSystem : EntitySystem
{
Expand Down
32 changes: 13 additions & 19 deletions Content.Shared/Movement/Components/WaddleAnimationComponent.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
using System.Numerics;
using System.Numerics;
using Robust.Shared.Serialization;

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;
}
[Serializable, NetSerializable]
public sealed class StartedWaddlingEvent : EntityEventArgs;

/// <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;
}
[Serializable, NetSerializable]
public sealed class StoppedWaddlingEvent : EntityEventArgs;

/// <summary>
/// Defines something as having a waddle animation when it moves.
/// </summary>
[RegisterComponent]
[RegisterComponent, AutoGenerateComponentState]
public sealed partial class WaddleAnimationComponent : Component
{
/// <summary>
Expand All @@ -38,26 +31,26 @@ public sealed partial class WaddleAnimationComponent : Component
///<summary>
/// How high should they hop during the waddle? Higher hop = more energy.
/// </summary>
[DataField]
[DataField, AutoNetworkedField]
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]
[DataField, AutoNetworkedField]
public float TumbleIntensity = 20.0f;

/// <summary>
/// How long should a complete step take? Less time = more chaos.
/// </summary>
[DataField]
[DataField, AutoNetworkedField]
public float AnimationLength = 0.66f;

/// <summary>
/// How much shorter should the animation be when running?
/// </summary>
[DataField]
[DataField, AutoNetworkedField]
public float RunAnimationLengthMultiplier = 0.568f;

/// <summary>
Expand All @@ -66,7 +59,8 @@ public sealed partial class WaddleAnimationComponent : Component
public bool LastStep;

/// <summary>
/// Stores if we're currently waddling so we can start/stop as appropriate and can tell other systems our state.
/// Stores if we're currently waddling, so we can start/stop as appropriate and can tell other systems our state.
/// </summary>
[DataField, AutoNetworkedField]
public bool IsCurrentlyWaddling;
}
Loading

0 comments on commit 9c39745

Please sign in to comment.