From bfedfd0abbc78e38e598ac78bb670c20bf449ec6 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 7 Feb 2023 12:46:49 -0800 Subject: [PATCH] Resolves VaporVisualizer is Obsolete (#13882) --- .../Chemistry/Visualizers/VaporVisualizer.cs | 76 ------------------- .../Visualizers/VaporVisualizerSystem.cs | 62 +++++++++++++++ .../Visualizers/VaporVisualsComponent.cs | 34 +++++++++ .../Objects/Misc/fire_extinguisher.yml | 7 +- .../Objects/Specific/Janitorial/spray.yml | 3 +- 5 files changed, 100 insertions(+), 82 deletions(-) delete mode 100644 Content.Client/Chemistry/Visualizers/VaporVisualizer.cs create mode 100644 Content.Client/Chemistry/Visualizers/VaporVisualizerSystem.cs create mode 100644 Content.Client/Chemistry/Visualizers/VaporVisualsComponent.cs diff --git a/Content.Client/Chemistry/Visualizers/VaporVisualizer.cs b/Content.Client/Chemistry/Visualizers/VaporVisualizer.cs deleted file mode 100644 index 3b40a179b2..0000000000 --- a/Content.Client/Chemistry/Visualizers/VaporVisualizer.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using Content.Shared.Vapor; -using JetBrains.Annotations; -using Robust.Client.Animations; -using Robust.Client.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Maths; -using Robust.Shared.Serialization; -using Robust.Shared.Serialization.Manager.Attributes; - -namespace Content.Client.Chemistry.Visualizers -{ - [UsedImplicitly] - public sealed class VaporVisualizer : AppearanceVisualizer, ISerializationHooks - { - private const string AnimationKey = "flick_animation"; - - [DataField("animation_time")] - private float _delay = 0.25f; - - [DataField("animation_state")] - private string _state = "chempuff"; - - private Animation VaporFlick = default!; - - void ISerializationHooks.AfterDeserialization() - { - VaporFlick = new Animation {Length = TimeSpan.FromSeconds(_delay)}; - { - var flick = new AnimationTrackSpriteFlick(); - VaporFlick.AnimationTracks.Add(flick); - flick.LayerKey = VaporVisualLayers.Base; - flick.KeyFrames.Add(new AnimationTrackSpriteFlick.KeyFrame(_state, 0f)); - } - } - - [Obsolete("Subscribe to AppearanceChangeEvent instead.")] - public override void OnChangeData(AppearanceComponent component) - { - base.OnChangeData(component); - - if (component.TryGetData(VaporVisuals.Color, out var color)) - { - SetColor(component, color); - } - - if (component.TryGetData(VaporVisuals.State, out var state)) - { - SetState(component, state); - } - } - - private void SetState(AppearanceComponent component, bool state) - { - if (!state) return; - - var animPlayer = IoCManager.Resolve().GetComponent(component.Owner); - - if(!animPlayer.HasRunningAnimation(AnimationKey)) - animPlayer.Play(VaporFlick, AnimationKey); - } - - private void SetColor(AppearanceComponent component, Color color) - { - var sprite = IoCManager.Resolve().GetComponent(component.Owner); - - sprite.Color = color; - } - } - - public enum VaporVisualLayers : byte - { - Base - } -} diff --git a/Content.Client/Chemistry/Visualizers/VaporVisualizerSystem.cs b/Content.Client/Chemistry/Visualizers/VaporVisualizerSystem.cs new file mode 100644 index 0000000000..42e8f40f9a --- /dev/null +++ b/Content.Client/Chemistry/Visualizers/VaporVisualizerSystem.cs @@ -0,0 +1,62 @@ +using Content.Shared.Vapor; +using Robust.Client.Animations; +using Robust.Client.GameObjects; + +namespace Content.Client.Chemistry.Visualizers; + +/// +/// Handles vapor playing the 'being sprayed' animation if necessary. +/// +public sealed class VaporVisualizerSystem : VisualizerSystem +{ + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnComponentInit); + } + + /// + /// Constructs the 'being sprayed' animation for the vapor entity. + /// + private void OnComponentInit(EntityUid uid, VaporVisualsComponent comp, ComponentInit args) + { + comp.VaporFlick = new Animation() + { + Length = TimeSpan.FromSeconds(comp.AnimationTime), + AnimationTracks = + { + new AnimationTrackSpriteFlick() + { + LayerKey = VaporVisualLayers.Base, + KeyFrames = + { + new AnimationTrackSpriteFlick.KeyFrame(comp.AnimationState, 0f) + } + } + } + }; + } + + /// + /// Ensures that the vapor entity plays its 'being sprayed' animation if necessary. + /// + protected override void OnAppearanceChange(EntityUid uid, VaporVisualsComponent comp, ref AppearanceChangeEvent args) + { + if (AppearanceSystem.TryGetData(uid, VaporVisuals.Color, out var color, args.Component) && args.Sprite != null) + { + args.Sprite.Color = color; + } + + if ((AppearanceSystem.TryGetData(uid, VaporVisuals.State, out var state, args.Component) && state) && + TryComp(uid, out var animPlayer) && + !AnimationSystem.HasRunningAnimation(uid, animPlayer, VaporVisualsComponent.AnimationKey)) + { + AnimationSystem.Play(uid, animPlayer, comp.VaporFlick, VaporVisualsComponent.AnimationKey); + } + } +} + +public enum VaporVisualLayers : byte +{ + Base +} diff --git a/Content.Client/Chemistry/Visualizers/VaporVisualsComponent.cs b/Content.Client/Chemistry/Visualizers/VaporVisualsComponent.cs new file mode 100644 index 0000000000..82e004ee82 --- /dev/null +++ b/Content.Client/Chemistry/Visualizers/VaporVisualsComponent.cs @@ -0,0 +1,34 @@ +using Robust.Client.Animations; + +namespace Content.Client.Chemistry.Visualizers; + +/// +/// A component that plays an animation when it is sprayed. +/// +[RegisterComponent] +[Access(typeof(VaporVisualizerSystem))] +public sealed class VaporVisualsComponent : Component +{ + /// + /// The id of the animation played when the vapor spawns in. + /// + public const string AnimationKey = "flick_animation"; + + /// + /// The amount of time over which the spray animation is played. + /// + [DataField("animationTime")] + public float AnimationTime = 0.25f; + + /// + /// The RSI state that is flicked when the vapor is sprayed. + /// + [DataField("animationState")] + public string AnimationState = "chempuff"; + + /// + /// The animation that plays when the vapor is sprayed. + /// Generated in + /// + public Animation VaporFlick = default!; +} diff --git a/Resources/Prototypes/Entities/Objects/Misc/fire_extinguisher.yml b/Resources/Prototypes/Entities/Objects/Misc/fire_extinguisher.yml index 2805c8f581..9918e03592 100644 --- a/Resources/Prototypes/Entities/Objects/Misc/fire_extinguisher.yml +++ b/Resources/Prototypes/Entities/Objects/Misc/fire_extinguisher.yml @@ -65,7 +65,6 @@ - type: Physics bodyType: Dynamic - type: Appearance - visuals: - - type: VaporVisualizer - animation_delay: 0.8 - animation_state: extinguish + - type: VaporVisuals + animationTime: 0.8 + animationState: extinguish diff --git a/Resources/Prototypes/Entities/Objects/Specific/Janitorial/spray.yml b/Resources/Prototypes/Entities/Objects/Specific/Janitorial/spray.yml index 5fcb6712a8..3bb9c97fd9 100644 --- a/Resources/Prototypes/Entities/Objects/Specific/Janitorial/spray.yml +++ b/Resources/Prototypes/Entities/Objects/Specific/Janitorial/spray.yml @@ -101,5 +101,4 @@ - FullTileMask - Opaque - type: Appearance - visuals: - - type: VaporVisualizer + - type: VaporVisuals