1
0

GuidebookSystem.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Linq;
  2. using Content.Client.Guidebook.Components;
  3. using Content.Client.Light;
  4. using Content.Client.Verbs;
  5. using Content.Shared.Guidebook;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.Light.Components;
  8. using Content.Shared.Speech;
  9. using Content.Shared.Tag;
  10. using Content.Shared.Verbs;
  11. using Robust.Client.GameObjects;
  12. using Robust.Client.Player;
  13. using Robust.Shared.Audio;
  14. using Robust.Shared.Audio.Systems;
  15. using Robust.Shared.Map;
  16. using Robust.Shared.Player;
  17. using Robust.Shared.Prototypes;
  18. using Robust.Shared.Timing;
  19. using Robust.Shared.Utility;
  20. namespace Content.Client.Guidebook;
  21. /// <summary>
  22. /// This system handles the help-verb and interactions with various client-side entities that are embedded into guidebooks.
  23. /// </summary>
  24. public sealed class GuidebookSystem : EntitySystem
  25. {
  26. [Dependency] private readonly IGameTiming _timing = default!;
  27. [Dependency] private readonly IPlayerManager _playerManager = default!;
  28. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  29. [Dependency] private readonly VerbSystem _verbSystem = default!;
  30. [Dependency] private readonly RgbLightControllerSystem _rgbLightControllerSystem = default!;
  31. [Dependency] private readonly SharedPointLightSystem _pointLightSystem = default!;
  32. [Dependency] private readonly TagSystem _tags = default!;
  33. public event Action<List<ProtoId<GuideEntryPrototype>>,
  34. List<ProtoId<GuideEntryPrototype>>?,
  35. ProtoId<GuideEntryPrototype>?,
  36. bool,
  37. ProtoId<GuideEntryPrototype>?>? OnGuidebookOpen;
  38. public const string GuideEmbedTag = "GuideEmbeded";
  39. private EntityUid _defaultUser;
  40. /// <inheritdoc/>
  41. public override void Initialize()
  42. {
  43. SubscribeLocalEvent<GuideHelpComponent, GetVerbsEvent<ExamineVerb>>(OnGetVerbs);
  44. SubscribeLocalEvent<GuideHelpComponent, ActivateInWorldEvent>(OnInteract);
  45. SubscribeLocalEvent<GuidebookControlsTestComponent, InteractHandEvent>(OnGuidebookControlsTestInteractHand);
  46. SubscribeLocalEvent<GuidebookControlsTestComponent, ActivateInWorldEvent>(OnGuidebookControlsTestActivateInWorld);
  47. SubscribeLocalEvent<GuidebookControlsTestComponent, GetVerbsEvent<AlternativeVerb>>(
  48. OnGuidebookControlsTestGetAlternateVerbs);
  49. }
  50. /// <summary>
  51. /// Gets a user entity to use for verbs and examinations. If the player has no attached entity, this will use a
  52. /// dummy client-side entity so that users can still use the guidebook when not attached to anything (e.g., in the
  53. /// lobby)
  54. /// </summary>
  55. public EntityUid GetGuidebookUser()
  56. {
  57. var user = _playerManager.LocalEntity;
  58. if (user != null)
  59. return user.Value;
  60. if (!Exists(_defaultUser))
  61. _defaultUser = Spawn(null, MapCoordinates.Nullspace);
  62. return _defaultUser;
  63. }
  64. private void OnGetVerbs(EntityUid uid, GuideHelpComponent component, GetVerbsEvent<ExamineVerb> args)
  65. {
  66. if (component.Guides.Count == 0 || _tags.HasTag(uid, GuideEmbedTag))
  67. return;
  68. args.Verbs.Add(new()
  69. {
  70. Text = Loc.GetString("guide-help-verb"),
  71. Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/information.svg.192dpi.png")),
  72. Act = () => OnGuidebookOpen?.Invoke(component.Guides, null, null, component.IncludeChildren, component.Guides[0]),
  73. ClientExclusive = true,
  74. CloseMenu = true
  75. });
  76. }
  77. public void OpenHelp(List<ProtoId<GuideEntryPrototype>> guides)
  78. {
  79. OnGuidebookOpen?.Invoke(guides, null, null, true, guides[0]);
  80. }
  81. private void OnInteract(EntityUid uid, GuideHelpComponent component, ActivateInWorldEvent args)
  82. {
  83. if (!_timing.IsFirstTimePredicted)
  84. return;
  85. if (!component.OpenOnActivation || component.Guides.Count == 0 || _tags.HasTag(uid, GuideEmbedTag))
  86. return;
  87. OnGuidebookOpen?.Invoke(component.Guides, null, null, component.IncludeChildren, component.Guides[0]);
  88. args.Handled = true;
  89. }
  90. private void OnGuidebookControlsTestGetAlternateVerbs(EntityUid uid, GuidebookControlsTestComponent component, GetVerbsEvent<AlternativeVerb> args)
  91. {
  92. args.Verbs.Add(new AlternativeVerb()
  93. {
  94. Act = () =>
  95. {
  96. if (Transform(uid).LocalRotation != Angle.Zero)
  97. Transform(uid).LocalRotation -= Angle.FromDegrees(90);
  98. },
  99. Text = Loc.GetString("guidebook-monkey-unspin"),
  100. Priority = -9999,
  101. });
  102. args.Verbs.Add(new AlternativeVerb()
  103. {
  104. Act = () =>
  105. {
  106. EnsureComp<PointLightComponent>(uid); // RGB demands this.
  107. _pointLightSystem.SetEnabled(uid, false);
  108. var rgb = EnsureComp<RgbLightControllerComponent>(uid);
  109. var sprite = EnsureComp<SpriteComponent>(uid);
  110. var layers = new List<int>();
  111. for (var i = 0; i < sprite.AllLayers.Count(); i++)
  112. {
  113. layers.Add(i);
  114. }
  115. _rgbLightControllerSystem.SetLayers(uid, layers, rgb);
  116. },
  117. Text = Loc.GetString("guidebook-monkey-disco"),
  118. Priority = -9998,
  119. });
  120. }
  121. private void OnGuidebookControlsTestActivateInWorld(EntityUid uid, GuidebookControlsTestComponent component, ActivateInWorldEvent args)
  122. {
  123. Transform(uid).LocalRotation += Angle.FromDegrees(90);
  124. }
  125. private void OnGuidebookControlsTestInteractHand(EntityUid uid, GuidebookControlsTestComponent component, InteractHandEvent args)
  126. {
  127. if (!TryComp<SpeechComponent>(uid, out var speech) || speech.SpeechSounds is null)
  128. return;
  129. // This code is broken because SpeechSounds isn't a file name or sound specifier directly.
  130. // Commenting out to avoid compile failure with https://github.com/space-wizards/RobustToolbox/pull/5540
  131. // _audioSystem.PlayGlobal(speech.SpeechSounds, Filter.Local(), false, speech.AudioParams);
  132. }
  133. public void FakeClientActivateInWorld(EntityUid activated)
  134. {
  135. var activateMsg = new ActivateInWorldEvent(GetGuidebookUser(), activated, true);
  136. RaiseLocalEvent(activated, activateMsg);
  137. }
  138. public void FakeClientAltActivateInWorld(EntityUid activated)
  139. {
  140. // Get list of alt-interact verbs
  141. var verbs = _verbSystem.GetLocalVerbs(activated, GetGuidebookUser(), typeof(AlternativeVerb), force: true);
  142. if (!verbs.Any())
  143. return;
  144. _verbSystem.ExecuteVerb(verbs.First(), GetGuidebookUser(), activated);
  145. }
  146. public void FakeClientUse(EntityUid activated)
  147. {
  148. var activateMsg = new InteractHandEvent(GetGuidebookUser(), activated);
  149. RaiseLocalEvent(activated, activateMsg);
  150. }
  151. }