Interrupt do-afters when the user gets incapacitated. (#6059)

This commit is contained in:
Leon Friedrich
2022-01-07 19:07:06 +13:00
committed by GitHub
parent 5d1c6a1867
commit 106f176d13
2 changed files with 21 additions and 11 deletions

View File

@@ -26,8 +26,6 @@ namespace Content.Server.DoAfter
public EntityCoordinates TargetGrid { get; } public EntityCoordinates TargetGrid { get; }
public bool TookDamage { get; set; }
public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running; public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running;
// NeedHand // NeedHand
@@ -62,6 +60,12 @@ namespace Content.Server.DoAfter
AsTask = Tcs.Task; AsTask = Tcs.Task;
} }
public void Cancel()
{
if (Status == DoAfterStatus.Running)
Tcs.SetResult(DoAfterStatus.Cancelled);
}
public void Run(float frameTime, IEntityManager entityManager) public void Run(float frameTime, IEntityManager entityManager)
{ {
switch (Status) switch (Status)
@@ -124,11 +128,6 @@ namespace Content.Server.DoAfter
return true; return true;
} }
if (EventArgs.BreakOnDamage && TookDamage)
{
return true;
}
if (EventArgs.ExtraCheck != null && !EventArgs.ExtraCheck.Invoke()) if (EventArgs.ExtraCheck != null && !EventArgs.ExtraCheck.Invoke())
{ {
return true; return true;

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.MobState;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -19,20 +20,30 @@ namespace Content.Server.DoAfter
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<DoAfterComponent, DamageChangedEvent>(HandleDamage); SubscribeLocalEvent<DoAfterComponent, DamageChangedEvent>(HandleDamage);
SubscribeLocalEvent<DoAfterComponent, MobStateChangedEvent>(HandleStateChanged);
}
private void HandleStateChanged(EntityUid uid, DoAfterComponent component, MobStateChangedEvent args)
{
if (!args.CurrentMobState.IsIncapacitated())
return;
foreach (var doAfter in component.DoAfters)
{
doAfter.Cancel();
}
} }
public void HandleDamage(EntityUid _, DoAfterComponent component, DamageChangedEvent args) public void HandleDamage(EntityUid _, DoAfterComponent component, DamageChangedEvent args)
{ {
if (component.DoAfters.Count == 0 || !args.InterruptsDoAfters) if (!args.InterruptsDoAfters || !args.DamageIncreased)
{
return; return;
}
foreach (var doAfter in component.DoAfters) foreach (var doAfter in component.DoAfters)
{ {
if (doAfter.EventArgs.BreakOnDamage) if (doAfter.EventArgs.BreakOnDamage)
{ {
doAfter.TookDamage = true; doAfter.Cancel();
} }
} }
} }