Allows pickuppable animals to resist out of inventory (#7545)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
keronshb
2022-05-12 03:58:27 -04:00
committed by GitHub
parent 7a08985994
commit e2ac92c3b5
5 changed files with 116 additions and 0 deletions

View File

@@ -348,6 +348,7 @@ namespace Content.Client.Entry
"InteractionPopup", "InteractionPopup",
"HealthAnalyzer", "HealthAnalyzer",
"Thirst", "Thirst",
"CanEscapeInventory",
"Wires" "Wires"
}; };
} }

View File

@@ -0,0 +1,29 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using Robust.Shared.Analyzers;
using System.Threading;
namespace Content.Server.Resist;
[RegisterComponent]
public sealed class CanEscapeInventoryComponent : Component
{
/// <summary>
/// How long it takes to break out of storage. Default at 5 seconds.
/// </summary>
[ViewVariables]
[DataField("resistTime")]
public float ResistTime = 5f;
/// <summary>
/// For quick exit if the player attempts to move while already resisting
/// </summary>
[ViewVariables]
public bool IsResisting = false;
/// <summary>
/// Cancellation token used to cancel the DoAfter if the mob is removed before it's complete
/// </summary>
public CancellationTokenSource? CancelToken;
}

View File

@@ -0,0 +1,83 @@
using Content.Shared.Movement;
using Content.Server.DoAfter;
using Robust.Shared.Containers;
using Content.Server.Popups;
using Content.Shared.Movement.EntitySystems;
using Robust.Shared.Player;
using Content.Shared.Storage;
using Content.Shared.Inventory;
using Content.Shared.Hands.Components;
namespace Content.Server.Resist;
public sealed class EscapeInventorySystem : EntitySystem
{
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CanEscapeInventoryComponent, RelayMoveInputEvent>(OnRelayMovement);
SubscribeLocalEvent<CanEscapeInventoryComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeDoAfterComplete>(OnEscapeComplete);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeDoAfterCancel>(OnEscapeFail);
}
private void OnRelayMovement(EntityUid uid, CanEscapeInventoryComponent component, RelayMoveInputEvent args)
{
//Prevents the user from creating multiple DoAfters if they're already resisting.
if (component.IsResisting == true)
return;
if (_containerSystem.TryGetContainingContainer(uid, out var container)
&& (HasComp<SharedStorageComponent>(container.Owner) || HasComp<InventoryComponent>(container.Owner) || HasComp<SharedHandsComponent>(container.Owner)))
{
AttemptEscape(uid, container.Owner, component);
}
}
private void OnMoveAttempt(EntityUid uid, CanEscapeInventoryComponent component, UpdateCanMoveEvent args)
{
if (_containerSystem.IsEntityOrParentInContainer(uid))
args.Cancel();
}
private void AttemptEscape(EntityUid user, EntityUid container, CanEscapeInventoryComponent component)
{
component.CancelToken = new();
var doAfterEventArgs = new DoAfterEventArgs(user, component.ResistTime, component.CancelToken.Token, container)
{
BreakOnTargetMove = false,
BreakOnUserMove = false,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = false,
UserFinishedEvent = new EscapeDoAfterComplete(),
UserCancelledEvent = new EscapeDoAfterCancel(),
};
component.IsResisting = true;
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting"), user, Filter.Entities(user));
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting-target"), container, Filter.Entities(container));
_doAfterSystem.DoAfter(doAfterEventArgs);
}
private void OnEscapeComplete(EntityUid uid, CanEscapeInventoryComponent component, EscapeDoAfterComplete ev)
{
//Drops the mob on the tile below the container
Transform(uid).AttachParentToContainerOrGrid(EntityManager);
component.IsResisting = false;
}
private void OnEscapeFail(EntityUid uid, CanEscapeInventoryComponent component, EscapeDoAfterCancel ev)
{
component.IsResisting = false;
}
private sealed class EscapeDoAfterComplete : EntityEventArgs { }
private sealed class EscapeDoAfterCancel : EntityEventArgs { }
}

View File

@@ -0,0 +1,2 @@
escape-inventory-component-start-resisting = You start struggling to escape!
escape-inventory-component-start-resisting-target = Something is struggling to get out of your inventory!

View File

@@ -747,6 +747,7 @@
- type: Bloodstream - type: Bloodstream
bloodMaxVolume: 50 bloodMaxVolume: 50
- type: DiseaseCarrier #The other class lab animal and disease vector - type: DiseaseCarrier #The other class lab animal and disease vector
- type: CanEscapeInventory
- type: entity - type: entity