1
0

UnpoweredFlashlightSystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Content.Shared.Actions;
  2. using Content.Shared.Emag.Systems;
  3. using Content.Shared.Light.Components;
  4. using Content.Shared.Mind.Components;
  5. using Content.Shared.Storage.Components;
  6. using Content.Shared.Toggleable;
  7. using Content.Shared.Verbs;
  8. using Robust.Shared.Audio.Systems;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Random;
  11. using Robust.Shared.Utility;
  12. namespace Content.Shared.Light.EntitySystems;
  13. public sealed class UnpoweredFlashlightSystem : EntitySystem
  14. {
  15. // TODO: Split some of this to ItemTogglePointLight
  16. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  17. [Dependency] private readonly IRobustRandom _random = default!;
  18. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  19. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  20. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  21. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  22. [Dependency] private readonly SharedPointLightSystem _light = default!;
  23. [Dependency] private readonly EmagSystem _emag = default!;
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. SubscribeLocalEvent<UnpoweredFlashlightComponent, GetVerbsEvent<ActivationVerb>>(AddToggleLightVerbs);
  28. SubscribeLocalEvent<UnpoweredFlashlightComponent, GetItemActionsEvent>(OnGetActions);
  29. SubscribeLocalEvent<UnpoweredFlashlightComponent, ToggleActionEvent>(OnToggleAction);
  30. SubscribeLocalEvent<UnpoweredFlashlightComponent, MindAddedMessage>(OnMindAdded);
  31. SubscribeLocalEvent<UnpoweredFlashlightComponent, GotEmaggedEvent>(OnGotEmagged);
  32. SubscribeLocalEvent<UnpoweredFlashlightComponent, MapInitEvent>(OnMapInit);
  33. }
  34. private void OnMapInit(EntityUid uid, UnpoweredFlashlightComponent component, MapInitEvent args)
  35. {
  36. _actionContainer.EnsureAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
  37. Dirty(uid, component);
  38. }
  39. private void OnToggleAction(EntityUid uid, UnpoweredFlashlightComponent component, ToggleActionEvent args)
  40. {
  41. if (args.Handled)
  42. return;
  43. TryToggleLight((uid, component), args.Performer);
  44. args.Handled = true;
  45. }
  46. private void OnGetActions(EntityUid uid, UnpoweredFlashlightComponent component, GetItemActionsEvent args)
  47. {
  48. args.AddAction(component.ToggleActionEntity);
  49. }
  50. private void AddToggleLightVerbs(EntityUid uid, UnpoweredFlashlightComponent component, GetVerbsEvent<ActivationVerb> args)
  51. {
  52. if (!args.CanAccess || !args.CanInteract)
  53. return;
  54. ActivationVerb verb = new()
  55. {
  56. Text = Loc.GetString("toggle-flashlight-verb-get-data-text"),
  57. Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
  58. Act = () => TryToggleLight((uid, component), args.User),
  59. Priority = -1 // For things like PDA's, Open-UI and other verbs that should be higher priority.
  60. };
  61. args.Verbs.Add(verb);
  62. }
  63. private void OnMindAdded(EntityUid uid, UnpoweredFlashlightComponent component, MindAddedMessage args)
  64. {
  65. _actionsSystem.AddAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
  66. }
  67. private void OnGotEmagged(EntityUid uid, UnpoweredFlashlightComponent component, ref GotEmaggedEvent args)
  68. {
  69. if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
  70. return;
  71. if (!_light.TryGetLight(uid, out var light))
  72. return;
  73. if (_prototypeManager.TryIndex(component.EmaggedColorsPrototype, out var possibleColors))
  74. {
  75. var pick = _random.Pick(possibleColors.Colors.Values);
  76. _light.SetColor(uid, pick, light);
  77. }
  78. args.Repeatable = true;
  79. args.Handled = true;
  80. }
  81. public void TryToggleLight(Entity<UnpoweredFlashlightComponent?> ent, EntityUid? user = null, bool quiet = false)
  82. {
  83. if (!Resolve(ent, ref ent.Comp, false))
  84. return;
  85. SetLight(ent, !ent.Comp.LightOn, user, quiet);
  86. }
  87. public void SetLight(Entity<UnpoweredFlashlightComponent?> ent, bool value, EntityUid? user = null, bool quiet = false)
  88. {
  89. if (!Resolve(ent, ref ent.Comp))
  90. return;
  91. if (ent.Comp.LightOn == value)
  92. return;
  93. if (!_light.TryGetLight(ent, out var light))
  94. return;
  95. Dirty(ent);
  96. ent.Comp.LightOn = value;
  97. _light.SetEnabled(ent, value, light);
  98. _appearance.SetData(ent, UnpoweredFlashlightVisuals.LightOn, value);
  99. if (!quiet)
  100. _audioSystem.PlayPredicted(ent.Comp.ToggleSound, ent, user);
  101. _actionsSystem.SetToggled(ent.Comp.ToggleActionEntity, value);
  102. RaiseLocalEvent(ent, new LightToggleEvent(value));
  103. }
  104. }