Cuffable/Handcuff ECS (#14382)
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Cuffs.Components;
|
||||
using Content.Shared.Humanoid;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.Cuffs.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedCuffableComponent))]
|
||||
public sealed class CuffableComponent : SharedCuffableComponent
|
||||
{
|
||||
[ViewVariables]
|
||||
private string? _currentRSI;
|
||||
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
if (curState is not CuffableComponentState cuffState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CanStillInteract = cuffState.CanStillInteract;
|
||||
_sysMan.GetEntitySystem<ActionBlockerSystem>().UpdateCanMove(Owner);
|
||||
|
||||
if (_entityManager.TryGetComponent<SpriteComponent>(Owner, out var spriteComponent))
|
||||
{
|
||||
spriteComponent.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0);
|
||||
spriteComponent.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color);
|
||||
|
||||
if (cuffState.NumHandsCuffed > 0)
|
||||
{
|
||||
if (_currentRSI != cuffState.RSI) // we don't want to keep loading the same RSI
|
||||
{
|
||||
_currentRSI = cuffState.RSI;
|
||||
|
||||
if (_currentRSI != null)
|
||||
{
|
||||
spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState), new ResourcePath(_currentRSI));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState)); // TODO: safety check to see if RSI contains the state?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ev = new CuffedStateChangeEvent();
|
||||
_entityManager.EventBus.RaiseLocalEvent(Owner, ref ev);
|
||||
}
|
||||
|
||||
protected override void OnRemove()
|
||||
{
|
||||
base.OnRemove();
|
||||
if (_entityManager.TryGetComponent<SpriteComponent>(Owner, out var spriteComponent))
|
||||
spriteComponent.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using Content.Shared.Cuffs.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Client.Cuffs.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedHandcuffComponent))]
|
||||
public sealed class HandcuffComponent : SharedHandcuffComponent
|
||||
{
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
if (curState is not HandcuffedComponentState state)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.IconState == string.Empty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IoCManager.Resolve<IEntityManager>().TryGetComponent<SpriteComponent?>(Owner, out var sprite))
|
||||
{
|
||||
sprite.LayerSetState(0, new RSI.StateId(state.IconState)); // TODO: safety check to see if RSI contains the state?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,79 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Cuffs;
|
||||
using Content.Shared.Cuffs.Components;
|
||||
using Content.Shared.Humanoid;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Client.Cuffs
|
||||
namespace Content.Client.Cuffs;
|
||||
|
||||
public sealed class CuffableSystem : SharedCuffableSystem
|
||||
{
|
||||
public sealed class CuffableSystem : SharedCuffableSystem
|
||||
{
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CuffableComponent, ComponentShutdown>(OnShutdown);
|
||||
SubscribeLocalEvent<CuffableComponent, ComponentHandleState>(OnCuffableHandleState);
|
||||
SubscribeLocalEvent<HandcuffComponent, ComponentHandleState>(OnHandcuffHandleState);
|
||||
}
|
||||
|
||||
private void OnShutdown(EntityUid uid, CuffableComponent component, ComponentShutdown args)
|
||||
{
|
||||
if (TryComp<SpriteComponent>(uid, out var sprite))
|
||||
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false);
|
||||
}
|
||||
|
||||
private void OnHandcuffHandleState(EntityUid uid, HandcuffComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not HandcuffComponentState state)
|
||||
return;
|
||||
|
||||
component.Cuffing = state.Cuffing;
|
||||
|
||||
if (state.IconState == string.Empty)
|
||||
return;
|
||||
|
||||
if (TryComp<SpriteComponent>(uid, out var sprite))
|
||||
{
|
||||
sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, state.IconState);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCuffableHandleState(EntityUid uid, CuffableComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not CuffableComponentState cuffState)
|
||||
return;
|
||||
|
||||
component.CanStillInteract = cuffState.CanStillInteract;
|
||||
component.Uncuffing = cuffState.Uncuffing;
|
||||
_actionBlocker.UpdateCanMove(uid);
|
||||
|
||||
var ev = new CuffedStateChangeEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
|
||||
if (!TryComp<SpriteComponent>(uid, out var sprite))
|
||||
return;
|
||||
var cuffed = cuffState.NumHandsCuffed > 0;
|
||||
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffed);
|
||||
|
||||
// if they are not cuffed, that means that we didn't get a valid color,
|
||||
// iconstate, or RSI. that also means we don't need to update the sprites.
|
||||
if (!cuffed)
|
||||
return;
|
||||
sprite.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color!.Value);
|
||||
|
||||
if (!Equals(component.CurrentRSI, cuffState.RSI) && cuffState.RSI != null) // we don't want to keep loading the same RSI
|
||||
{
|
||||
component.CurrentRSI = cuffState.RSI;
|
||||
sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, cuffState.IconState, component.CurrentRSI);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, cuffState.IconState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user