HandheldLightSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Content.Server.Actions;
  2. using Content.Server.Popups;
  3. using Content.Server.Power.EntitySystems;
  4. using Content.Server.PowerCell;
  5. using Content.Shared.Actions;
  6. using Content.Shared.Examine;
  7. using Content.Shared.Interaction;
  8. using Content.Shared.Light;
  9. using Content.Shared.Light.Components;
  10. using Content.Shared.Rounding;
  11. using Content.Shared.Toggleable;
  12. using JetBrains.Annotations;
  13. using Robust.Server.GameObjects;
  14. using Robust.Shared.Audio;
  15. using Robust.Shared.Audio.Systems;
  16. using Robust.Shared.Containers;
  17. using Robust.Shared.GameStates;
  18. using Robust.Shared.Utility;
  19. namespace Content.Server.Light.EntitySystems
  20. {
  21. public sealed class HandheldLightSystem : SharedHandheldLightSystem
  22. {
  23. [Dependency] private readonly ActionsSystem _actions = default!;
  24. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  25. [Dependency] private readonly PopupSystem _popup = default!;
  26. [Dependency] private readonly PowerCellSystem _powerCell = default!;
  27. [Dependency] private readonly BatterySystem _battery = default!;
  28. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  29. [Dependency] private readonly SharedAudioSystem _audio = default!;
  30. [Dependency] private readonly SharedPointLightSystem _lights = default!;
  31. // TODO: Ideally you'd be able to subscribe to power stuff to get events at certain percentages.. or something?
  32. // But for now this will be better anyway.
  33. private readonly HashSet<Entity<HandheldLightComponent>> _activeLights = new();
  34. public override void Initialize()
  35. {
  36. base.Initialize();
  37. SubscribeLocalEvent<HandheldLightComponent, ComponentRemove>(OnRemove);
  38. SubscribeLocalEvent<HandheldLightComponent, ComponentGetState>(OnGetState);
  39. SubscribeLocalEvent<HandheldLightComponent, MapInitEvent>(OnMapInit);
  40. SubscribeLocalEvent<HandheldLightComponent, ComponentShutdown>(OnShutdown);
  41. SubscribeLocalEvent<HandheldLightComponent, ExaminedEvent>(OnExamine);
  42. SubscribeLocalEvent<HandheldLightComponent, ActivateInWorldEvent>(OnActivate);
  43. SubscribeLocalEvent<HandheldLightComponent, GetItemActionsEvent>(OnGetActions);
  44. SubscribeLocalEvent<HandheldLightComponent, ToggleActionEvent>(OnToggleAction);
  45. SubscribeLocalEvent<HandheldLightComponent, EntInsertedIntoContainerMessage>(OnEntInserted);
  46. SubscribeLocalEvent<HandheldLightComponent, EntRemovedFromContainerMessage>(OnEntRemoved);
  47. }
  48. private void OnEntInserted(Entity<HandheldLightComponent> ent, ref EntInsertedIntoContainerMessage args)
  49. {
  50. // Not guaranteed to be the correct container for our slot, I don't care.
  51. UpdateLevel(ent);
  52. }
  53. private void OnEntRemoved(Entity<HandheldLightComponent> ent, ref EntRemovedFromContainerMessage args)
  54. {
  55. // Ditto above
  56. UpdateLevel(ent);
  57. }
  58. private void OnGetActions(EntityUid uid, HandheldLightComponent component, GetItemActionsEvent args)
  59. {
  60. args.AddAction(ref component.ToggleActionEntity, component.ToggleAction);
  61. }
  62. private void OnToggleAction(Entity<HandheldLightComponent> ent, ref ToggleActionEvent args)
  63. {
  64. if (args.Handled)
  65. return;
  66. if (ent.Comp.Activated)
  67. TurnOff(ent);
  68. else
  69. TurnOn(args.Performer, ent);
  70. args.Handled = true;
  71. }
  72. private void OnGetState(Entity<HandheldLightComponent> ent, ref ComponentGetState args)
  73. {
  74. args.State = new HandheldLightComponent.HandheldLightComponentState(ent.Comp.Activated, GetLevel(ent));
  75. }
  76. private void OnMapInit(Entity<HandheldLightComponent> ent, ref MapInitEvent args)
  77. {
  78. var component = ent.Comp;
  79. _actionContainer.EnsureAction(ent, ref component.ToggleActionEntity, component.ToggleAction);
  80. _actions.AddAction(ent, ref component.SelfToggleActionEntity, component.ToggleAction);
  81. }
  82. private void OnShutdown(EntityUid uid, HandheldLightComponent component, ComponentShutdown args)
  83. {
  84. _actions.RemoveAction(uid, component.ToggleActionEntity);
  85. _actions.RemoveAction(uid, component.SelfToggleActionEntity);
  86. }
  87. private byte? GetLevel(Entity<HandheldLightComponent> ent)
  88. {
  89. // Curently every single flashlight has the same number of levels for status and that's all it uses the charge for
  90. // Thus we'll just check if the level changes.
  91. if (!_powerCell.TryGetBatteryFromSlot(ent, out var battery))
  92. return null;
  93. if (MathHelper.CloseToPercent(battery.CurrentCharge, 0) || ent.Comp.Wattage > battery.CurrentCharge)
  94. return 0;
  95. return (byte?) ContentHelpers.RoundToNearestLevels(battery.CurrentCharge / battery.MaxCharge * 255, 255, HandheldLightComponent.StatusLevels);
  96. }
  97. private void OnRemove(Entity<HandheldLightComponent> ent, ref ComponentRemove args)
  98. {
  99. _activeLights.Remove(ent);
  100. }
  101. private void OnActivate(Entity<HandheldLightComponent> ent, ref ActivateInWorldEvent args)
  102. {
  103. if (args.Handled || !args.Complex || !ent.Comp.ToggleOnInteract)
  104. return;
  105. if (ToggleStatus(args.User, ent))
  106. args.Handled = true;
  107. }
  108. /// <summary>
  109. /// Illuminates the light if it is not active, extinguishes it if it is active.
  110. /// </summary>
  111. /// <returns>True if the light's status was toggled, false otherwise.</returns>
  112. public bool ToggleStatus(EntityUid user, Entity<HandheldLightComponent> ent)
  113. {
  114. return ent.Comp.Activated ? TurnOff(ent) : TurnOn(user, ent);
  115. }
  116. private void OnExamine(EntityUid uid, HandheldLightComponent component, ExaminedEvent args)
  117. {
  118. args.PushMarkup(component.Activated
  119. ? Loc.GetString("handheld-light-component-on-examine-is-on-message")
  120. : Loc.GetString("handheld-light-component-on-examine-is-off-message"));
  121. }
  122. public override void Shutdown()
  123. {
  124. base.Shutdown();
  125. _activeLights.Clear();
  126. }
  127. public override void Update(float frameTime)
  128. {
  129. var toRemove = new RemQueue<Entity<HandheldLightComponent>>();
  130. foreach (var handheld in _activeLights)
  131. {
  132. if (handheld.Comp.Deleted)
  133. {
  134. toRemove.Add(handheld);
  135. continue;
  136. }
  137. if (Paused(handheld))
  138. continue;
  139. TryUpdate(handheld, frameTime);
  140. }
  141. foreach (var light in toRemove)
  142. {
  143. _activeLights.Remove(light);
  144. }
  145. }
  146. public override bool TurnOff(Entity<HandheldLightComponent> ent, bool makeNoise = true)
  147. {
  148. if (!ent.Comp.Activated || !_lights.TryGetLight(ent, out var pointLightComponent))
  149. {
  150. return false;
  151. }
  152. _lights.SetEnabled(ent, false, pointLightComponent);
  153. SetActivated(ent, false, ent, makeNoise);
  154. ent.Comp.Level = null;
  155. _activeLights.Remove(ent);
  156. return true;
  157. }
  158. public override bool TurnOn(EntityUid user, Entity<HandheldLightComponent> uid)
  159. {
  160. var component = uid.Comp;
  161. if (component.Activated || !_lights.TryGetLight(uid, out var pointLightComponent))
  162. {
  163. return false;
  164. }
  165. if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery) &&
  166. !TryComp(uid, out battery))
  167. {
  168. _audio.PlayPvs(_audio.ResolveSound(component.TurnOnFailSound), uid);
  169. _popup.PopupEntity(Loc.GetString("handheld-light-component-cell-missing-message"), uid, user);
  170. return false;
  171. }
  172. // To prevent having to worry about frame time in here.
  173. // Let's just say you need a whole second of charge before you can turn it on.
  174. // Simple enough.
  175. if (component.Wattage > battery.CurrentCharge)
  176. {
  177. _audio.PlayPvs(_audio.ResolveSound(component.TurnOnFailSound), uid);
  178. _popup.PopupEntity(Loc.GetString("handheld-light-component-cell-dead-message"), uid, user);
  179. return false;
  180. }
  181. _lights.SetEnabled(uid, true, pointLightComponent);
  182. SetActivated(uid, true, component, true);
  183. _activeLights.Add(uid);
  184. return true;
  185. }
  186. public void TryUpdate(Entity<HandheldLightComponent> uid, float frameTime)
  187. {
  188. var component = uid.Comp;
  189. if (!_powerCell.TryGetBatteryFromSlot(uid, out var batteryUid, out var battery, null) &&
  190. !TryComp(uid, out battery))
  191. {
  192. TurnOff(uid, false);
  193. return;
  194. }
  195. if (batteryUid == null)
  196. return;
  197. var appearanceComponent = EntityManager.GetComponentOrNull<AppearanceComponent>(uid);
  198. var fraction = battery.CurrentCharge / battery.MaxCharge;
  199. if (fraction >= 0.30)
  200. {
  201. _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.FullPower, appearanceComponent);
  202. }
  203. else if (fraction >= 0.10)
  204. {
  205. _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.LowPower, appearanceComponent);
  206. }
  207. else
  208. {
  209. _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.Dying, appearanceComponent);
  210. }
  211. if (component.Activated && !_battery.TryUseCharge(batteryUid.Value, component.Wattage * frameTime, battery))
  212. TurnOff(uid, false);
  213. UpdateLevel(uid);
  214. }
  215. private void UpdateLevel(Entity<HandheldLightComponent> ent)
  216. {
  217. var level = GetLevel(ent);
  218. if (level == ent.Comp.Level)
  219. return;
  220. ent.Comp.Level = level;
  221. Dirty(ent);
  222. }
  223. }
  224. }