Files
tbd-station-14/Content.Client/Light/EmergencyLightSystem.cs
metalgearsloth 91665cf0e6 Optimise emergency lights a bit (#4567)
* Optimise emergency lights a bit

* Fixes

* Delete the failing test, sinple

* Revert "Delete the failing test, sinple"

This reverts commit 7d9e3a3bb6975c15add2987e39e0d3ba85d88be3.

* Start server and fix test

It just werks
2021-09-27 14:51:17 +10:00

87 lines
3.1 KiB
C#

using System;
using Content.Client.Light.Components;
using Content.Shared.Light;
using Content.Shared.Light.Component;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Maths;
namespace Content.Client.Light
{
public sealed class EmergencyLightSystem : SharedEmergencyLightSystem
{
private static Animation Animation =>
new()
{
Length = TimeSpan.FromSeconds(4),
AnimationTracks =
{
new AnimationTrackComponentProperty
{
ComponentType = typeof(PointLightComponent),
InterpolationMode = AnimationInterpolationMode.Linear,
Property = nameof(PointLightComponent.Rotation),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(Angle.Zero, 0),
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(1080), 4)
}
}
}
};
private const string AnimKey = "emergency";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EmergencyLightComponent, ComponentStartup>(HandleStartup);
SubscribeLocalEvent<EmergencyLightComponent, AnimationCompletedEvent>(HandleAnimationComplete);
SubscribeLocalEvent<EmergencyLightComponent, ComponentHandleState>(HandleCompState);
}
private void HandleCompState(EntityUid uid, EmergencyLightComponent component, ref ComponentHandleState args)
{
if (args.Current is not EmergencyLightComponentState state) return;
if (component.Enabled == state.Enabled) return;
var playerComponent = component.Owner.EnsureComponent<AnimationPlayerComponent>();
component.Enabled = state.Enabled;
if (component.Enabled && !playerComponent.HasRunningAnimation(AnimKey))
playerComponent.Play(Animation, AnimKey);
if (!component.Enabled)
playerComponent.Stop(AnimKey);
}
private void HandleAnimationComplete(EntityUid uid, EmergencyLightComponent component, AnimationCompletedEvent args)
{
if (!component.Enabled ||
!ComponentManager.TryGetComponent<AnimationPlayerComponent>(uid, out var playerComponent)) return;
playerComponent.Play(Animation, AnimKey);
}
private void HandleStartup(EntityUid uid, EmergencyLightComponent component, ComponentStartup args)
{
PlayAnimation(component);
}
private void PlayAnimation(EmergencyLightComponent component)
{
if (!component.Enabled) return;
var playerComponent = component.Owner.EnsureComponent<AnimationPlayerComponent>();
if (!playerComponent.HasRunningAnimation(AnimKey))
playerComponent.Play(Animation, AnimKey);
}
}
}