ExpendableLightSystem.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Content.Server.Light.Components;
  2. using Content.Shared.Clothing.Components;
  3. using Content.Shared.Clothing.EntitySystems;
  4. using Content.Shared.IgnitionSource;
  5. using Content.Shared.Interaction.Events;
  6. using Content.Shared.Item;
  7. using Content.Shared.Light.Components;
  8. using Content.Shared.Tag;
  9. using Content.Shared.Verbs;
  10. using JetBrains.Annotations;
  11. using Robust.Server.GameObjects;
  12. using Robust.Shared.Audio.Systems;
  13. using Robust.Shared.Utility;
  14. namespace Content.Server.Light.EntitySystems
  15. {
  16. [UsedImplicitly]
  17. public sealed class ExpendableLightSystem : EntitySystem
  18. {
  19. [Dependency] private readonly SharedItemSystem _item = default!;
  20. [Dependency] private readonly ClothingSystem _clothing = default!;
  21. [Dependency] private readonly TagSystem _tagSystem = default!;
  22. [Dependency] private readonly SharedAudioSystem _audio = default!;
  23. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  24. [Dependency] private readonly MetaDataSystem _metaData = default!;
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. SubscribeLocalEvent<ExpendableLightComponent, ComponentInit>(OnExpLightInit);
  29. SubscribeLocalEvent<ExpendableLightComponent, UseInHandEvent>(OnExpLightUse);
  30. SubscribeLocalEvent<ExpendableLightComponent, GetVerbsEvent<ActivationVerb>>(AddIgniteVerb);
  31. }
  32. public override void Update(float frameTime)
  33. {
  34. var query = EntityQueryEnumerator<ExpendableLightComponent>();
  35. while (query.MoveNext(out var uid, out var light))
  36. {
  37. UpdateLight((uid, light), frameTime);
  38. }
  39. }
  40. private void UpdateLight(Entity<ExpendableLightComponent> ent, float frameTime)
  41. {
  42. var component = ent.Comp;
  43. if (!component.Activated)
  44. return;
  45. component.StateExpiryTime -= frameTime;
  46. if (component.StateExpiryTime <= 0f)
  47. {
  48. switch (component.CurrentState)
  49. {
  50. case ExpendableLightState.Lit:
  51. component.CurrentState = ExpendableLightState.Fading;
  52. component.StateExpiryTime = component.FadeOutDuration;
  53. UpdateVisualizer(ent);
  54. break;
  55. default:
  56. case ExpendableLightState.Fading:
  57. component.CurrentState = ExpendableLightState.Dead;
  58. var meta = MetaData(ent);
  59. _metaData.SetEntityName(ent, Loc.GetString(component.SpentName), meta);
  60. _metaData.SetEntityDescription(ent, Loc.GetString(component.SpentDesc), meta);
  61. _tagSystem.AddTag(ent, "Trash");
  62. UpdateSounds(ent);
  63. UpdateVisualizer(ent);
  64. if (TryComp<ItemComponent>(ent, out var item))
  65. {
  66. _item.SetHeldPrefix(ent, "unlit", component: item);
  67. }
  68. break;
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Enables the light if it is not active. Once active it cannot be turned off.
  74. /// </summary>
  75. public bool TryActivate(Entity<ExpendableLightComponent> ent)
  76. {
  77. var component = ent.Comp;
  78. if (!component.Activated && component.CurrentState == ExpendableLightState.BrandNew)
  79. {
  80. if (TryComp<ItemComponent>(ent, out var item))
  81. {
  82. _item.SetHeldPrefix(ent, "lit", component: item);
  83. }
  84. var ignite = new IgnitionEvent(true);
  85. RaiseLocalEvent(ent, ref ignite);
  86. component.CurrentState = ExpendableLightState.Lit;
  87. component.StateExpiryTime = component.GlowDuration;
  88. UpdateSounds(ent);
  89. UpdateVisualizer(ent);
  90. return true;
  91. }
  92. return false;
  93. }
  94. private void UpdateVisualizer(Entity<ExpendableLightComponent> ent, AppearanceComponent? appearance = null)
  95. {
  96. var component = ent.Comp;
  97. if (!Resolve(ent, ref appearance, false))
  98. return;
  99. _appearance.SetData(ent, ExpendableLightVisuals.State, component.CurrentState, appearance);
  100. switch (component.CurrentState)
  101. {
  102. case ExpendableLightState.Lit:
  103. _appearance.SetData(ent, ExpendableLightVisuals.Behavior, component.TurnOnBehaviourID, appearance);
  104. break;
  105. case ExpendableLightState.Fading:
  106. _appearance.SetData(ent, ExpendableLightVisuals.Behavior, component.FadeOutBehaviourID, appearance);
  107. break;
  108. case ExpendableLightState.Dead:
  109. _appearance.SetData(ent, ExpendableLightVisuals.Behavior, string.Empty, appearance);
  110. var ignite = new IgnitionEvent(false);
  111. RaiseLocalEvent(ent, ref ignite);
  112. break;
  113. }
  114. }
  115. private void UpdateSounds(Entity<ExpendableLightComponent> ent)
  116. {
  117. var component = ent.Comp;
  118. switch (component.CurrentState)
  119. {
  120. case ExpendableLightState.Lit:
  121. _audio.PlayPvs(component.LitSound, ent);
  122. break;
  123. case ExpendableLightState.Fading:
  124. break;
  125. default:
  126. _audio.PlayPvs(component.DieSound, ent);
  127. break;
  128. }
  129. if (TryComp<ClothingComponent>(ent, out var clothing))
  130. {
  131. _clothing.SetEquippedPrefix(ent, component.Activated ? "Activated" : string.Empty, clothing);
  132. }
  133. }
  134. private void OnExpLightInit(EntityUid uid, ExpendableLightComponent component, ComponentInit args)
  135. {
  136. if (TryComp<ItemComponent>(uid, out var item))
  137. {
  138. _item.SetHeldPrefix(uid, "unlit", component: item);
  139. }
  140. component.CurrentState = ExpendableLightState.BrandNew;
  141. EntityManager.EnsureComponent<PointLightComponent>(uid);
  142. }
  143. private void OnExpLightUse(Entity<ExpendableLightComponent> ent, ref UseInHandEvent args)
  144. {
  145. if (args.Handled)
  146. return;
  147. if (TryActivate(ent))
  148. args.Handled = true;
  149. }
  150. private void AddIgniteVerb(Entity<ExpendableLightComponent> ent, ref GetVerbsEvent<ActivationVerb> args)
  151. {
  152. if (!args.CanAccess || !args.CanInteract)
  153. return;
  154. if (ent.Comp.CurrentState != ExpendableLightState.BrandNew)
  155. return;
  156. // Ignite the flare or make the glowstick glow.
  157. // Also hot damn, those are some shitty glowsticks, we need to get a refund.
  158. ActivationVerb verb = new()
  159. {
  160. Text = Loc.GetString("expendable-light-start-verb"),
  161. Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
  162. Act = () => TryActivate(ent)
  163. };
  164. args.Verbs.Add(verb);
  165. }
  166. }
  167. }