New Status Effects system: Relay events (#38579)
This commit is contained in:
51
Content.Shared/StatusEffectNew/StatusEffectSystem.Relay.cs
Normal file
51
Content.Shared/StatusEffectNew/StatusEffectSystem.Relay.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Content.Shared.StatusEffectNew.Components;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Shared.StatusEffectNew;
|
||||
|
||||
public abstract partial class SharedStatusEffectsSystem
|
||||
{
|
||||
protected void InitializeRelay()
|
||||
{
|
||||
SubscribeLocalEvent<StatusEffectContainerComponent, LocalPlayerAttachedEvent>(RelayStatusEffectEvent);
|
||||
SubscribeLocalEvent<StatusEffectContainerComponent, LocalPlayerDetachedEvent>(RelayStatusEffectEvent);
|
||||
}
|
||||
|
||||
protected void RefRelayStatusEffectEvent<T>(EntityUid uid, StatusEffectContainerComponent component, ref T args) where T : struct
|
||||
{
|
||||
RelayEvent((uid, component), ref args);
|
||||
}
|
||||
|
||||
protected void RelayStatusEffectEvent<T>(EntityUid uid, StatusEffectContainerComponent component, T args) where T : class
|
||||
{
|
||||
RelayEvent((uid, component), args);
|
||||
}
|
||||
|
||||
public void RelayEvent<T>(Entity<StatusEffectContainerComponent> statusEffect, ref T args) where T : struct
|
||||
{
|
||||
// this copies the by-ref event if it is a struct
|
||||
var ev = new StatusEffectRelayedEvent<T>(args);
|
||||
foreach (var activeEffect in statusEffect.Comp.ActiveStatusEffects)
|
||||
{
|
||||
RaiseLocalEvent(activeEffect, ref ev);
|
||||
}
|
||||
// and now we copy it back
|
||||
args = ev.Args;
|
||||
}
|
||||
|
||||
public void RelayEvent<T>(Entity<StatusEffectContainerComponent> statusEffect, T args) where T : class
|
||||
{
|
||||
// this copies the by-ref event if it is a struct
|
||||
var ev = new StatusEffectRelayedEvent<T>(args);
|
||||
foreach (var activeEffect in statusEffect.Comp.ActiveStatusEffects)
|
||||
{
|
||||
RaiseLocalEvent(activeEffect, ref ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event wrapper for relayed events.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct StatusEffectRelayedEvent<TEvent>(TEvent Args);
|
||||
Reference in New Issue
Block a user