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
This commit is contained in:
@@ -1,45 +1,12 @@
|
||||
using System;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.Animations;
|
||||
using Content.Shared.Light.Component;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Client.Light.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class EmergencyLightComponent : Component
|
||||
[NetworkedComponent]
|
||||
public class EmergencyLightComponent : SharedEmergencyLightComponent
|
||||
{
|
||||
public override string Name => "EmergencyLight";
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
var animation = new Animation
|
||||
{
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var playerComponent = Owner.EnsureComponent<AnimationPlayerComponent>();
|
||||
playerComponent.Play(animation, "emergency");
|
||||
|
||||
playerComponent.AnimationCompleted += s => playerComponent.Play(animation, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
86
Content.Client/Light/EmergencyLightSystem.cs
Normal file
86
Content.Client/Light/EmergencyLightSystem.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,10 +33,6 @@ namespace Content.Client.Light
|
||||
public override void OnChangeData(AppearanceComponent component)
|
||||
{
|
||||
base.OnChangeData(component);
|
||||
if (component.Deleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PlayAnimation(component);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.IntegrationTests.Tests
|
||||
@@ -14,8 +15,7 @@ namespace Content.IntegrationTests.Tests
|
||||
[Test]
|
||||
public async Task Test()
|
||||
{
|
||||
var client = StartClient();
|
||||
await client.WaitIdleAsync();
|
||||
var (client, _) = await StartConnectedServerClientPair();
|
||||
|
||||
var prototypeManager = client.ResolveDependency<IPrototypeManager>();
|
||||
var resourceCache = client.ResolveDependency<IResourceCache>();
|
||||
|
||||
@@ -16,10 +16,8 @@ namespace Content.Server.Light.Components
|
||||
/// Component that represents an emergency light, it has an internal battery that charges when the power is on.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class EmergencyLightComponent : Component, IExamine
|
||||
public class EmergencyLightComponent : SharedEmergencyLightComponent, IExamine
|
||||
{
|
||||
public override string Name => "EmergencyLight";
|
||||
|
||||
[ViewVariables]
|
||||
private EmergencyLightState State
|
||||
{
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Light.Components;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Component;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Server.Light.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class EmergencyLightSystem : EntitySystem
|
||||
public sealed class EmergencyLightSystem : SharedEmergencyLightSystem
|
||||
{
|
||||
private readonly HashSet<EmergencyLightComponent> _activeLights = new();
|
||||
|
||||
@@ -15,6 +18,20 @@ namespace Content.Server.Light.EntitySystems
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<EmergencyLightMessage>(HandleEmergencyLightMessage);
|
||||
SubscribeLocalEvent<EmergencyLightComponent, ComponentGetState>(GetCompState);
|
||||
SubscribeLocalEvent<EmergencyLightComponent, PointLightToggleEvent>(HandleLightToggle);
|
||||
}
|
||||
|
||||
private void HandleLightToggle(EntityUid uid, EmergencyLightComponent component, PointLightToggleEvent args)
|
||||
{
|
||||
if (component.Enabled == args.Enabled) return;
|
||||
component.Enabled = args.Enabled;
|
||||
component.Dirty();
|
||||
}
|
||||
|
||||
private void GetCompState(EntityUid uid, EmergencyLightComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new EmergencyLightComponentState(component.Enabled);
|
||||
}
|
||||
|
||||
private void HandleEmergencyLightMessage(EmergencyLightMessage message)
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Light.Component
|
||||
{
|
||||
[NetworkedComponent]
|
||||
public abstract class SharedEmergencyLightComponent : Robust.Shared.GameObjects.Component
|
||||
{
|
||||
public override string Name => "EmergencyLight";
|
||||
|
||||
public bool Enabled { get; set; } = false;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class EmergencyLightComponentState : ComponentState
|
||||
{
|
||||
public bool Enabled;
|
||||
|
||||
public EmergencyLightComponentState(bool enabled)
|
||||
{
|
||||
Enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum EmergencyLightVisuals
|
||||
{
|
||||
|
||||
9
Content.Shared/Light/SharedEmergencyLightSystem.cs
Normal file
9
Content.Shared/Light/SharedEmergencyLightSystem.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Light
|
||||
{
|
||||
public abstract class SharedEmergencyLightSystem : EntitySystem
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user