1
0

LightBehaviorSystem.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Linq;
  2. using Content.Client.Light.Components;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Animations;
  5. using Robust.Shared.Random;
  6. using Robust.Shared.Animations;
  7. namespace Content.Client.Light.EntitySystems;
  8. public sealed class LightBehaviorSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IRobustRandom _random = default!;
  11. [Dependency] private readonly AnimationPlayerSystem _player = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<LightBehaviourComponent, ComponentStartup>(OnLightStartup);
  16. SubscribeLocalEvent<LightBehaviourComponent, AnimationCompletedEvent>(OnBehaviorAnimationCompleted);
  17. }
  18. private void OnBehaviorAnimationCompleted(EntityUid uid, LightBehaviourComponent component, AnimationCompletedEvent args)
  19. {
  20. if (!args.Finished)
  21. return;
  22. var container = component.Animations.FirstOrDefault(x => x.FullKey == args.Key);
  23. if (container == null)
  24. {
  25. return;
  26. }
  27. if (container.LightBehaviour.IsLooped)
  28. {
  29. container.LightBehaviour.UpdatePlaybackValues(container.Animation);
  30. _player.Play(uid, container.Animation, container.FullKey);
  31. }
  32. }
  33. private void OnLightStartup(Entity<LightBehaviourComponent> entity, ref ComponentStartup args)
  34. {
  35. // TODO: Do NOT ensure component here. And use eventbus events instead...
  36. EnsureComp<AnimationPlayerComponent>(entity);
  37. foreach (var container in entity.Comp.Animations)
  38. {
  39. container.LightBehaviour.Initialize(entity, _random, EntityManager);
  40. }
  41. // we need to initialize all behaviours before starting any
  42. foreach (var container in entity.Comp.Animations)
  43. {
  44. if (container.LightBehaviour.Enabled)
  45. {
  46. StartLightBehaviour(entity, container.LightBehaviour.ID);
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// If we disable all the light behaviours we want to be able to revert the light to its original state.
  52. /// </summary>
  53. private void CopyLightSettings(Entity<LightBehaviourComponent> entity, string property)
  54. {
  55. if (EntityManager.TryGetComponent(entity, out PointLightComponent? light))
  56. {
  57. var propertyValue = AnimationHelper.GetAnimatableProperty(light, property);
  58. if (propertyValue != null)
  59. {
  60. entity.Comp.OriginalPropertyValues.Add(property, propertyValue);
  61. }
  62. }
  63. else
  64. {
  65. Log.Warning($"{EntityManager.GetComponent<MetaDataComponent>(entity).EntityName} has a {nameof(LightBehaviourComponent)} but it has no {nameof(PointLightComponent)}! Check the prototype!");
  66. }
  67. }
  68. /// <summary>
  69. /// Start animating a light behaviour with the specified ID. If the specified ID is empty, it will start animating all light behaviour entries.
  70. /// If specified light behaviours are already animating, calling this does nothing.
  71. /// Multiple light behaviours can have the same ID.
  72. /// </summary>
  73. public void StartLightBehaviour(Entity<LightBehaviourComponent> entity, string id = "")
  74. {
  75. if (!EntityManager.TryGetComponent(entity, out AnimationPlayerComponent? animation))
  76. {
  77. return;
  78. }
  79. foreach (var container in entity.Comp.Animations)
  80. {
  81. if (container.LightBehaviour.ID == id || id == string.Empty)
  82. {
  83. if (!_player.HasRunningAnimation(entity, animation, LightBehaviourComponent.KeyPrefix + container.Key))
  84. {
  85. CopyLightSettings(entity, container.LightBehaviour.Property);
  86. container.LightBehaviour.UpdatePlaybackValues(container.Animation);
  87. _player.Play(entity, container.Animation, LightBehaviourComponent.KeyPrefix + container.Key);
  88. }
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// If any light behaviour with the specified ID is animating, then stop it.
  94. /// If no ID is specified then all light behaviours will be stopped.
  95. /// Multiple light behaviours can have the same ID.
  96. /// </summary>
  97. /// <param name="id"></param>
  98. /// <param name="removeBehaviour">Should the behaviour(s) also be removed permanently?</param>
  99. /// <param name="resetToOriginalSettings">Should the light have its original settings applied?</param>
  100. public void StopLightBehaviour(Entity<LightBehaviourComponent> entity, string id = "", bool removeBehaviour = false, bool resetToOriginalSettings = false)
  101. {
  102. if (!EntityManager.TryGetComponent(entity, out AnimationPlayerComponent? animation))
  103. {
  104. return;
  105. }
  106. var comp = entity.Comp;
  107. var toRemove = new List<LightBehaviourComponent.AnimationContainer>();
  108. foreach (var container in comp.Animations)
  109. {
  110. if (container.LightBehaviour.ID == id || id == string.Empty)
  111. {
  112. if (_player.HasRunningAnimation(entity, animation, LightBehaviourComponent.KeyPrefix + container.Key))
  113. {
  114. _player.Stop(entity, animation, LightBehaviourComponent.KeyPrefix + container.Key);
  115. }
  116. if (removeBehaviour)
  117. {
  118. toRemove.Add(container);
  119. }
  120. }
  121. }
  122. foreach (var container in toRemove)
  123. {
  124. comp.Animations.Remove(container);
  125. }
  126. if (resetToOriginalSettings && EntityManager.TryGetComponent(entity, out PointLightComponent? light))
  127. {
  128. foreach (var (property, value) in comp.OriginalPropertyValues)
  129. {
  130. AnimationHelper.SetAnimatableProperty(light, property, value);
  131. }
  132. }
  133. comp.OriginalPropertyValues.Clear();
  134. }
  135. /// <summary>
  136. /// Checks if at least one behaviour is running.
  137. /// </summary>
  138. /// <returns>Whether at least one behaviour is running, false if none is.</returns>
  139. public bool HasRunningBehaviours(Entity<LightBehaviourComponent> entity)
  140. {
  141. //var uid = Owner;
  142. if (!EntityManager.TryGetComponent(entity, out AnimationPlayerComponent? animation))
  143. {
  144. return false;
  145. }
  146. return entity.Comp.Animations.Any(container => _player.HasRunningAnimation(entity, animation, LightBehaviourComponent.KeyPrefix + container.Key));
  147. }
  148. /// <summary>
  149. /// Add a new light behaviour to the component and start it immediately unless otherwise specified.
  150. /// </summary>
  151. public void AddNewLightBehaviour(Entity<LightBehaviourComponent> entity, LightBehaviourAnimationTrack behaviour, bool playImmediately = true)
  152. {
  153. var key = 0;
  154. var comp = entity.Comp;
  155. while (comp.Animations.Any(x => x.Key == key))
  156. {
  157. key++;
  158. }
  159. var animation = new Animation()
  160. {
  161. AnimationTracks = { behaviour }
  162. };
  163. behaviour.Initialize(entity.Owner, _random, EntityManager);
  164. var container = new LightBehaviourComponent.AnimationContainer(key, animation, behaviour);
  165. comp.Animations.Add(container);
  166. if (playImmediately)
  167. {
  168. StartLightBehaviour(entity, behaviour.ID);
  169. }
  170. }
  171. }