Cuffable/Handcuff ECS (#14382)
This commit is contained in:
@@ -1,216 +1,44 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Cuffs.Components;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Cuffs;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Weapons.Melee.Events;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Player;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Content.Server.Hands.Systems;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.Cuffs.Components;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Server.Cuffs
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class CuffableSystem : SharedCuffableSystem
|
||||
{
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
[Dependency] private readonly HandVirtualItemSystem _virtualSystem = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<HandCountChangedEvent>(OnHandCountChanged);
|
||||
SubscribeLocalEvent<UncuffAttemptEvent>(OnUncuffAttempt);
|
||||
SubscribeLocalEvent<CuffableComponent, GetVerbsEvent<Verb>>(AddUncuffVerb);
|
||||
SubscribeLocalEvent<HandcuffComponent, AfterInteractEvent>(OnCuffAfterInteract);
|
||||
SubscribeLocalEvent<HandcuffComponent, MeleeHitEvent>(OnCuffMeleeHit);
|
||||
SubscribeLocalEvent<CuffableComponent, EntRemovedFromContainerMessage>(OnCuffsRemoved);
|
||||
SubscribeLocalEvent<HandcuffComponent, ComponentGetState>(OnHandcuffGetState);
|
||||
SubscribeLocalEvent<CuffableComponent, ComponentGetState>(OnCuffableGetState);
|
||||
}
|
||||
|
||||
private void OnCuffsRemoved(EntityUid uid, CuffableComponent component, EntRemovedFromContainerMessage args)
|
||||
private void OnHandcuffGetState(EntityUid uid, HandcuffComponent component, ref ComponentGetState args)
|
||||
{
|
||||
if (args.Container.ID == component.Container.ID)
|
||||
_virtualSystem.DeleteInHandsMatching(uid, args.Entity);
|
||||
args.State = new HandcuffComponentState(component.OverlayIconState, component.Cuffing);
|
||||
}
|
||||
|
||||
private void AddUncuffVerb(EntityUid uid, CuffableComponent component, GetVerbsEvent<Verb> args)
|
||||
private void OnCuffableGetState(EntityUid uid, CuffableComponent component, ref ComponentGetState args)
|
||||
{
|
||||
// Can the user access the cuffs, and is there even anything to uncuff?
|
||||
if (!args.CanAccess || component.CuffedHandCount == 0 || args.Hands == null)
|
||||
return;
|
||||
|
||||
// We only check can interact if the user is not uncuffing themselves. As a result, the verb will show up
|
||||
// when the user is incapacitated & trying to uncuff themselves, but TryUncuff() will still fail when
|
||||
// attempted.
|
||||
if (args.User != args.Target && !args.CanInteract)
|
||||
return;
|
||||
|
||||
Verb verb = new()
|
||||
{
|
||||
Act = () => component.TryUncuff(args.User),
|
||||
DoContactInteraction = true,
|
||||
Text = Loc.GetString("uncuff-verb-get-data-text")
|
||||
};
|
||||
//TODO VERB ICON add uncuffing symbol? may re-use the alert symbol showing that you are currently cuffed?
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
private void OnCuffAfterInteract(EntityUid uid, HandcuffComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Target is not {Valid: true} target)
|
||||
return;
|
||||
|
||||
if (!args.CanReach)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-too-far-away-error"), args.User, args.User);
|
||||
return;
|
||||
}
|
||||
|
||||
TryCuffing(uid, args.User, args.Target.Value, component);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void TryCuffing(EntityUid handcuff, EntityUid user, EntityUid target, HandcuffComponent component)
|
||||
{
|
||||
if (component.Cuffing || !EntityManager.TryGetComponent<CuffableComponent>(target, out var cuffed))
|
||||
return;
|
||||
|
||||
if (!EntityManager.TryGetComponent<HandsComponent?>(target, out var hands))
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-target-has-no-hands-error",("targetName", target)), user, user);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cuffed.CuffedHandCount >= hands.Count)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-target-has-no-free-hands-error",("targetName", target)), user, user);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO these messages really need third-party variants. I.e., "{$user} starts cuffing {$target}!"
|
||||
if (target == user)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-target-self"), user, user);
|
||||
}
|
||||
else
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-start-cuffing-target-message",("targetName", target)), user, user);
|
||||
_popup.PopupEntity(Loc.GetString("handcuff-component-start-cuffing-by-other-message",("otherName", user)), target, target);
|
||||
}
|
||||
|
||||
_audio.PlayPvs(component.StartCuffSound, handcuff);
|
||||
|
||||
component.TryUpdateCuff(user, target, cuffed);
|
||||
}
|
||||
|
||||
private void OnCuffMeleeHit(EntityUid uid, HandcuffComponent component, MeleeHitEvent args)
|
||||
{
|
||||
if (!args.HitEntities.Any())
|
||||
return;
|
||||
|
||||
TryCuffing(uid, args.User, args.HitEntities.First(), component);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
|
||||
private void OnUncuffAttempt(UncuffAttemptEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!EntityManager.EntityExists(args.User))
|
||||
{
|
||||
// Should this even be possible?
|
||||
args.Cancel();
|
||||
return;
|
||||
}
|
||||
// If the user is the target, special logic applies.
|
||||
// This is because the CanInteract blocking of the cuffs prevents self-uncuff.
|
||||
if (args.User == args.Target)
|
||||
{
|
||||
// This UncuffAttemptEvent check should probably be In MobStateSystem, not here?
|
||||
if (_mobState.IsIncapacitated(args.User))
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO Find a way for cuffable to check ActionBlockerSystem.CanInteract() without blocking itself
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if the user can interact.
|
||||
if (!_actionBlockerSystem.CanInteract(args.User, args.Target))
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
if (args.Cancelled)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("cuffable-component-cannot-interact-message"), args.Target, args.User);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check the current amount of hands the owner has, and if there's less hands than active cuffs we remove some cuffs.
|
||||
/// </summary>
|
||||
private void OnHandCountChanged(HandCountChangedEvent message)
|
||||
{
|
||||
var owner = message.Sender;
|
||||
|
||||
if (!EntityManager.TryGetComponent(owner, out CuffableComponent? cuffable) ||
|
||||
!cuffable.Initialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dirty = false;
|
||||
var handCount = EntityManager.GetComponentOrNull<HandsComponent>(owner)?.Count ?? 0;
|
||||
|
||||
while (cuffable.CuffedHandCount > handCount && cuffable.CuffedHandCount > 0)
|
||||
{
|
||||
dirty = true;
|
||||
|
||||
var container = cuffable.Container;
|
||||
var entity = container.ContainedEntities[^1];
|
||||
|
||||
container.Remove(entity);
|
||||
EntityManager.GetComponent<TransformComponent>(entity).WorldPosition = EntityManager.GetComponent<TransformComponent>(owner).WorldPosition;
|
||||
}
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
UpdateCuffState(owner, cuffable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event fired on the User when the User attempts to cuff the Target.
|
||||
/// Should generate popups on the User.
|
||||
/// </summary>
|
||||
public sealed class UncuffAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public readonly EntityUid User;
|
||||
public readonly EntityUid Target;
|
||||
|
||||
public UncuffAttemptEvent(EntityUid user, EntityUid target)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
// there are 2 approaches i can think of to handle the handcuff overlay on players
|
||||
// 1 - make the current RSI the handcuff type that's currently active. all handcuffs on the player will appear the same.
|
||||
// 2 - allow for several different player overlays for each different cuff type.
|
||||
// approach #2 would be more difficult/time consuming to do and the payoff doesn't make it worth it.
|
||||
// right now we're doing approach #1.
|
||||
HandcuffComponent? cuffs = null;
|
||||
if (component.CuffedHandCount > 0)
|
||||
TryComp(component.LastAddedCuffs, out cuffs);
|
||||
args.State = new CuffableComponentState(component.CuffedHandCount,
|
||||
component.CanStillInteract,
|
||||
component.Uncuffing,
|
||||
cuffs?.CuffedRSI,
|
||||
$"{cuffs?.OverlayIconState}-{component.CuffedHandCount}",
|
||||
cuffs?.Color);
|
||||
// the iconstate is formatted as blah-2, blah-4, blah-6, etc.
|
||||
// the number corresponds to how many hands are cuffed.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user