Files
wwdpublic/Content.Shared/Gravity/SharedFloatingVisualizerSystem.cs
gluesniffler 1b4f129428 Harpy Flight System (#919)
# Description

This PR adds a generic system which gives an entity the ability to fly.
Optionally increasing their speed in exchange for a continuous stamina
drain, which can, and **will** stamcrit them if left unchecked.

---

# Technical Details?

We normally dont have this section but I'd like to outline the changes
since I messed with quite a few systems:

- Introduces a `FlightComponent` which can be added to any entity in
YML, needs to be tied to an action with an event of type
`ToggleFlightEvent` This component holds properties for:
- Toggling animations on and off, either at the entity level or the
layer level.
    - Altering shader animation properties
- Altering speed, stamina drain, sounds played, delay between sounds,
etc etc.
- Adds a `FlyingVisualizerSystem` that can take a given `AnimationKey`
which points to a shader, and optionally can apply it to either the
entire sprite, or a given layer.
- Adds a check in `SharedGravitySystem` for making the entity weightless
when it has the `FlightComponent` and is flying.
- Adds a check in `SharedCuffableSystem` to disable cuffing when the
target has the `FlightComponent` and is flying.
- Introduces a new field in the `StaminaComponent` which serves as a
dictionary for persistent drains, with the key being the source (UID) of
where it came from. The drains can also indicate if they should apply
the stamina slowdown or not (relevant for both this PR, and for an
eventual sprinting PR)

---


<details><summary><h1>Media</h1></summary>
<p>

[![Flight
Demo](https://i.ytimg.com/vi/Wndv9hYaZ_s/maxresdefault.jpg)](https://youtu.be/Wndv9hYaZ_s
"Flight Demo")
</p>
</details>

---

# Changelog

🆑 Mocho
- add: Harpies are now able to fly on station for limited periods of
time, moving faster at the cost of stamina.

---------

Signed-off-by: gluesniffler <159397573+gluesniffler@users.noreply.github.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
2024-10-19 12:53:54 +07:00

87 lines
3.0 KiB
C#

using System.Numerics;
using Robust.Shared.Map;
using Content.Shared.Flight.Events;
namespace Content.Shared.Gravity;
/// <summary>
/// Handles offsetting a sprite when there is no gravity
/// </summary>
public abstract class SharedFloatingVisualizerSystem : EntitySystem
{
[Dependency] private readonly SharedGravitySystem GravitySystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FloatingVisualsComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<GravityChangedEvent>(OnGravityChanged);
SubscribeLocalEvent<FloatingVisualsComponent, EntParentChangedMessage>(OnEntParentChanged);
SubscribeNetworkEvent<FlightEvent>(OnFlight);
}
/// <summary>
/// Offsets a sprite with a linear interpolation animation
/// </summary>
public virtual void FloatAnimation(EntityUid uid, Vector2 offset, string animationKey, float animationTime, bool stop = false) { }
protected bool CanFloat(EntityUid uid, FloatingVisualsComponent component, TransformComponent? transform = null)
{
if (!Resolve(uid, ref transform))
return false;
if (transform.MapID == MapId.Nullspace)
return false;
component.CanFloat = GravitySystem.IsWeightless(uid, xform: transform);
Dirty(component);
return component.CanFloat;
}
private void OnComponentStartup(EntityUid uid, FloatingVisualsComponent component, ComponentStartup args)
{
if (CanFloat(uid, component))
FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime);
}
private void OnGravityChanged(ref GravityChangedEvent args)
{
var query = EntityQueryEnumerator<FloatingVisualsComponent, TransformComponent>();
while (query.MoveNext(out var uid, out var floating, out var transform))
{
if (transform.MapID == MapId.Nullspace)
continue;
if (transform.GridUid != args.ChangedGridIndex)
continue;
floating.CanFloat = !args.HasGravity;
Dirty(uid, floating);
if (!args.HasGravity)
FloatAnimation(uid, floating.Offset, floating.AnimationKey, floating.AnimationTime);
}
}
private void OnFlight(FlightEvent args)
{
var uid = GetEntity(args.Uid);
if (!TryComp<FloatingVisualsComponent>(uid, out var floating))
return;
floating.CanFloat = args.IsFlying;
if (!args.IsFlying
|| !args.IsAnimated)
return;
FloatAnimation(uid, floating.Offset, floating.AnimationKey, floating.AnimationTime);
}
private void OnEntParentChanged(EntityUid uid, FloatingVisualsComponent component, ref EntParentChangedMessage args)
{
var transform = args.Transform;
if (CanFloat(uid, component, transform))
FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime);
}
}