Files
wwdpublic/Content.Shared/Flight/SharedFlightSystem.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

105 lines
3.7 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Movement.Systems;
using Content.Shared.Damage.Systems;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction.Components;
using Content.Shared.Inventory.VirtualItem;
using Content.Shared.Flight.Events;
namespace Content.Shared.Flight;
public abstract class SharedFlightSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
[Dependency] private readonly StaminaSystem _staminaSystem = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeed = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FlightComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<FlightComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<FlightComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMoveSpeed);
}
#region Core Functions
private void OnStartup(EntityUid uid, FlightComponent component, ComponentStartup args)
{
_actionsSystem.AddAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
}
private void OnShutdown(EntityUid uid, FlightComponent component, ComponentShutdown args)
{
_actionsSystem.RemoveAction(uid, component.ToggleActionEntity);
}
public void ToggleActive(EntityUid uid, bool active, FlightComponent component)
{
component.On = active;
component.TimeUntilFlap = 0f;
_actionsSystem.SetToggled(component.ToggleActionEntity, component.On);
RaiseNetworkEvent(new FlightEvent(GetNetEntity(uid), component.On, component.IsAnimated));
_staminaSystem.ToggleStaminaDrain(uid, component.StaminaDrainRate, active, false);
_movementSpeed.RefreshMovementSpeedModifiers(uid);
UpdateHands(uid, active);
Dirty(uid, component);
}
private void UpdateHands(EntityUid uid, bool flying)
{
if (!TryComp<HandsComponent>(uid, out var handsComponent))
return;
if (flying)
BlockHands(uid, handsComponent);
else
FreeHands(uid);
}
private void BlockHands(EntityUid uid, HandsComponent handsComponent)
{
var freeHands = 0;
foreach (var hand in _hands.EnumerateHands(uid, handsComponent))
{
if (hand.HeldEntity == null)
{
freeHands++;
continue;
}
// Is this entity removable? (they might have handcuffs on)
if (HasComp<UnremoveableComponent>(hand.HeldEntity) && hand.HeldEntity != uid)
continue;
_hands.DoDrop(uid, hand, true, handsComponent);
freeHands++;
if (freeHands == 2)
break;
}
if (_virtualItem.TrySpawnVirtualItemInHand(uid, uid, out var virtItem1))
EnsureComp<UnremoveableComponent>(virtItem1.Value);
if (_virtualItem.TrySpawnVirtualItemInHand(uid, uid, out var virtItem2))
EnsureComp<UnremoveableComponent>(virtItem2.Value);
}
private void FreeHands(EntityUid uid)
{
_virtualItem.DeleteInHandsMatching(uid, uid);
}
private void OnRefreshMoveSpeed(EntityUid uid, FlightComponent component, RefreshMovementSpeedModifiersEvent args)
{
if (!component.On)
return;
args.ModifySpeed(component.SpeedModifier, component.SpeedModifier);
}
#endregion
}
public sealed partial class ToggleFlightEvent : InstantActionEvent { }