Make DoAfters use DamageChangedMessage instead of an event (#3115)
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Content.Server.GameObjects.EntitySystems.DoAfter;
|
using Content.Server.GameObjects.EntitySystems.DoAfter;
|
||||||
using Content.Shared.GameObjects.Components;
|
using Content.Shared.GameObjects.Components;
|
||||||
|
using Content.Shared.GameObjects.Components.Damage;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.Interfaces.GameObjects;
|
||||||
|
|
||||||
namespace Content.Server.GameObjects.Components
|
namespace Content.Server.GameObjects.Components
|
||||||
{
|
{
|
||||||
@@ -40,6 +42,35 @@ namespace Content.Server.GameObjects.Components
|
|||||||
return new DoAfterComponentState(toAdd);
|
return new DoAfterComponentState(toAdd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||||
|
{
|
||||||
|
base.HandleMessage(message, component);
|
||||||
|
|
||||||
|
switch (message)
|
||||||
|
{
|
||||||
|
case DamageChangedMessage msg:
|
||||||
|
if (DoAfters.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!msg.TookDamage)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var doAfter in _doAfters.Keys)
|
||||||
|
{
|
||||||
|
if (doAfter.EventArgs.BreakOnDamage)
|
||||||
|
{
|
||||||
|
doAfter.TookDamage = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Add(DoAfter doAfter)
|
public void Add(DoAfter doAfter)
|
||||||
{
|
{
|
||||||
_doAfters.Add(doAfter, _runningIndex);
|
_doAfters.Add(doAfter, _runningIndex);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using System.Threading.Tasks;
|
|||||||
using Content.Server.GameObjects.Components.GUI;
|
using Content.Server.GameObjects.Components.GUI;
|
||||||
using Content.Server.GameObjects.Components.Items.Storage;
|
using Content.Server.GameObjects.Components.Items.Storage;
|
||||||
using Content.Server.GameObjects.Components.Mobs;
|
using Content.Server.GameObjects.Components.Mobs;
|
||||||
using Content.Shared.GameObjects.Components.Damage;
|
|
||||||
using Robust.Shared.Interfaces.Timing;
|
using Robust.Shared.Interfaces.Timing;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
@@ -27,7 +26,7 @@ namespace Content.Server.GameObjects.EntitySystems.DoAfter
|
|||||||
|
|
||||||
public EntityCoordinates TargetGrid { get; }
|
public EntityCoordinates TargetGrid { get; }
|
||||||
|
|
||||||
private bool _tookDamage;
|
public bool TookDamage { get; set; }
|
||||||
|
|
||||||
public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running;
|
public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running;
|
||||||
|
|
||||||
@@ -63,11 +62,6 @@ namespace Content.Server.GameObjects.EntitySystems.DoAfter
|
|||||||
AsTask = Tcs.Task;
|
AsTask = Tcs.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandleDamage(DamageChangedEventArgs args)
|
|
||||||
{
|
|
||||||
_tookDamage = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Run(float frameTime)
|
public void Run(float frameTime)
|
||||||
{
|
{
|
||||||
switch (Status)
|
switch (Status)
|
||||||
@@ -130,7 +124,7 @@ namespace Content.Server.GameObjects.EntitySystems.DoAfter
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EventArgs.BreakOnDamage && _tookDamage)
|
if (EventArgs.BreakOnDamage && TookDamage)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Content.Server.GameObjects.Components;
|
using Content.Server.GameObjects.Components;
|
||||||
using Content.Shared.GameObjects.Components.Damage;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Robust.Shared.GameObjects.Systems;
|
using Robust.Shared.GameObjects.Systems;
|
||||||
|
|
||||||
@@ -68,21 +67,9 @@ namespace Content.Server.GameObjects.EntitySystems.DoAfter
|
|||||||
// Caller's gonna be responsible for this I guess
|
// Caller's gonna be responsible for this I guess
|
||||||
var doAfterComponent = eventArgs.User.GetComponent<DoAfterComponent>();
|
var doAfterComponent = eventArgs.User.GetComponent<DoAfterComponent>();
|
||||||
doAfterComponent.Add(doAfter);
|
doAfterComponent.Add(doAfter);
|
||||||
IDamageableComponent? damageableComponent = null;
|
|
||||||
|
|
||||||
// TODO: If the component's deleted this may not get unsubscribed?
|
|
||||||
if (eventArgs.BreakOnDamage && eventArgs.User.TryGetComponent(out damageableComponent))
|
|
||||||
{
|
|
||||||
damageableComponent.HealthChangedEvent += doAfter.HandleDamage;
|
|
||||||
}
|
|
||||||
|
|
||||||
await doAfter.AsTask;
|
await doAfter.AsTask;
|
||||||
|
|
||||||
if (damageableComponent != null)
|
|
||||||
{
|
|
||||||
damageableComponent.HealthChangedEvent -= doAfter.HandleDamage;
|
|
||||||
}
|
|
||||||
|
|
||||||
return doAfter.Status;
|
return doAfter.Status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,5 +31,21 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
/// List containing data on each <see cref="DamageType"/> that was changed.
|
/// List containing data on each <see cref="DamageType"/> that was changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<DamageChangeData> Data { get; }
|
public IReadOnlyList<DamageChangeData> Data { get; }
|
||||||
|
|
||||||
|
public bool TookDamage
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
foreach (var datum in Data)
|
||||||
|
{
|
||||||
|
if (datum.Delta > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,6 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
private readonly HashSet<DamageClass> _supportedClasses = new();
|
private readonly HashSet<DamageClass> _supportedClasses = new();
|
||||||
private DamageFlag _flags;
|
private DamageFlag _flags;
|
||||||
|
|
||||||
public event Action<DamageChangedEventArgs>? HealthChangedEvent;
|
|
||||||
|
|
||||||
// TODO DAMAGE Use as default values, specify overrides in a separate property through yaml for better (de)serialization
|
// TODO DAMAGE Use as default values, specify overrides in a separate property through yaml for better (de)serialization
|
||||||
[ViewVariables] public string DamageContainerId { get; set; } = default!;
|
[ViewVariables] public string DamageContainerId { get; set; } = default!;
|
||||||
|
|
||||||
@@ -460,7 +458,6 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
protected virtual void OnHealthChanged(DamageChangedEventArgs e)
|
protected virtual void OnHealthChanged(DamageChangedEventArgs e)
|
||||||
{
|
{
|
||||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, e);
|
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, e);
|
||||||
HealthChangedEvent?.Invoke(e);
|
|
||||||
|
|
||||||
var message = new DamageChangedMessage(this, e.Data);
|
var message = new DamageChangedMessage(this, e.Data);
|
||||||
SendMessage(message);
|
SendMessage(message);
|
||||||
|
|||||||
@@ -9,14 +9,6 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
{
|
{
|
||||||
public interface IDamageableComponent : IComponent, IExAct
|
public interface IDamageableComponent : IComponent, IExAct
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Called when the entity's <see cref="IDamageableComponent"/> values change.
|
|
||||||
/// Of note is that a "deal 0 damage" call will still trigger this event
|
|
||||||
/// (including both damage negated by resistance or simply inputting 0 as
|
|
||||||
/// the amount of damage to deal).
|
|
||||||
/// </summary>
|
|
||||||
event Action<DamageChangedEventArgs> HealthChangedEvent;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sum of all damages taken.
|
/// Sum of all damages taken.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user