mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-10 10:51:14 +03:00
* phase 1 * delayed knockdown * brudda * microbalance * enrrhguy gjkasnnboew * does nothing * whoop * fix YAML linter * some more changes * no discounts * I hate ContainerContainer * gorbino's quest * asfassaffsa * dgdgsdgsgds * humburber * better description * make it a metabolite... * nevermind * API text * grok --------- Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
409 lines
15 KiB
C#
409 lines
15 KiB
C#
using Content.Shared.ActionBlocker;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.IdentityManagement;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Interaction.Events;
|
|
using Content.Shared.Item.ItemToggle.Components;
|
|
using Content.Shared.Popups;
|
|
using Content.Shared.Power;
|
|
using Content.Shared.Power.EntitySystems;
|
|
using Content.Shared.Temperature;
|
|
using Content.Shared.Toggleable;
|
|
using Content.Shared.Trigger.Components.Effects;
|
|
using Content.Shared.Verbs;
|
|
using Content.Shared.Wieldable;
|
|
using Robust.Shared.Audio.Systems;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Shared.Item.ItemToggle;
|
|
/// <summary>
|
|
/// Handles generic item toggles, like a welder turning on and off, or an e-sword.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// If you need extended functionality (e.g. requiring power) then add a new component and use events.
|
|
/// </remarks>
|
|
public sealed partial class ItemToggleSystem : EntitySystem
|
|
{
|
|
[Dependency] private INetManager _netManager = default!;
|
|
[Dependency] private SharedAppearanceSystem _appearance = default!;
|
|
[Dependency] private SharedAudioSystem _audio = default!;
|
|
[Dependency] private SharedBatterySystem _battery = default!;
|
|
[Dependency] private SharedPopupSystem _popup = default!;
|
|
[Dependency] private ActionBlockerSystem _actionBlocker = default!;
|
|
[Dependency] private IGameTiming _gameTiming = default!;
|
|
|
|
[Dependency] private EntityQuery<ItemToggleComponent> _itemToggleQuery = default!;
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnStartup(Entity<ItemToggleComponent> ent, ref ComponentStartup args)
|
|
{
|
|
UpdateVisuals(ent);
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnMapInit(Entity<ItemToggleComponent> ent, ref MapInitEvent args)
|
|
{
|
|
if (!ent.Comp.Activated)
|
|
return;
|
|
|
|
var ev = new ItemToggledEvent(Predicted: ent.Comp.Predictable, Activated: ent.Comp.Activated, User: null);
|
|
RaiseLocalEvent(ent, ref ev);
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnUseInHand(Entity<ItemToggleComponent> ent, ref UseInHandEvent args)
|
|
{
|
|
if (args.Handled || !ent.Comp.OnUse)
|
|
return;
|
|
|
|
args.Handled = true;
|
|
|
|
Toggle((ent, ent.Comp), args.User, predicted: ent.Comp.Predictable);
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnActivateVerb(Entity<ItemToggleComponent> ent, ref GetVerbsEvent<ActivationVerb> args)
|
|
{
|
|
if (!args.CanAccess || !args.CanInteract || !ent.Comp.OnActivate)
|
|
return;
|
|
|
|
if (ent.Comp.RequireComplexInteract && !args.CanComplexInteract)
|
|
return;
|
|
|
|
var user = args.User;
|
|
|
|
if (ent.Comp.Activated)
|
|
{
|
|
var ev = new ItemToggleDeactivateAttemptEvent(args.User);
|
|
RaiseLocalEvent(ent.Owner, ref ev);
|
|
|
|
if (ev.Cancelled)
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
var ev = new ItemToggleActivateAttemptEvent(args.User);
|
|
RaiseLocalEvent(ent.Owner, ref ev);
|
|
|
|
if (ev.Cancelled)
|
|
return;
|
|
}
|
|
|
|
args.Verbs.Add(new ActivationVerb()
|
|
{
|
|
Text = !ent.Comp.Activated ? Loc.GetString(ent.Comp.VerbToggleOn) : Loc.GetString(ent.Comp.VerbToggleOff),
|
|
Act = () =>
|
|
{
|
|
Toggle((ent.Owner, ent.Comp), user, predicted: ent.Comp.Predictable);
|
|
}
|
|
});
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnActivate(Entity<ItemToggleComponent> ent, ref ActivateInWorldEvent args)
|
|
{
|
|
if (args.Handled || !ent.Comp.OnActivate)
|
|
return;
|
|
|
|
args.Handled = true;
|
|
Toggle((ent.Owner, ent.Comp), args.User, predicted: ent.Comp.Predictable);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used when an item is attempted to be toggled.
|
|
/// Sets its state to the opposite of what it is.
|
|
/// </summary>
|
|
/// <returns>Same as <see cref="TrySetActive"/></returns>
|
|
public bool Toggle(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true, bool showPopup = true)
|
|
{
|
|
if (!_itemToggleQuery.Resolve(ent, ref ent.Comp, false))
|
|
return false;
|
|
|
|
return TrySetActive(ent, !ent.Comp.Activated, user, predicted, showPopup);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to set the activated bool from a value.
|
|
/// </summary>
|
|
/// <returns>false if the attempt fails for any reason</returns>
|
|
public bool TrySetActive(Entity<ItemToggleComponent?> ent, bool active, EntityUid? user = null, bool predicted = true, bool showPopup = true)
|
|
{
|
|
if (active)
|
|
return TryActivate(ent, user, predicted: predicted, showPopup);
|
|
else
|
|
return TryDeactivate(ent, user, predicted: predicted, showPopup);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used when an item is attempting to be activated. It returns false if the attempt fails any reason, interrupting the activation.
|
|
/// </summary>
|
|
/// <param name="ent">The item to activate, with an optional resolved <see cref="ItemToggleComponent"/>.</param>
|
|
/// <param name="user">The entity attempting the activation, if any.</param>
|
|
/// <param name="predicted">Whether to predict feedback (sounds/popups) on the client.</param>
|
|
/// <param name="showPopup">Whether to show a popup with the action outcome.</param>
|
|
/// <param name="consciousAction">Whether this is a deliberate action, or a trigger activation. See <see cref="ItemToggleOnTriggerComponent.ConsciousAction"/>.</param>
|
|
public bool TryActivate(Entity<ItemToggleComponent?> ent, EntityUid? user, bool predicted, bool showPopup, bool consciousAction = true)
|
|
{
|
|
if (!_itemToggleQuery.Resolve(ent, ref ent.Comp, false))
|
|
return false;
|
|
|
|
var uid = ent.Owner;
|
|
var comp = ent.Comp;
|
|
if (comp.Activated)
|
|
return true;
|
|
|
|
// Check the complex interact requirement, or bypass it with consciousAction.
|
|
// Handles things like mice triggering mousetraps while not being able to set them with verbs.
|
|
if (user != null && ent.Comp.RequireComplexInteract && consciousAction && !_actionBlocker.CanComplexInteract(user.Value))
|
|
return false;
|
|
|
|
var attempt = new ItemToggleActivateAttemptEvent(user);
|
|
RaiseLocalEvent(uid, ref attempt);
|
|
|
|
if (!comp.Predictable)
|
|
predicted = false;
|
|
|
|
if (!predicted && _netManager.IsClient)
|
|
return false;
|
|
|
|
if (attempt.Cancelled)
|
|
{
|
|
if (attempt.Silent)
|
|
return false;
|
|
|
|
if (predicted)
|
|
_audio.PlayPredicted(comp.SoundFailToActivate, uid, user);
|
|
else
|
|
_audio.PlayPvs(comp.SoundFailToActivate, uid);
|
|
|
|
if (showPopup && attempt.Popup != null && user != null)
|
|
{
|
|
_popup.PopupEntity(attempt.Popup, uid, user.Value);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Activate((uid, comp), predicted, user, showPopup);
|
|
return true;
|
|
}
|
|
|
|
/// <inheritdoc cref="ItemToggleSystem.TryActivate"/>
|
|
public bool TryActivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true, bool showPopup = true)
|
|
{
|
|
return TryActivate(ent, user, predicted, showPopup, consciousAction: true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used when an item is attempting to be deactivated. It returns false if the attempt fails any reason, interrupting the deactivation.
|
|
/// </summary>
|
|
/// <param name="ent">The item to activate, with an optional resolved <see cref="ItemToggleComponent"/>.</param>
|
|
/// <param name="user">The entity attempting the activation, if any.</param>
|
|
/// <param name="predicted">Whether to predict feedback (sounds/popups) on the client.</param>
|
|
/// <param name="showPopup">Whether to show a popup with the action outcome.</param>
|
|
/// <param name="consciousAction">Whether this is a deliberate action, or a trigger activation. See <see cref="ItemToggleOnTriggerComponent.ConsciousAction"/>.</param>
|
|
public bool TryDeactivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true, bool showPopup = true, bool consciousAction = true)
|
|
{
|
|
if (!_itemToggleQuery.Resolve(ent, ref ent.Comp, false))
|
|
return false;
|
|
|
|
var uid = ent.Owner;
|
|
var comp = ent.Comp;
|
|
if (!comp.Activated)
|
|
return true;
|
|
|
|
if (!comp.Predictable)
|
|
predicted = false;
|
|
|
|
// Check the complex interact requirement, or bypass it with consciousAction.
|
|
// Handles things like mice triggering mousetraps while not being able to set them with verbs.
|
|
if (user != null && ent.Comp.RequireComplexInteract && consciousAction && !_actionBlocker.CanComplexInteract(user.Value))
|
|
return false;
|
|
|
|
var attempt = new ItemToggleDeactivateAttemptEvent(user);
|
|
RaiseLocalEvent(uid, ref attempt);
|
|
|
|
if (!predicted && _netManager.IsClient)
|
|
return false;
|
|
|
|
if (attempt.Cancelled)
|
|
{
|
|
if (attempt.Silent)
|
|
return false;
|
|
|
|
if (showPopup && attempt.Popup != null && user != null)
|
|
{
|
|
_popup.PopupEntity(attempt.Popup, uid, user.Value);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Deactivate((uid, comp), predicted, user, showPopup);
|
|
return true;
|
|
}
|
|
|
|
private void Activate(Entity<ItemToggleComponent> ent, bool predicted, EntityUid? user = null, bool showPopup = true)
|
|
{
|
|
var (uid, comp) = ent;
|
|
var soundToPlay = comp.SoundActivate;
|
|
|
|
if (predicted)
|
|
_audio.PlayPredicted(soundToPlay, uid, user);
|
|
else
|
|
_audio.PlayPvs(soundToPlay, uid);
|
|
|
|
if (showPopup && ent.Comp.PopupActivate != null && user != null)
|
|
_popup.PopupEntity(Loc.GetString(ent.Comp.PopupActivate), user.Value, user.Value);
|
|
|
|
comp.Activated = true;
|
|
UpdateVisuals((uid, comp));
|
|
Dirty(uid, comp);
|
|
|
|
var toggleUsed = new ItemToggledEvent(predicted, Activated: true, user);
|
|
RaiseLocalEvent(uid, ref toggleUsed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to make the actual changes to the item's components on deactivation.
|
|
/// </summary>
|
|
private void Deactivate(Entity<ItemToggleComponent> ent, bool predicted, EntityUid? user = null, bool showPopup = true)
|
|
{
|
|
var (uid, comp) = ent;
|
|
var soundToPlay = comp.SoundDeactivate;
|
|
if (predicted)
|
|
{
|
|
_audio.PlayPredicted(soundToPlay, uid, user);
|
|
if (showPopup && ent.Comp.PopupDeactivate != null && user != null)
|
|
_popup.PopupEntity(Loc.GetString(ent.Comp.PopupDeactivate), user.Value, user.Value);
|
|
}
|
|
else
|
|
{
|
|
_audio.PlayPvs(soundToPlay, uid);
|
|
if (showPopup && ent.Comp.PopupDeactivate != null && user != null)
|
|
_popup.PopupEntity(Loc.GetString(ent.Comp.PopupDeactivate), user.Value, user.Value);
|
|
}
|
|
|
|
comp.Activated = false;
|
|
UpdateVisuals((uid, comp));
|
|
Dirty(uid, comp);
|
|
|
|
var toggleUsed = new ItemToggledEvent(predicted, Activated: false, user);
|
|
RaiseLocalEvent(uid, ref toggleUsed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets if this toggleable item can be activated in world by pressing "e"
|
|
/// </summary>
|
|
public void SetOnActivate(Entity<ItemToggleComponent?> ent, bool val)
|
|
{
|
|
if (!Resolve(ent, ref ent.Comp))
|
|
return;
|
|
|
|
if (ent.Comp.OnActivate == val)
|
|
return;
|
|
|
|
ent.Comp.OnActivate = val;
|
|
Dirty(ent);
|
|
}
|
|
|
|
private void UpdateVisuals(Entity<ItemToggleComponent> ent)
|
|
{
|
|
if (TryComp(ent, out AppearanceComponent? appearance))
|
|
{
|
|
_appearance.SetData(ent, ToggleableVisuals.Enabled, ent.Comp.Activated, appearance);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used for items that require to be wielded in both hands to activate. For instance the dual energy sword will turn off if not wielded.
|
|
/// </summary>
|
|
[SubscribeLocalEvent]
|
|
private void TurnOffOnUnwielded(Entity<ItemToggleComponent> ent, ref ItemUnwieldedEvent args)
|
|
{
|
|
TryDeactivate((ent, ent.Comp), args.User);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wieldable items will automatically turn on when wielded.
|
|
/// </summary>
|
|
[SubscribeLocalEvent]
|
|
private void TurnOnOnWielded(Entity<ItemToggleComponent> ent, ref ItemWieldedEvent args)
|
|
{
|
|
TryActivate((ent, ent.Comp), args.User);
|
|
}
|
|
|
|
public bool IsActivated(Entity<ItemToggleComponent?> ent)
|
|
{
|
|
if (!_itemToggleQuery.Resolve(ent, ref ent.Comp, false))
|
|
return true; // assume always activated if no component
|
|
|
|
return ent.Comp.Activated;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to make the item hot when activated.
|
|
/// </summary>
|
|
[SubscribeLocalEvent]
|
|
private void OnIsHotEvent(Entity<ItemToggleHotComponent> ent, ref IsHotEvent args)
|
|
{
|
|
args.IsHot |= IsActivated(ent.Owner);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to update the looping active sound linked to the entity.
|
|
/// </summary>
|
|
[SubscribeLocalEvent]
|
|
private void UpdateActiveSound(Entity<ItemToggleActiveSoundComponent> ent, ref ItemToggledEvent args)
|
|
{
|
|
if (!_gameTiming.IsFirstTimePredicted)
|
|
return;
|
|
|
|
var (uid, comp) = ent;
|
|
if (!args.Activated)
|
|
{
|
|
comp.PlayingStream = _audio.Stop(comp.PlayingStream);
|
|
return;
|
|
}
|
|
|
|
if (comp.ActiveSound != null && comp.PlayingStream == null)
|
|
{
|
|
var loop = comp.ActiveSound.Params.WithLoop(true);
|
|
var stream = args.Predicted
|
|
? _audio.PlayPredicted(comp.ActiveSound, uid, args.User, loop)
|
|
: _audio.PlayPvs(comp.ActiveSound, uid, loop);
|
|
if (stream?.Entity is { } entity)
|
|
comp.PlayingStream = entity;
|
|
}
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnToggleCheckCharge(Entity<ItemToggleRequiresChargeComponent> ent, ref ItemToggleActivateAttemptEvent args)
|
|
{
|
|
if (args.Cancelled)
|
|
return;
|
|
|
|
if (_battery.GetCharge(ent.Owner).Charge >= ent.Comp.RequiredCharge)
|
|
return;
|
|
|
|
args.Popup = Loc.GetString(ent.Comp.FailPopup);
|
|
args.Cancelled = true;
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnChargeChanged(Entity<ItemToggleRequiresChargeComponent> ent, ref ChargeChangedEvent args)
|
|
{
|
|
if (_battery.GetCharge(ent.Owner).Charge >= ent.Comp.RequiredCharge)
|
|
return;
|
|
|
|
TryDeactivate(ent.Owner);
|
|
}
|
|
|
|
[SubscribeLocalEvent]
|
|
private void OnExamined(Entity<ItemToggleExaminableStatusComponent> ent, ref ExaminedEvent args)
|
|
{
|
|
var status = Loc.GetString(IsActivated(ent.Owner) ? ent.Comp.OnText : ent.Comp.OffText, ("target", Identity.Entity(ent, EntityManager)));
|
|
args.PushMarkup(status);
|
|
}
|
|
}
|