| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using Content.Shared.Chemistry.EntitySystems;
- using Content.Shared.Examine;
- using Content.Shared.Lock;
- using Content.Shared.Interaction;
- using Content.Shared.Interaction.Events;
- using Content.Shared.Nutrition.Components;
- using Content.Shared.Popups;
- using Content.Shared.Verbs;
- using Content.Shared.Weapons.Melee.Events;
- using Robust.Shared.Audio.Systems;
- using Robust.Shared.Utility;
- namespace Content.Shared.Nutrition.EntitySystems;
- /// <summary>
- /// Provides API for openable food and drinks, handles opening on use and preventing transfer when closed.
- /// </summary>
- public sealed partial class OpenableSystem : EntitySystem
- {
- [Dependency] private readonly LockSystem _lock = default!;
- [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
- [Dependency] private readonly SharedAudioSystem _audio = default!;
- [Dependency] private readonly SharedPopupSystem _popup = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<OpenableComponent, ComponentInit>(OnInit);
- SubscribeLocalEvent<OpenableComponent, UseInHandEvent>(OnUse);
- // always try to unlock first before opening
- SubscribeLocalEvent<OpenableComponent, ActivateInWorldEvent>(OnActivated, after: new[] { typeof(LockSystem) });
- SubscribeLocalEvent<OpenableComponent, ExaminedEvent>(OnExamined);
- SubscribeLocalEvent<OpenableComponent, MeleeHitEvent>(HandleIfClosed);
- SubscribeLocalEvent<OpenableComponent, AfterInteractEvent>(HandleIfClosed);
- SubscribeLocalEvent<OpenableComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerbs);
- SubscribeLocalEvent<OpenableComponent, SolutionTransferAttemptEvent>(OnTransferAttempt);
- SubscribeLocalEvent<OpenableComponent, AttemptShakeEvent>(OnAttemptShake);
- SubscribeLocalEvent<OpenableComponent, AttemptAddFizzinessEvent>(OnAttemptAddFizziness);
- SubscribeLocalEvent<OpenableComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
- #if DEBUG
- SubscribeLocalEvent<OpenableComponent, MapInitEvent>(OnMapInit);
- }
- private void OnMapInit(Entity<OpenableComponent> ent, ref MapInitEvent args)
- {
- if (ent.Comp.Opened && _lock.IsLocked(ent.Owner))
- Log.Error($"Entity {ent} spawned locked open, this is a prototype mistake.");
- }
- #else
- }
- #endif
- private void OnInit(Entity<OpenableComponent> ent, ref ComponentInit args)
- {
- UpdateAppearance(ent, ent.Comp);
- }
- private void OnUse(Entity<OpenableComponent> ent, ref UseInHandEvent args)
- {
- if (args.Handled || !ent.Comp.OpenableByHand)
- return;
- args.Handled = TryOpen(ent, ent, args.User);
- }
- private void OnActivated(Entity<OpenableComponent> ent, ref ActivateInWorldEvent args)
- {
- if (args.Handled || !ent.Comp.OpenOnActivate)
- return;
- args.Handled = TryToggle(ent, args.User);
- }
- private void OnExamined(EntityUid uid, OpenableComponent comp, ExaminedEvent args)
- {
- if (!comp.Opened || !args.IsInDetailsRange)
- return;
- var text = Loc.GetString(comp.ExamineText);
- args.PushMarkup(text);
- }
- private void HandleIfClosed(EntityUid uid, OpenableComponent comp, HandledEntityEventArgs args)
- {
- // prevent spilling/pouring/whatever drinks when closed
- args.Handled = !comp.Opened;
- }
- private void OnGetVerbs(EntityUid uid, OpenableComponent comp, GetVerbsEvent<AlternativeVerb> args)
- {
- if (args.Hands == null || !args.CanAccess || !args.CanInteract || _lock.IsLocked(uid))
- return;
- AlternativeVerb verb;
- if (comp.Opened)
- {
- if (!comp.Closeable)
- return;
- verb = new()
- {
- Text = Loc.GetString(comp.CloseVerbText),
- Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/close.svg.192dpi.png")),
- Act = () => TryClose(args.Target, comp, args.User),
- // this verb is lower priority than drink verb (2) so it doesn't conflict
- };
- }
- else
- {
- verb = new()
- {
- Text = Loc.GetString(comp.OpenVerbText),
- Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/open.svg.192dpi.png")),
- Act = () => TryOpen(args.Target, comp, args.User)
- };
- }
- args.Verbs.Add(verb);
- }
- private void OnTransferAttempt(Entity<OpenableComponent> ent, ref SolutionTransferAttemptEvent args)
- {
- if (!ent.Comp.Opened)
- {
- // message says its just for drinks, shouldn't matter since you typically dont have a food that is openable and can be poured out
- args.Cancel(Loc.GetString("drink-component-try-use-drink-not-open", ("owner", ent.Owner)));
- }
- }
- private void OnAttemptShake(Entity<OpenableComponent> entity, ref AttemptShakeEvent args)
- {
- // Prevent shaking open containers
- if (entity.Comp.Opened)
- args.Cancelled = true;
- }
- private void OnAttemptAddFizziness(Entity<OpenableComponent> entity, ref AttemptAddFizzinessEvent args)
- {
- // Can't add fizziness to an open container
- if (entity.Comp.Opened)
- args.Cancelled = true;
- }
- private void OnLockToggleAttempt(Entity<OpenableComponent> ent, ref LockToggleAttemptEvent args)
- {
- // can't lock something while it's open
- if (ent.Comp.Opened)
- args.Cancelled = true;
- }
- /// <summary>
- /// Returns true if the entity either does not have OpenableComponent or it is opened.
- /// Drinks that don't have OpenableComponent are automatically open, so it returns true.
- /// </summary>
- public bool IsOpen(EntityUid uid, OpenableComponent? comp = null)
- {
- if (!Resolve(uid, ref comp, false))
- return true;
- return comp.Opened;
- }
- /// <summary>
- /// Returns true if the entity both has OpenableComponent and is not opened.
- /// Drinks that don't have OpenableComponent are automatically open, so it returns false.
- /// If user is not null a popup will be shown to them.
- /// </summary>
- public bool IsClosed(EntityUid uid, EntityUid? user = null, OpenableComponent? comp = null)
- {
- if (!Resolve(uid, ref comp, false))
- return false;
- if (comp.Opened)
- return false;
- if (user != null)
- _popup.PopupEntity(Loc.GetString(comp.ClosedPopup, ("owner", uid)), user.Value, user.Value);
- return true;
- }
- /// <summary>
- /// Update open visuals to the current value.
- /// </summary>
- public void UpdateAppearance(EntityUid uid, OpenableComponent? comp = null, AppearanceComponent? appearance = null)
- {
- if (!Resolve(uid, ref comp))
- return;
- _appearance.SetData(uid, OpenableVisuals.Opened, comp.Opened, appearance);
- }
- /// <summary>
- /// Sets the opened field and updates open visuals.
- /// </summary>
- public void SetOpen(EntityUid uid, bool opened = true, OpenableComponent? comp = null, EntityUid? user = null)
- {
- if (!Resolve(uid, ref comp, false) || opened == comp.Opened)
- return;
- comp.Opened = opened;
- Dirty(uid, comp);
- if (opened)
- {
- var ev = new OpenableOpenedEvent(user);
- RaiseLocalEvent(uid, ref ev);
- }
- else
- {
- var ev = new OpenableClosedEvent(user);
- RaiseLocalEvent(uid, ref ev);
- }
- UpdateAppearance(uid, comp);
- }
- /// <summary>
- /// If closed, opens it and plays the sound.
- /// </summary>
- /// <returns>Whether it got opened</returns>
- public bool TryOpen(EntityUid uid, OpenableComponent? comp = null, EntityUid? user = null)
- {
- if (!Resolve(uid, ref comp, false) || comp.Opened || _lock.IsLocked(uid))
- return false;
- var ev = new OpenableOpenAttemptEvent(user);
- RaiseLocalEvent(uid, ref ev);
- if (ev.Cancelled)
- return false;
- SetOpen(uid, true, comp, user);
- _audio.PlayPredicted(comp.Sound, uid, user);
- return true;
- }
- /// <summary>
- /// If opened, closes it and plays the close sound, if one is defined.
- /// </summary>
- /// <returns>Whether it got closed</returns>
- public bool TryClose(EntityUid uid, OpenableComponent? comp = null, EntityUid? user = null)
- {
- if (!Resolve(uid, ref comp, false) || !comp.Opened || !comp.Closeable)
- return false;
- SetOpen(uid, false, comp, user);
- if (comp.CloseSound != null)
- _audio.PlayPredicted(comp.CloseSound, uid, user);
- return true;
- }
- /// <summary>
- /// If opened, tries closing it if it's closeable.
- /// If closed, tries opening it.
- /// </summary>
- public bool TryToggle(Entity<OpenableComponent> ent, EntityUid? user)
- {
- if (ent.Comp.Opened && ent.Comp.Closeable)
- return TryClose(ent, ent.Comp, user);
- return TryOpen(ent, ent.Comp, user);
- }
- }
- /// <summary>
- /// Raised after an Openable is opened.
- /// </summary>
- [ByRefEvent]
- public record struct OpenableOpenedEvent(EntityUid? User = null);
- /// <summary>
- /// Raised after an Openable is closed.
- /// </summary>
- [ByRefEvent]
- public record struct OpenableClosedEvent(EntityUid? User = null);
- /// <summary>
- /// Raised before trying to open an Openable.
- /// </summary>
- [ByRefEvent]
- public record struct OpenableOpenAttemptEvent(EntityUid? User, bool Cancelled = false);
|