Ensnaring Component and Bola Update (#9968)
This commit is contained in:
15
Content.Server/Ensnaring/Components/EnsnareableComponent.cs
Normal file
15
Content.Server/Ensnaring/Components/EnsnareableComponent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Content.Shared.Ensnaring.Components;
|
||||
using Robust.Shared.Containers;
|
||||
|
||||
namespace Content.Server.Ensnaring.Components;
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedEnsnareableComponent))]
|
||||
public sealed class EnsnareableComponent : SharedEnsnareableComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// The container where the <see cref="EnsnaringComponent"/> entity will be stored
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("container")]
|
||||
public Container Container = default!;
|
||||
}
|
||||
43
Content.Server/Ensnaring/Components/EnsnaringComponent.cs
Normal file
43
Content.Server/Ensnaring/Components/EnsnaringComponent.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.Ensnaring.Components;
|
||||
|
||||
namespace Content.Server.Ensnaring.Components;
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedEnsnaringComponent))]
|
||||
public sealed class EnsnaringComponent : SharedEnsnaringComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Should movement cancel breaking out?
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("canMoveBreakout")]
|
||||
public bool CanMoveBreakout;
|
||||
|
||||
public CancellationTokenSource? CancelToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for the do after event to free the entity that owns the <see cref="EnsnareableComponent"/>
|
||||
/// </summary>
|
||||
public sealed class FreeEnsnareDoAfterComplete : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid EnsnaringEntity;
|
||||
|
||||
public FreeEnsnareDoAfterComplete(EntityUid ensnaringEntity)
|
||||
{
|
||||
EnsnaringEntity = ensnaringEntity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for the do after event when it fails to free the entity that owns the <see cref="EnsnareableComponent"/>
|
||||
/// </summary>
|
||||
public sealed class FreeEnsnareDoAfterCancel : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid EnsnaringEntity;
|
||||
|
||||
public FreeEnsnareDoAfterCancel(EntityUid ensnaringEntity)
|
||||
{
|
||||
EnsnaringEntity = ensnaringEntity;
|
||||
}
|
||||
}
|
||||
149
Content.Server/Ensnaring/EnsnareableSystem.Ensnaring.cs
Normal file
149
Content.Server/Ensnaring/EnsnareableSystem.Ensnaring.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System.Threading;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Ensnaring.Components;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Ensnaring.Components;
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Content.Shared.StepTrigger.Systems;
|
||||
using Content.Shared.Throwing;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Ensnaring;
|
||||
|
||||
public sealed partial class EnsnareableSystem
|
||||
{
|
||||
[Dependency] private readonly DoAfterSystem _doAfter = default!;
|
||||
[Dependency] private readonly AlertsSystem _alerts = default!;
|
||||
|
||||
public void InitializeEnsnaring()
|
||||
{
|
||||
SubscribeLocalEvent<EnsnaringComponent, ComponentRemove>(OnComponentRemove);
|
||||
SubscribeLocalEvent<EnsnaringComponent, StepTriggerAttemptEvent>(AttemptStepTrigger);
|
||||
SubscribeLocalEvent<EnsnaringComponent, StepTriggeredEvent>(OnStepTrigger);
|
||||
SubscribeLocalEvent<EnsnaringComponent, ThrowDoHitEvent>(OnThrowHit);
|
||||
}
|
||||
|
||||
private void OnComponentRemove(EntityUid uid, EnsnaringComponent component, ComponentRemove args)
|
||||
{
|
||||
if (!TryComp<EnsnareableComponent>(component.Ensnared, out var ensnared))
|
||||
return;
|
||||
|
||||
if (ensnared.IsEnsnared)
|
||||
ForceFree(component);
|
||||
}
|
||||
|
||||
private void AttemptStepTrigger(EntityUid uid, EnsnaringComponent component, ref StepTriggerAttemptEvent args)
|
||||
{
|
||||
args.Continue = true;
|
||||
}
|
||||
|
||||
private void OnStepTrigger(EntityUid uid, EnsnaringComponent component, ref StepTriggeredEvent args)
|
||||
{
|
||||
TryEnsnare(args.Tripper, component);
|
||||
}
|
||||
|
||||
private void OnThrowHit(EntityUid uid, EnsnaringComponent component, ThrowDoHitEvent args)
|
||||
{
|
||||
if (!component.CanThrowTrigger)
|
||||
return;
|
||||
|
||||
TryEnsnare(args.Target, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used where you want to try to ensnare an entity with the <see cref="EnsnareableComponent"/>
|
||||
/// </summary>
|
||||
/// <param name="ensnaringEntity">The entity that will be used to ensnare</param>
|
||||
/// <param name="target">The entity that will be ensnared</param>
|
||||
/// <param name="component">The ensnaring component</param>
|
||||
public void TryEnsnare(EntityUid target, EnsnaringComponent component)
|
||||
{
|
||||
//Don't do anything if they don't have the ensnareable component.
|
||||
if (!TryComp<EnsnareableComponent>(target, out var ensnareable))
|
||||
return;
|
||||
|
||||
component.Ensnared = target;
|
||||
ensnareable.Container.Insert(component.Owner);
|
||||
ensnareable.IsEnsnared = true;
|
||||
|
||||
UpdateAlert(ensnareable);
|
||||
var ev = new EnsnareEvent(component.WalkSpeed, component.SprintSpeed);
|
||||
RaiseLocalEvent(target, ev, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used where you want to try to free an entity with the <see cref="EnsnareableComponent"/>
|
||||
/// </summary>
|
||||
/// <param name="target">The entity that will be free</param>
|
||||
/// <param name="component">The ensnaring component</param>
|
||||
public void TryFree(EntityUid target, EnsnaringComponent component, EntityUid? user = null)
|
||||
{
|
||||
//Don't do anything if they don't have the ensnareable component.
|
||||
if (!TryComp<EnsnareableComponent>(target, out var ensnareable))
|
||||
return;
|
||||
|
||||
if (component.CancelToken != null)
|
||||
return;
|
||||
|
||||
component.CancelToken = new CancellationTokenSource();
|
||||
|
||||
var isOwner = !(user != null && target != user);
|
||||
var freeTime = isOwner ? component.BreakoutTime : component.FreeTime;
|
||||
bool breakOnMove;
|
||||
|
||||
if (isOwner)
|
||||
breakOnMove = !component.CanMoveBreakout;
|
||||
else
|
||||
breakOnMove = true;
|
||||
|
||||
var doAfterEventArgs = new DoAfterEventArgs(target, freeTime, component.CancelToken.Token, target)
|
||||
{
|
||||
BreakOnUserMove = breakOnMove,
|
||||
BreakOnTargetMove = breakOnMove,
|
||||
BreakOnDamage = false,
|
||||
BreakOnStun = true,
|
||||
NeedHand = true,
|
||||
TargetFinishedEvent = new FreeEnsnareDoAfterComplete(component.Owner),
|
||||
TargetCancelledEvent = new FreeEnsnareDoAfterCancel(component.Owner),
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs);
|
||||
|
||||
if (isOwner)
|
||||
_popup.PopupEntity(Loc.GetString("ensnare-component-try-free", ("ensnare", component.Owner)), target, Filter.Entities(target));
|
||||
|
||||
if (!isOwner && user != null)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("ensnare-component-try-free-other", ("ensnare", component.Owner), ("user", Identity.Entity(target, EntityManager))), user.Value, Filter.Entities(user.Value));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to force free someone for things like if the <see cref="EnsnaringComponent"/> is removed
|
||||
/// </summary>
|
||||
public void ForceFree(EnsnaringComponent component)
|
||||
{
|
||||
if (!TryComp<EnsnareableComponent>(component.Ensnared, out var ensnareable))
|
||||
return;
|
||||
|
||||
ensnareable.Container.ForceRemove(component.Owner);
|
||||
ensnareable.IsEnsnared = false;
|
||||
component.Ensnared = null;
|
||||
|
||||
UpdateAlert(ensnareable);
|
||||
var ev = new EnsnareRemoveEvent();
|
||||
RaiseLocalEvent(component.Owner, ev, false);
|
||||
}
|
||||
|
||||
public void UpdateAlert(EnsnareableComponent component)
|
||||
{
|
||||
if (!component.IsEnsnared)
|
||||
{
|
||||
_alerts.ClearAlert(component.Owner, AlertType.Ensnared);
|
||||
}
|
||||
else
|
||||
{
|
||||
_alerts.ShowAlert(component.Owner, AlertType.Ensnared);
|
||||
}
|
||||
}
|
||||
}
|
||||
62
Content.Server/Ensnaring/EnsnareableSystem.cs
Normal file
62
Content.Server/Ensnaring/EnsnareableSystem.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Content.Server.Ensnaring.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Ensnaring;
|
||||
using Content.Shared.Ensnaring.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Server.Containers;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Ensnaring;
|
||||
|
||||
public sealed partial class EnsnareableSystem : SharedEnsnareableSystem
|
||||
{
|
||||
[Dependency] private readonly ContainerSystem _container = default!;
|
||||
[Dependency] private readonly PopupSystem _popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
InitializeEnsnaring();
|
||||
|
||||
SubscribeLocalEvent<EnsnareableComponent, ComponentInit>(OnEnsnareableInit);
|
||||
SubscribeLocalEvent<EnsnareableComponent, FreeEnsnareDoAfterComplete>(OnFreeComplete);
|
||||
SubscribeLocalEvent<EnsnareableComponent, FreeEnsnareDoAfterCancel>(OnFreeFail);
|
||||
}
|
||||
|
||||
private void OnEnsnareableInit(EntityUid uid, EnsnareableComponent component, ComponentInit args)
|
||||
{
|
||||
component.Container = _container.EnsureContainer<Container>(component.Owner, "ensnare");
|
||||
}
|
||||
|
||||
private void OnFreeComplete(EntityUid uid, EnsnareableComponent component, FreeEnsnareDoAfterComplete args)
|
||||
{
|
||||
if (!TryComp<EnsnaringComponent>(args.EnsnaringEntity, out var ensnaring))
|
||||
return;
|
||||
|
||||
component.Container.Remove(args.EnsnaringEntity);
|
||||
component.IsEnsnared = false;
|
||||
ensnaring.Ensnared = null;
|
||||
|
||||
_popup.PopupEntity(Loc.GetString("ensnare-component-try-free-complete", ("ensnare", args.EnsnaringEntity)),
|
||||
uid, Filter.Entities(uid), PopupType.Large);
|
||||
|
||||
UpdateAlert(component);
|
||||
var ev = new EnsnareRemoveEvent();
|
||||
RaiseLocalEvent(uid, ev, false);
|
||||
|
||||
ensnaring.CancelToken = null;
|
||||
}
|
||||
|
||||
private void OnFreeFail(EntityUid uid, EnsnareableComponent component, FreeEnsnareDoAfterCancel args)
|
||||
{
|
||||
if (!TryComp<EnsnaringComponent>(args.EnsnaringEntity, out var ensnaring))
|
||||
return;
|
||||
|
||||
ensnaring.CancelToken = null;
|
||||
|
||||
_popup.PopupEntity(Loc.GetString("ensnare-component-try-free-fail", ("ensnare", args.EnsnaringEntity)),
|
||||
uid, Filter.Entities(uid), PopupType.Large);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user