light refactoring/rework (#19314)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -1,18 +1,8 @@
|
||||
using Content.Shared.Light.Component;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Content.Shared.Light.Components;
|
||||
|
||||
namespace Content.Client.Light.Components;
|
||||
|
||||
[RegisterComponent]
|
||||
[NetworkedComponent]
|
||||
public sealed partial class EmergencyLightComponent : SharedEmergencyLightComponent
|
||||
{
|
||||
}
|
||||
|
||||
public enum EmergencyLightVisualLayers
|
||||
{
|
||||
Base,
|
||||
LightOff,
|
||||
LightOn,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Content.Client.Light.EntitySystems;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
namespace Content.Client.Light.Components;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
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 const float DegreesPerSecond = 90;
|
||||
private static Animation Animation =>
|
||||
new()
|
||||
{
|
||||
Length = TimeSpan.FromSeconds(360f/ DegreesPerSecond),
|
||||
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(120), 120f/DegreesPerSecond),
|
||||
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(240), 120f/DegreesPerSecond),
|
||||
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(360), 120f/DegreesPerSecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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 ||
|
||||
!EntityManager.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Content.Client.Light.Components;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
namespace Content.Client.Light.EntitySystems;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Content.Client.Light.Components;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
namespace Content.Client.Light.Visualizers;
|
||||
|
||||
89
Content.Client/Light/EntitySystems/RotatingLightSystem.cs
Normal file
89
Content.Client/Light/EntitySystems/RotatingLightSystem.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Components;
|
||||
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.Systems;
|
||||
|
||||
public sealed class RotatingLightSystem : SharedRotatingLightSystem
|
||||
{
|
||||
private Animation GetAnimation(float speed)
|
||||
{
|
||||
var third = 120f / speed;
|
||||
return new Animation()
|
||||
{
|
||||
Length = TimeSpan.FromSeconds(360f / speed),
|
||||
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(120), third),
|
||||
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(240), third),
|
||||
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(360), third)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private const string AnimKey = "rotating_light";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<RotatingLightComponent, ComponentStartup>(OnStartup);
|
||||
SubscribeLocalEvent<RotatingLightComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
|
||||
SubscribeLocalEvent<RotatingLightComponent, AnimationCompletedEvent>(OnAnimationComplete);
|
||||
}
|
||||
|
||||
private void OnStartup(EntityUid uid, RotatingLightComponent comp, ComponentStartup args)
|
||||
{
|
||||
var player = EnsureComp<AnimationPlayerComponent>(uid);
|
||||
PlayAnimation(uid, comp, player);
|
||||
}
|
||||
|
||||
private void OnAfterAutoHandleState(EntityUid uid, RotatingLightComponent comp, ref AfterAutoHandleStateEvent args)
|
||||
{
|
||||
if (!TryComp<AnimationPlayerComponent>(uid, out var player))
|
||||
return;
|
||||
|
||||
if (comp.Enabled)
|
||||
{
|
||||
PlayAnimation(uid, comp, player);
|
||||
}
|
||||
else
|
||||
{
|
||||
player.Stop(AnimKey);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAnimationComplete(EntityUid uid, RotatingLightComponent comp, AnimationCompletedEvent args)
|
||||
{
|
||||
PlayAnimation(uid, comp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play the light rotation animation.
|
||||
/// </summary>
|
||||
public void PlayAnimation(EntityUid uid, RotatingLightComponent? comp = null, AnimationPlayerComponent? player = null)
|
||||
{
|
||||
if (!Resolve(uid, ref comp, ref player) || !comp.Enabled)
|
||||
return;
|
||||
|
||||
if (!player.HasRunningAnimation(AnimKey))
|
||||
{
|
||||
player.Play(GetAnimation(comp.Speed), AnimKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Content.Client.Items;
|
||||
using Content.Client.Light.Components;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Components;
|
||||
using Content.Shared.Toggleable;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
@@ -4,7 +4,7 @@ using Content.Shared.Clothing;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Component;
|
||||
using Content.Shared.Light.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map.Components;
|
||||
|
||||
Reference in New Issue
Block a user