From 21e3d8564d333d5e0d4abb9eed6caa3febb7ff9f Mon Sep 17 00:00:00 2001 From: gluesniffler Date: Thu, 12 Sep 2024 17:49:06 -0400 Subject: [PATCH] messing around with shader code --- Content.Client/DeltaV/Harpy/FlightSystem.cs | 36 +++++++--- .../DeltaV/Harpy/FlyingVisualizerSystem.cs | 0 .../DeltaV/Harpy/FlyingVisualsSystem.cs | 57 +++++++++++++++ .../Components/FlyingVisualsComponent.cs | 29 ++++++++ Content.Shared/DeltaV/Harpy/Events.cs | 8 ++- .../DeltaV/Harpy/FlightComponent.cs | 41 ++++++++++- .../DeltaV/Harpy/FlyingVisualsComponent.cs | 0 .../DeltaV/Harpy/SharedFlightSystem.cs | 2 +- .../Harpy/SharedFlyingVisualizerSystem.cs | 69 ------------------- .../Gravity/SharedFloatingVisualizerSystem.cs | 2 +- .../Entities/Mobs/Species/harpy.yml | 2 + 11 files changed, 163 insertions(+), 83 deletions(-) delete mode 100644 Content.Client/DeltaV/Harpy/FlyingVisualizerSystem.cs create mode 100644 Content.Client/DeltaV/Harpy/FlyingVisualsSystem.cs create mode 100644 Content.Shared/DeltaV/Harpy/Components/FlyingVisualsComponent.cs delete mode 100644 Content.Shared/DeltaV/Harpy/FlyingVisualsComponent.cs delete mode 100644 Content.Shared/DeltaV/Harpy/SharedFlyingVisualizerSystem.cs diff --git a/Content.Client/DeltaV/Harpy/FlightSystem.cs b/Content.Client/DeltaV/Harpy/FlightSystem.cs index 9bb216ff4a8..58f5a396e95 100644 --- a/Content.Client/DeltaV/Harpy/FlightSystem.cs +++ b/Content.Client/DeltaV/Harpy/FlightSystem.cs @@ -1,9 +1,11 @@ +using Robust.Client.GameObjects; using Content.Shared.Humanoid; using Content.Shared.Humanoid.Markings; -using Robust.Client.GameObjects; +using Content.Shared.DeltaV.Harpy; +using Content.Shared.DeltaV.Harpy.Components; using Content.Shared.DeltaV.Harpy.Events; -namespace Content.Shared.DeltaV.Harpy +namespace Content.Client.DeltaV.Harpy { public sealed class FlightSystem : SharedFlightSystem { @@ -22,22 +24,38 @@ private void OnFlight(FlightEvent args) if (!_entityManager.TryGetComponent(uid, out SpriteComponent? sprite)) return; - var wingLayer = GetHarpyWingLayer(uid, sprite); - if (wingLayer == null) - return; - sprite.LayerSetColor(wingLayer.Value, Color.White); + if (args.Layer != string.Empty) + { + var targetLayer = GetAnimatedLayer(uid, args.Layer, sprite); + if (targetLayer == null) + return; + sprite.LayerSetColor(targetLayer.Value, Color.White); + } + + if (args.IsFlying && args.IsAnimated && args.AnimationKey != "default") + { + var comp = new FlyingVisualsComponent + { + AnimationKey = args.AnimationKey, + }; + AddComp(uid, comp, true); + } + if (!args.IsFlying) + { + RemComp(uid); + } } - public int? GetHarpyWingLayer(EntityUid harpyUid, SpriteComponent? sprite = null) + public int? GetAnimatedLayer(EntityUid uid, string targetLayer, SpriteComponent? sprite = null) { - if (!Resolve(harpyUid, ref sprite)) + if (!Resolve(uid, ref sprite)) return null; int index = 0; foreach (var layer in sprite.AllLayers) { // This feels like absolute shitcode, isn't there a better way to check for it? - if (layer.Rsi?.Path.ToString() == "/Textures/Mobs/Customization/Harpy/harpy_wings.rsi") + if (layer.Rsi?.Path.ToString() == targetLayer) { return index; } diff --git a/Content.Client/DeltaV/Harpy/FlyingVisualizerSystem.cs b/Content.Client/DeltaV/Harpy/FlyingVisualizerSystem.cs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/Content.Client/DeltaV/Harpy/FlyingVisualsSystem.cs b/Content.Client/DeltaV/Harpy/FlyingVisualsSystem.cs new file mode 100644 index 00000000000..9417f9c8d34 --- /dev/null +++ b/Content.Client/DeltaV/Harpy/FlyingVisualsSystem.cs @@ -0,0 +1,57 @@ +using Robust.Client.GameObjects; +using Robust.Client.Graphics; +using Robust.Shared.Prototypes; +using Robust.Shared.Random; +using Content.Shared.DeltaV.Harpy.Events; +using Content.Shared.DeltaV.Harpy.Components; + +namespace Content.Client.DeltaV.Harpy; + +/// +/// Handles offsetting an entity while flying +/// +public abstract class FlyingVisualizerSystem : EntitySystem +{ + [Dependency] private readonly IPrototypeManager _protoMan = default!; + [Dependency] private readonly IRobustRandom _random = default!; + private ShaderInstance _shader = default!; + public override void Initialize() + { + base.Initialize(); + + _shader = _protoMan.Index("Wave").InstanceUnique(); + + SubscribeLocalEvent(OnStartup); + SubscribeLocalEvent(OnShutdown); + SubscribeLocalEvent(OnBeforeShaderPost); + } + + private void OnStartup(EntityUid uid, FlyingVisualsComponent comp, ref ComponentStartup args) + { + _shader = _protoMan.Index("Wave").InstanceUnique(); + comp.Offset = _random.NextFloat(0, 1000); + SetShader(uid, _shader); + } + + private void OnShutdown(EntityUid uid, FlyingVisualsComponent comp, ref ComponentShutdown args) + { + SetShader(uid, null); + } + + private void SetShader(Entity entity, ShaderInstance? instance) + { + if (!Resolve(entity, ref entity.Comp, false)) + return; + + entity.Comp.PostShader = instance; + entity.Comp.GetScreenTexture = instance is not null; + entity.Comp.RaiseShaderEvent = instance is not null; + } + + private void OnBeforeShaderPost(EntityUid uid, FlyingVisualsComponent comp, ref BeforePostShaderRenderEvent args) + { + _shader.SetParameter("Speed", comp.Speed); + _shader.SetParameter("Dis", comp.Dis); + _shader.SetParameter("Offset", comp.Offset); + } +} diff --git a/Content.Shared/DeltaV/Harpy/Components/FlyingVisualsComponent.cs b/Content.Shared/DeltaV/Harpy/Components/FlyingVisualsComponent.cs new file mode 100644 index 00000000000..fe1321eaa01 --- /dev/null +++ b/Content.Shared/DeltaV/Harpy/Components/FlyingVisualsComponent.cs @@ -0,0 +1,29 @@ +using System.Numerics; +using Robust.Shared.GameStates; + +namespace Content.Shared.DeltaV.Harpy.Components; + +[RegisterComponent, NetworkedComponent] +public sealed partial class FlyingVisualsComponent : Component +{ + /// + /// How long does the animation last + /// + [DataField] + public float AnimationTime = 2f; + + /// + /// How far it goes in any direction. + /// + [DataField] + public float Offset = 0.2f; + + /// + /// How much the limbs (if there are any) rotate. + /// + [DataField] + public float Rotation = 0.5f; + + [DataField] + public string AnimationKey = default; +} diff --git a/Content.Shared/DeltaV/Harpy/Events.cs b/Content.Shared/DeltaV/Harpy/Events.cs index a07ac450a82..38d67e6f90f 100644 --- a/Content.Shared/DeltaV/Harpy/Events.cs +++ b/Content.Shared/DeltaV/Harpy/Events.cs @@ -20,11 +20,17 @@ public sealed class FlightEvent : EntityEventArgs { public NetEntity Uid { get; } public bool IsFlying { get; } + public bool IsAnimated { get; } + public string Layer { get; } + public string AnimationKey { get; } - public FlightEvent(NetEntity uid, bool isFlying) + public FlightEvent(NetEntity uid, bool isFlying, bool isAnimated, string layer, string animationKey) { Uid = uid; IsFlying = isFlying; + IsAnimated = isAnimated; + Layer = layer; + AnimationKey = animationKey; } } diff --git a/Content.Shared/DeltaV/Harpy/FlightComponent.cs b/Content.Shared/DeltaV/Harpy/FlightComponent.cs index de3c88680b9..30b2fe2947e 100644 --- a/Content.Shared/DeltaV/Harpy/FlightComponent.cs +++ b/Content.Shared/DeltaV/Harpy/FlightComponent.cs @@ -18,6 +18,10 @@ public sealed partial class FlightComponent : Component [DataField, AutoNetworkedField] public EntityUid? ToggleActionEntity; + /// + /// Is the user flying right now? + /// + [ViewVariables(VVAccess.ReadWrite), DataField("on"), AutoNetworkedField] public bool On; @@ -29,20 +33,53 @@ public sealed partial class FlightComponent : Component public float StaminaDrainRate = 3f; /// - /// Delay until the user becomes weightless. + /// DoAfter delay until the user becomes weightless. /// [ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] public float ActivationDelay = 1.0f; + /// + /// Path to a sound specifier or collection for the noises made during flight + /// + [DataField("flapSound")] public SoundSpecifier FlapSound = new SoundCollectionSpecifier("WingFlaps"); /// - /// Time between flap sounds being played + /// Is the flight animated? + /// + + [DataField("isAnimated")] + public bool IsAnimated = true; + + /// + /// Does the animation animate a layer?. + /// + + [DataField("isLayerAnimated")] + public bool IsLayerAnimated = false; + + /// + /// Which RSI layer path does this animate? + /// + + [DataField("layer")] + public string? Layer; + + /// + /// What animation does the flight use? + /// + + [DataField("animationKey")] + public string AnimationKey = "default"; + + /// + /// Time between sounds being played /// [DataField("flapInterval")] public float FlapInterval = 1.25f; + public float TimeUntilFlap; } } \ No newline at end of file diff --git a/Content.Shared/DeltaV/Harpy/FlyingVisualsComponent.cs b/Content.Shared/DeltaV/Harpy/FlyingVisualsComponent.cs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/Content.Shared/DeltaV/Harpy/SharedFlightSystem.cs b/Content.Shared/DeltaV/Harpy/SharedFlightSystem.cs index 6901d0eb140..04880016551 100644 --- a/Content.Shared/DeltaV/Harpy/SharedFlightSystem.cs +++ b/Content.Shared/DeltaV/Harpy/SharedFlightSystem.cs @@ -43,7 +43,7 @@ public void ToggleActive(EntityUid uid, bool active, FlightComponent component) component.TimeUntilFlap = 0f; _actionsSystem.SetToggled(component.ToggleActionEntity, component.On); // Triggers the flight animation - RaiseNetworkEvent(new FlightEvent(GetNetEntity(uid), component.On)); + RaiseNetworkEvent(new FlightEvent(GetNetEntity(uid), component.On, component.IsAnimated, component.Layer ?? string.Empty, component.AnimationKey)); _staminaSystem.ToggleStaminaDrain(uid, component.StaminaDrainRate, active); UpdateHands(uid, active); Dirty(uid, component); diff --git a/Content.Shared/DeltaV/Harpy/SharedFlyingVisualizerSystem.cs b/Content.Shared/DeltaV/Harpy/SharedFlyingVisualizerSystem.cs deleted file mode 100644 index 112baf0a265..00000000000 --- a/Content.Shared/DeltaV/Harpy/SharedFlyingVisualizerSystem.cs +++ /dev/null @@ -1,69 +0,0 @@ -/*namespace Content.Shared.DeltaV.Harpy; - -/// -/// Handles animating a harpy sprite when flying. -/// -public abstract class SharedFlyingVisualizerSystem : EntitySystem -{ - [Dependency] private readonly SharedGravitySystem GravitySystem = default!; - - public override void Initialize() - { - base.Initialize(); - - SubscribeLocalEvent(OnComponentStartup); - SubscribeLocalEvent(OnGravityChanged); - SubscribeLocalEvent(OnEntParentChanged); - } - - /// - /// Offsets a sprite with a linear interpolation animation - /// - 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(); - 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 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); - } -} -*/ \ No newline at end of file diff --git a/Content.Shared/Gravity/SharedFloatingVisualizerSystem.cs b/Content.Shared/Gravity/SharedFloatingVisualizerSystem.cs index 70b38cfff53..ce30c58c3eb 100644 --- a/Content.Shared/Gravity/SharedFloatingVisualizerSystem.cs +++ b/Content.Shared/Gravity/SharedFloatingVisualizerSystem.cs @@ -71,7 +71,7 @@ private void OnFlight(FlightEvent args) return; floating.CanFloat = args.IsFlying; - if (args.IsFlying) + if (args.IsFlying && args.IsAnimated) { FloatAnimation(uid, floating.Offset, floating.AnimationKey, floating.AnimationTime); } diff --git a/Resources/Prototypes/Entities/Mobs/Species/harpy.yml b/Resources/Prototypes/Entities/Mobs/Species/harpy.yml index c7b4323d789..ad1007aee9d 100644 --- a/Resources/Prototypes/Entities/Mobs/Species/harpy.yml +++ b/Resources/Prototypes/Entities/Mobs/Species/harpy.yml @@ -8,6 +8,8 @@ - type: Dash dashAction: "ActionDashFlight" - type: Flight + isLayerAnimated: true + layer: "/Textures/Mobs/Customization/Harpy/harpy_wings.rsi" - type: Singer proto: HarpySinger - type: Sprite