Files
tbd-station-14/Content.Client/GameObjects/Components/FlashLightVisualizer.cs
SoulSloth df823d2245 Add Flashlight Visualizer/states (#1861)
* Add art assets for cloning

* Added a 'Scan DNA' button to the medical scanner

* Made the UI update unconditional for the medical scanner until checks for power changes are in place

* Update Medical scanner to reflect powered status and fix #1774

* added a 'scan dna' button the the medical scanner that will add the contained bodies Uid to a list in CloningSystem, fixed an issue with the menu not populating if the scanner starts in an unpowered state

* Add disabling logic to 'Scan DNA' button on medical scanner

* Removed un-used libraries

* Added playing parameter to radiatingLightComponent, changed it's animation key to radiatingLight

* refactored RadiatingLight into a visualizer

* Added different light animations for differnt power states of a flashlight

* split out the radiating light visualizer into two seperate visualizers

* further refactored and tweaked handheldlight animations

* further lantern light tweaks

* removed un-used attributes in flashlight and lantern prototypes

* fix null check in handheldlightcomponent
2020-08-24 12:32:18 +02:00

120 lines
4.7 KiB
C#

using System;
using Content.Shared.GameObjects.Components;
using JetBrains.Annotations;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Client.GameObjects.Components.Animations;
using Robust.Shared.Animations;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components
{
[UsedImplicitly]
public class FlashLightVisualizer : AppearanceVisualizer
{
private readonly Animation _radiatingLightAnimation = new Animation
{
Length = TimeSpan.FromSeconds(1),
AnimationTracks =
{
new AnimationTrackComponentProperty
{
ComponentType = typeof(PointLightComponent),
InterpolationMode = AnimationInterpolationMode.Linear,
Property = nameof(PointLightComponent.Radius),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(3.0f, 0),
new AnimationTrackProperty.KeyFrame(2.0f, 0.5f),
new AnimationTrackProperty.KeyFrame(3.0f, 1)
}
}
}
};
private readonly Animation _blinkingLightAnimation = new Animation
{
Length = TimeSpan.FromSeconds(1),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
ComponentType = typeof(PointLightComponent),
//To create the blinking effect we go from nearly zero radius, to the light radius, and back
//We do this instead of messing with the `PointLightComponent.enabled` because we don't want the animation to affect component behavior
InterpolationMode = AnimationInterpolationMode.Nearest,
Property = nameof(PointLightComponent.Radius),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(0.1f, 0),
new AnimationTrackProperty.KeyFrame(2f, 0.5f),
new AnimationTrackProperty.KeyFrame(0.1f, 1)
}
}
}
};
private Action<string> _radiatingCallback;
private Action<string> _blinkingCallback;
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
if (component.Deleted)
{
return;
}
if (component.TryGetData(HandheldLightVisuals.Power,
out HandheldLightPowerStates state))
{
PlayAnimation(component, state);
}
}
private void PlayAnimation(AppearanceComponent component, HandheldLightPowerStates state)
{
component.Owner.EnsureComponent(out AnimationPlayerComponent animationPlayer);
switch (state)
{
case HandheldLightPowerStates.LowPower:
if (!animationPlayer.HasRunningAnimation("radiatingLight"))
{
animationPlayer.Play(_radiatingLightAnimation, "radiatingLight");
_radiatingCallback = (s) => animationPlayer.Play(_radiatingLightAnimation, s);
animationPlayer.AnimationCompleted += _radiatingCallback;
}
break;
case HandheldLightPowerStates.Dying:
animationPlayer.Stop("radiatingLight");
animationPlayer.AnimationCompleted -= _radiatingCallback;
if (!animationPlayer.HasRunningAnimation("blinkingLight"))
{
animationPlayer.Play(_blinkingLightAnimation, "blinkingLight");
_blinkingCallback = (s) => animationPlayer.Play(_blinkingLightAnimation, s);
animationPlayer.AnimationCompleted += _blinkingCallback;
}
break;
case HandheldLightPowerStates.FullPower:
if (animationPlayer.HasRunningAnimation("blinkingLight"))
{
animationPlayer.Stop("blinkingLight");
animationPlayer.AnimationCompleted -= _blinkingCallback;
}
if (animationPlayer.HasRunningAnimation("radiatingLight"))
{
animationPlayer.Stop("radiatingLight");
animationPlayer.AnimationCompleted -= _radiatingCallback;
}
break;
}
}
}
}