RotatingLightSystem.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Shared.Light;
  2. using Content.Shared.Light.Components;
  3. using Robust.Client.Animations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Shared.Animations;
  6. namespace Content.Client.Light.EntitySystems;
  7. public sealed class RotatingLightSystem : SharedRotatingLightSystem
  8. {
  9. [Dependency] private readonly AnimationPlayerSystem _animations = default!;
  10. private Animation GetAnimation(float speed)
  11. {
  12. var third = 120f / speed;
  13. return new Animation()
  14. {
  15. Length = TimeSpan.FromSeconds(360f / speed),
  16. AnimationTracks =
  17. {
  18. new AnimationTrackComponentProperty
  19. {
  20. ComponentType = typeof(PointLightComponent),
  21. InterpolationMode = AnimationInterpolationMode.Linear,
  22. Property = nameof(PointLightComponent.Rotation),
  23. KeyFrames =
  24. {
  25. new AnimationTrackProperty.KeyFrame(Angle.Zero, 0),
  26. new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(120), third),
  27. new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(240), third),
  28. new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(360), third)
  29. }
  30. }
  31. }
  32. };
  33. }
  34. private const string AnimKey = "rotating_light";
  35. public override void Initialize()
  36. {
  37. base.Initialize();
  38. SubscribeLocalEvent<RotatingLightComponent, ComponentStartup>(OnStartup);
  39. SubscribeLocalEvent<RotatingLightComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
  40. SubscribeLocalEvent<RotatingLightComponent, AnimationCompletedEvent>(OnAnimationComplete);
  41. }
  42. private void OnStartup(EntityUid uid, RotatingLightComponent comp, ComponentStartup args)
  43. {
  44. var player = EnsureComp<AnimationPlayerComponent>(uid);
  45. PlayAnimation(uid, comp, player);
  46. }
  47. private void OnAfterAutoHandleState(EntityUid uid, RotatingLightComponent comp, ref AfterAutoHandleStateEvent args)
  48. {
  49. if (!TryComp<AnimationPlayerComponent>(uid, out var player))
  50. return;
  51. if (comp.Enabled)
  52. {
  53. PlayAnimation(uid, comp, player);
  54. }
  55. else
  56. {
  57. _animations.Stop(uid, player, AnimKey);
  58. }
  59. }
  60. private void OnAnimationComplete(EntityUid uid, RotatingLightComponent comp, AnimationCompletedEvent args)
  61. {
  62. if (!args.Finished)
  63. return;
  64. PlayAnimation(uid, comp);
  65. }
  66. /// <summary>
  67. /// Play the light rotation animation.
  68. /// </summary>
  69. public void PlayAnimation(EntityUid uid, RotatingLightComponent? comp = null, AnimationPlayerComponent? player = null)
  70. {
  71. if (!Resolve(uid, ref comp, ref player) || !comp.Enabled)
  72. return;
  73. if (!_animations.HasRunningAnimation(uid, player, AnimKey))
  74. {
  75. _animations.Play(uid, player, GetAnimation(comp.Speed), AnimKey);
  76. }
  77. }
  78. }