OpenableSystem.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using Content.Shared.Chemistry.EntitySystems;
  2. using Content.Shared.Examine;
  3. using Content.Shared.Lock;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Interaction.Events;
  6. using Content.Shared.Nutrition.Components;
  7. using Content.Shared.Popups;
  8. using Content.Shared.Verbs;
  9. using Content.Shared.Weapons.Melee.Events;
  10. using Robust.Shared.Audio.Systems;
  11. using Robust.Shared.Utility;
  12. namespace Content.Shared.Nutrition.EntitySystems;
  13. /// <summary>
  14. /// Provides API for openable food and drinks, handles opening on use and preventing transfer when closed.
  15. /// </summary>
  16. public sealed partial class OpenableSystem : EntitySystem
  17. {
  18. [Dependency] private readonly LockSystem _lock = default!;
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. [Dependency] private readonly SharedAudioSystem _audio = default!;
  21. [Dependency] private readonly SharedPopupSystem _popup = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<OpenableComponent, ComponentInit>(OnInit);
  26. SubscribeLocalEvent<OpenableComponent, UseInHandEvent>(OnUse);
  27. // always try to unlock first before opening
  28. SubscribeLocalEvent<OpenableComponent, ActivateInWorldEvent>(OnActivated, after: new[] { typeof(LockSystem) });
  29. SubscribeLocalEvent<OpenableComponent, ExaminedEvent>(OnExamined);
  30. SubscribeLocalEvent<OpenableComponent, MeleeHitEvent>(HandleIfClosed);
  31. SubscribeLocalEvent<OpenableComponent, AfterInteractEvent>(HandleIfClosed);
  32. SubscribeLocalEvent<OpenableComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerbs);
  33. SubscribeLocalEvent<OpenableComponent, SolutionTransferAttemptEvent>(OnTransferAttempt);
  34. SubscribeLocalEvent<OpenableComponent, AttemptShakeEvent>(OnAttemptShake);
  35. SubscribeLocalEvent<OpenableComponent, AttemptAddFizzinessEvent>(OnAttemptAddFizziness);
  36. SubscribeLocalEvent<OpenableComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
  37. #if DEBUG
  38. SubscribeLocalEvent<OpenableComponent, MapInitEvent>(OnMapInit);
  39. }
  40. private void OnMapInit(Entity<OpenableComponent> ent, ref MapInitEvent args)
  41. {
  42. if (ent.Comp.Opened && _lock.IsLocked(ent.Owner))
  43. Log.Error($"Entity {ent} spawned locked open, this is a prototype mistake.");
  44. }
  45. #else
  46. }
  47. #endif
  48. private void OnInit(Entity<OpenableComponent> ent, ref ComponentInit args)
  49. {
  50. UpdateAppearance(ent, ent.Comp);
  51. }
  52. private void OnUse(Entity<OpenableComponent> ent, ref UseInHandEvent args)
  53. {
  54. if (args.Handled || !ent.Comp.OpenableByHand)
  55. return;
  56. args.Handled = TryOpen(ent, ent, args.User);
  57. }
  58. private void OnActivated(Entity<OpenableComponent> ent, ref ActivateInWorldEvent args)
  59. {
  60. if (args.Handled || !ent.Comp.OpenOnActivate)
  61. return;
  62. args.Handled = TryToggle(ent, args.User);
  63. }
  64. private void OnExamined(EntityUid uid, OpenableComponent comp, ExaminedEvent args)
  65. {
  66. if (!comp.Opened || !args.IsInDetailsRange)
  67. return;
  68. var text = Loc.GetString(comp.ExamineText);
  69. args.PushMarkup(text);
  70. }
  71. private void HandleIfClosed(EntityUid uid, OpenableComponent comp, HandledEntityEventArgs args)
  72. {
  73. // prevent spilling/pouring/whatever drinks when closed
  74. args.Handled = !comp.Opened;
  75. }
  76. private void OnGetVerbs(EntityUid uid, OpenableComponent comp, GetVerbsEvent<AlternativeVerb> args)
  77. {
  78. if (args.Hands == null || !args.CanAccess || !args.CanInteract || _lock.IsLocked(uid))
  79. return;
  80. AlternativeVerb verb;
  81. if (comp.Opened)
  82. {
  83. if (!comp.Closeable)
  84. return;
  85. verb = new()
  86. {
  87. Text = Loc.GetString(comp.CloseVerbText),
  88. Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/close.svg.192dpi.png")),
  89. Act = () => TryClose(args.Target, comp, args.User),
  90. // this verb is lower priority than drink verb (2) so it doesn't conflict
  91. };
  92. }
  93. else
  94. {
  95. verb = new()
  96. {
  97. Text = Loc.GetString(comp.OpenVerbText),
  98. Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/open.svg.192dpi.png")),
  99. Act = () => TryOpen(args.Target, comp, args.User)
  100. };
  101. }
  102. args.Verbs.Add(verb);
  103. }
  104. private void OnTransferAttempt(Entity<OpenableComponent> ent, ref SolutionTransferAttemptEvent args)
  105. {
  106. if (!ent.Comp.Opened)
  107. {
  108. // message says its just for drinks, shouldn't matter since you typically dont have a food that is openable and can be poured out
  109. args.Cancel(Loc.GetString("drink-component-try-use-drink-not-open", ("owner", ent.Owner)));
  110. }
  111. }
  112. private void OnAttemptShake(Entity<OpenableComponent> entity, ref AttemptShakeEvent args)
  113. {
  114. // Prevent shaking open containers
  115. if (entity.Comp.Opened)
  116. args.Cancelled = true;
  117. }
  118. private void OnAttemptAddFizziness(Entity<OpenableComponent> entity, ref AttemptAddFizzinessEvent args)
  119. {
  120. // Can't add fizziness to an open container
  121. if (entity.Comp.Opened)
  122. args.Cancelled = true;
  123. }
  124. private void OnLockToggleAttempt(Entity<OpenableComponent> ent, ref LockToggleAttemptEvent args)
  125. {
  126. // can't lock something while it's open
  127. if (ent.Comp.Opened)
  128. args.Cancelled = true;
  129. }
  130. /// <summary>
  131. /// Returns true if the entity either does not have OpenableComponent or it is opened.
  132. /// Drinks that don't have OpenableComponent are automatically open, so it returns true.
  133. /// </summary>
  134. public bool IsOpen(EntityUid uid, OpenableComponent? comp = null)
  135. {
  136. if (!Resolve(uid, ref comp, false))
  137. return true;
  138. return comp.Opened;
  139. }
  140. /// <summary>
  141. /// Returns true if the entity both has OpenableComponent and is not opened.
  142. /// Drinks that don't have OpenableComponent are automatically open, so it returns false.
  143. /// If user is not null a popup will be shown to them.
  144. /// </summary>
  145. public bool IsClosed(EntityUid uid, EntityUid? user = null, OpenableComponent? comp = null)
  146. {
  147. if (!Resolve(uid, ref comp, false))
  148. return false;
  149. if (comp.Opened)
  150. return false;
  151. if (user != null)
  152. _popup.PopupEntity(Loc.GetString(comp.ClosedPopup, ("owner", uid)), user.Value, user.Value);
  153. return true;
  154. }
  155. /// <summary>
  156. /// Update open visuals to the current value.
  157. /// </summary>
  158. public void UpdateAppearance(EntityUid uid, OpenableComponent? comp = null, AppearanceComponent? appearance = null)
  159. {
  160. if (!Resolve(uid, ref comp))
  161. return;
  162. _appearance.SetData(uid, OpenableVisuals.Opened, comp.Opened, appearance);
  163. }
  164. /// <summary>
  165. /// Sets the opened field and updates open visuals.
  166. /// </summary>
  167. public void SetOpen(EntityUid uid, bool opened = true, OpenableComponent? comp = null, EntityUid? user = null)
  168. {
  169. if (!Resolve(uid, ref comp, false) || opened == comp.Opened)
  170. return;
  171. comp.Opened = opened;
  172. Dirty(uid, comp);
  173. if (opened)
  174. {
  175. var ev = new OpenableOpenedEvent(user);
  176. RaiseLocalEvent(uid, ref ev);
  177. }
  178. else
  179. {
  180. var ev = new OpenableClosedEvent(user);
  181. RaiseLocalEvent(uid, ref ev);
  182. }
  183. UpdateAppearance(uid, comp);
  184. }
  185. /// <summary>
  186. /// If closed, opens it and plays the sound.
  187. /// </summary>
  188. /// <returns>Whether it got opened</returns>
  189. public bool TryOpen(EntityUid uid, OpenableComponent? comp = null, EntityUid? user = null)
  190. {
  191. if (!Resolve(uid, ref comp, false) || comp.Opened || _lock.IsLocked(uid))
  192. return false;
  193. var ev = new OpenableOpenAttemptEvent(user);
  194. RaiseLocalEvent(uid, ref ev);
  195. if (ev.Cancelled)
  196. return false;
  197. SetOpen(uid, true, comp, user);
  198. _audio.PlayPredicted(comp.Sound, uid, user);
  199. return true;
  200. }
  201. /// <summary>
  202. /// If opened, closes it and plays the close sound, if one is defined.
  203. /// </summary>
  204. /// <returns>Whether it got closed</returns>
  205. public bool TryClose(EntityUid uid, OpenableComponent? comp = null, EntityUid? user = null)
  206. {
  207. if (!Resolve(uid, ref comp, false) || !comp.Opened || !comp.Closeable)
  208. return false;
  209. SetOpen(uid, false, comp, user);
  210. if (comp.CloseSound != null)
  211. _audio.PlayPredicted(comp.CloseSound, uid, user);
  212. return true;
  213. }
  214. /// <summary>
  215. /// If opened, tries closing it if it's closeable.
  216. /// If closed, tries opening it.
  217. /// </summary>
  218. public bool TryToggle(Entity<OpenableComponent> ent, EntityUid? user)
  219. {
  220. if (ent.Comp.Opened && ent.Comp.Closeable)
  221. return TryClose(ent, ent.Comp, user);
  222. return TryOpen(ent, ent.Comp, user);
  223. }
  224. }
  225. /// <summary>
  226. /// Raised after an Openable is opened.
  227. /// </summary>
  228. [ByRefEvent]
  229. public record struct OpenableOpenedEvent(EntityUid? User = null);
  230. /// <summary>
  231. /// Raised after an Openable is closed.
  232. /// </summary>
  233. [ByRefEvent]
  234. public record struct OpenableClosedEvent(EntityUid? User = null);
  235. /// <summary>
  236. /// Raised before trying to open an Openable.
  237. /// </summary>
  238. [ByRefEvent]
  239. public record struct OpenableOpenAttemptEvent(EntityUid? User, bool Cancelled = false);