1
0

SharedHandheldLightSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Content.Shared.Actions;
  2. using Content.Shared.Clothing.EntitySystems;
  3. using Content.Shared.Item;
  4. using Content.Shared.Light.Components;
  5. using Content.Shared.Toggleable;
  6. using Content.Shared.Verbs;
  7. using Robust.Shared.Audio;
  8. using Robust.Shared.Audio.Systems;
  9. using Robust.Shared.GameStates;
  10. using Robust.Shared.Utility;
  11. namespace Content.Shared.Light;
  12. public abstract class SharedHandheldLightSystem : EntitySystem
  13. {
  14. [Dependency] private readonly SharedItemSystem _itemSys = default!;
  15. [Dependency] private readonly ClothingSystem _clothingSys = default!;
  16. [Dependency] private readonly SharedActionsSystem _actionSystem = default!;
  17. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  18. [Dependency] private readonly SharedAudioSystem _audio = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<HandheldLightComponent, ComponentInit>(OnInit);
  23. SubscribeLocalEvent<HandheldLightComponent, ComponentHandleState>(OnHandleState);
  24. SubscribeLocalEvent<HandheldLightComponent, GetVerbsEvent<ActivationVerb>>(AddToggleLightVerb);
  25. }
  26. private void OnInit(EntityUid uid, HandheldLightComponent component, ComponentInit args)
  27. {
  28. UpdateVisuals(uid, component);
  29. // Want to make sure client has latest data on level so battery displays properly.
  30. Dirty(uid, component);
  31. }
  32. private void OnHandleState(EntityUid uid, HandheldLightComponent component, ref ComponentHandleState args)
  33. {
  34. if (args.Current is not HandheldLightComponent.HandheldLightComponentState state)
  35. return;
  36. component.Level = state.Charge;
  37. SetActivated(uid, state.Activated, component, false);
  38. }
  39. public void SetActivated(EntityUid uid, bool activated, HandheldLightComponent? component = null, bool makeNoise = true)
  40. {
  41. if (!Resolve(uid, ref component))
  42. return;
  43. if (component.Activated == activated)
  44. return;
  45. component.Activated = activated;
  46. if (makeNoise)
  47. {
  48. var sound = component.Activated ? component.TurnOnSound : component.TurnOffSound;
  49. _audio.PlayPvs(sound, uid);
  50. }
  51. Dirty(uid, component);
  52. UpdateVisuals(uid, component);
  53. }
  54. public void UpdateVisuals(EntityUid uid, HandheldLightComponent? component = null, AppearanceComponent? appearance = null)
  55. {
  56. if (!Resolve(uid, ref component, ref appearance, false))
  57. return;
  58. if (component.AddPrefix)
  59. {
  60. var prefix = component.Activated ? "on" : "off";
  61. _itemSys.SetHeldPrefix(uid, prefix);
  62. _clothingSys.SetEquippedPrefix(uid, prefix);
  63. }
  64. if (component.ToggleActionEntity != null)
  65. _actionSystem.SetToggled(component.ToggleActionEntity, component.Activated);
  66. _appearance.SetData(uid, ToggleableLightVisuals.Enabled, component.Activated, appearance);
  67. }
  68. private void AddToggleLightVerb(Entity<HandheldLightComponent> ent, ref GetVerbsEvent<ActivationVerb> args)
  69. {
  70. if (!args.CanAccess || !args.CanInteract || !ent.Comp.ToggleOnInteract)
  71. return;
  72. var @event = args;
  73. ActivationVerb verb = new()
  74. {
  75. Text = Loc.GetString("verb-common-toggle-light"),
  76. Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
  77. Act = ent.Comp.Activated
  78. ? () => TurnOff(ent)
  79. : () => TurnOn(@event.User, ent)
  80. };
  81. args.Verbs.Add(verb);
  82. }
  83. public abstract bool TurnOff(Entity<HandheldLightComponent> ent, bool makeNoise = true);
  84. public abstract bool TurnOn(EntityUid user, Entity<HandheldLightComponent> uid);
  85. }