Files
wwdpublic/Content.Server/Flight/FlightSystem.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

158 lines
4.8 KiB
C#

using Content.Shared.Cuffs.Components;
using Content.Shared.Damage.Components;
using Content.Shared.DoAfter;
using Content.Shared.Flight;
using Content.Shared.Flight.Events;
using Content.Shared.Mobs;
using Content.Shared.Popups;
using Content.Shared.Stunnable;
using Content.Shared.Zombies;
using Robust.Shared.Audio.Systems;
namespace Content.Server.Flight;
public sealed class FlightSystem : SharedFlightSystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FlightComponent, ToggleFlightEvent>(OnToggleFlight);
SubscribeLocalEvent<FlightComponent, FlightDoAfterEvent>(OnFlightDoAfter);
SubscribeLocalEvent<FlightComponent, MobStateChangedEvent>(OnMobStateChangedEvent);
SubscribeLocalEvent<FlightComponent, EntityZombifiedEvent>(OnZombified);
SubscribeLocalEvent<FlightComponent, KnockedDownEvent>(OnKnockedDown);
SubscribeLocalEvent<FlightComponent, StunnedEvent>(OnStunned);
SubscribeLocalEvent<FlightComponent, SleepStateChangedEvent>(OnSleep);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<FlightComponent>();
while (query.MoveNext(out var uid, out var component))
{
if (!component.On)
continue;
component.TimeUntilFlap -= frameTime;
if (component.TimeUntilFlap > 0f)
continue;
_audio.PlayPvs(component.FlapSound, uid);
component.TimeUntilFlap = component.FlapInterval;
}
}
#region Core Functions
private void OnToggleFlight(EntityUid uid, FlightComponent component, ToggleFlightEvent args)
{
// If the user isnt flying, we check for conditionals and initiate a doafter.
if (!component.On)
{
if (!CanFly(uid, component))
return;
var doAfterArgs = new DoAfterArgs(EntityManager,
uid, component.ActivationDelay,
new FlightDoAfterEvent(), uid, target: uid)
{
BlockDuplicate = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
NeedHand = true
};
if (!_doAfter.TryStartDoAfter(doAfterArgs))
return;
}
else
ToggleActive(uid, false, component);
}
private void OnFlightDoAfter(EntityUid uid, FlightComponent component, FlightDoAfterEvent args)
{
if (args.Handled || args.Cancelled)
return;
ToggleActive(uid, true, component);
args.Handled = true;
}
#endregion
#region Conditionals
private bool CanFly(EntityUid uid, FlightComponent component)
{
if (TryComp<CuffableComponent>(uid, out var cuffableComp) && !cuffableComp.CanStillInteract)
{
_popupSystem.PopupEntity(Loc.GetString("no-flight-while-restrained"), uid, uid, PopupType.Medium);
return false;
}
if (HasComp<ZombieComponent>(uid))
{
_popupSystem.PopupEntity(Loc.GetString("no-flight-while-zombified"), uid, uid, PopupType.Medium);
return false;
}
return true;
}
private void OnMobStateChangedEvent(EntityUid uid, FlightComponent component, MobStateChangedEvent args)
{
if (!component.On
|| args.NewMobState is MobState.Critical or MobState.Dead)
return;
ToggleActive(args.Target, false, component);
}
private void OnZombified(EntityUid uid, FlightComponent component, ref EntityZombifiedEvent args)
{
if (!component.On)
return;
ToggleActive(args.Target, false, component);
if (!TryComp<StaminaComponent>(uid, out var stamina))
return;
Dirty(uid, stamina);
}
private void OnKnockedDown(EntityUid uid, FlightComponent component, ref KnockedDownEvent args)
{
if (!component.On)
return;
ToggleActive(uid, false, component);
}
private void OnStunned(EntityUid uid, FlightComponent component, ref StunnedEvent args)
{
if (!component.On)
return;
ToggleActive(uid, false, component);
}
private void OnSleep(EntityUid uid, FlightComponent component, ref SleepStateChangedEvent args)
{
if (!component.On
|| !args.FellAsleep)
return;
ToggleActive(uid, false, component);
if (!TryComp<StaminaComponent>(uid, out var stamina))
return;
Dirty(uid, stamina);
}
#endregion
}