New Status Effects system: Relay events (#38579)
This commit is contained in:
@@ -2,6 +2,7 @@ using Content.Shared.Drowsiness;
|
|||||||
using Content.Shared.StatusEffectNew;
|
using Content.Shared.StatusEffectNew;
|
||||||
using Robust.Client.Graphics;
|
using Robust.Client.Graphics;
|
||||||
using Robust.Client.Player;
|
using Robust.Client.Player;
|
||||||
|
using Robust.Shared.Player;
|
||||||
|
|
||||||
namespace Content.Client.Drowsiness;
|
namespace Content.Client.Drowsiness;
|
||||||
|
|
||||||
@@ -20,8 +21,8 @@ public sealed class DrowsinessSystem : SharedDrowsinessSystem
|
|||||||
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectAppliedEvent>(OnDrowsinessApply);
|
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectAppliedEvent>(OnDrowsinessApply);
|
||||||
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectRemovedEvent>(OnDrowsinessShutdown);
|
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectRemovedEvent>(OnDrowsinessShutdown);
|
||||||
|
|
||||||
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectPlayerAttachedEvent>(OnStatusEffectPlayerAttached);
|
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectRelayedEvent<LocalPlayerAttachedEvent>>(OnStatusEffectPlayerAttached);
|
||||||
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectPlayerDetachedEvent>(OnStatusEffectPlayerDetached);
|
SubscribeLocalEvent<DrowsinessStatusEffectComponent, StatusEffectRelayedEvent<LocalPlayerDetachedEvent>>(OnStatusEffectPlayerDetached);
|
||||||
|
|
||||||
_overlay = new();
|
_overlay = new();
|
||||||
}
|
}
|
||||||
@@ -44,17 +45,14 @@ public sealed class DrowsinessSystem : SharedDrowsinessSystem
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnStatusEffectPlayerAttached(Entity<DrowsinessStatusEffectComponent> ent, ref StatusEffectPlayerAttachedEvent args)
|
private void OnStatusEffectPlayerAttached(Entity<DrowsinessStatusEffectComponent> ent, ref StatusEffectRelayedEvent<LocalPlayerAttachedEvent> args)
|
||||||
{
|
{
|
||||||
if (_player.LocalEntity != args.Target)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_overlayMan.AddOverlay(_overlay);
|
_overlayMan.AddOverlay(_overlay);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnStatusEffectPlayerDetached(Entity<DrowsinessStatusEffectComponent> ent, ref StatusEffectPlayerDetachedEvent args)
|
private void OnStatusEffectPlayerDetached(Entity<DrowsinessStatusEffectComponent> ent, ref StatusEffectRelayedEvent<LocalPlayerDetachedEvent> args)
|
||||||
{
|
{
|
||||||
if (_player.LocalEntity != args.Target)
|
if (_player.LocalEntity is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!_statusEffects.HasEffectComp<DrowsinessStatusEffectComponent>(_player.LocalEntity.Value))
|
if (!_statusEffects.HasEffectComp<DrowsinessStatusEffectComponent>(_player.LocalEntity.Value))
|
||||||
|
|||||||
@@ -25,11 +25,4 @@ public sealed partial class SSDIndicatorComponent : Component
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[DataField, AutoPausedField, Access(typeof(SSDIndicatorSystem))]
|
[DataField, AutoPausedField, Access(typeof(SSDIndicatorSystem))]
|
||||||
public TimeSpan FallAsleepTime = TimeSpan.Zero;
|
public TimeSpan FallAsleepTime = TimeSpan.Zero;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Required to don't remove forced sleep from other sources
|
|
||||||
/// </summary>
|
|
||||||
[ViewVariables(VVAccess.ReadWrite)]
|
|
||||||
[AutoNetworkedField]
|
|
||||||
public bool ForcedSleepAdded = false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
using Content.Shared.Bed.Sleep;
|
|
||||||
using Content.Shared.CCVar;
|
using Content.Shared.CCVar;
|
||||||
using Content.Shared.StatusEffectNew;
|
using Content.Shared.StatusEffectNew;
|
||||||
using Robust.Shared.Configuration;
|
using Robust.Shared.Configuration;
|
||||||
using Robust.Shared.Player;
|
using Robust.Shared.Player;
|
||||||
|
using Robust.Shared.Prototypes;
|
||||||
using Robust.Shared.Timing;
|
using Robust.Shared.Timing;
|
||||||
|
|
||||||
namespace Content.Shared.SSDIndicator;
|
namespace Content.Shared.SSDIndicator;
|
||||||
@@ -12,6 +12,8 @@ namespace Content.Shared.SSDIndicator;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class SSDIndicatorSystem : EntitySystem
|
public sealed class SSDIndicatorSystem : EntitySystem
|
||||||
{
|
{
|
||||||
|
public static readonly EntProtoId StatusEffectSSDSleeping = "StatusEffectSSDSleeping";
|
||||||
|
|
||||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
[Dependency] private readonly IGameTiming _timing = default!;
|
[Dependency] private readonly IGameTiming _timing = default!;
|
||||||
[Dependency] private readonly SharedStatusEffectsSystem _statusEffects = default!;
|
[Dependency] private readonly SharedStatusEffectsSystem _statusEffects = default!;
|
||||||
@@ -37,11 +39,7 @@ public sealed class SSDIndicatorSystem : EntitySystem
|
|||||||
if (_icSsdSleep)
|
if (_icSsdSleep)
|
||||||
{
|
{
|
||||||
component.FallAsleepTime = TimeSpan.Zero;
|
component.FallAsleepTime = TimeSpan.Zero;
|
||||||
if (component.ForcedSleepAdded) // Remove component only if it has been added by this system
|
_statusEffects.TryRemoveStatusEffect(uid, StatusEffectSSDSleeping);
|
||||||
{
|
|
||||||
_statusEffects.TryRemoveStatusEffect(uid, SleepingSystem.StatusEffectForcedSleeping);
|
|
||||||
component.ForcedSleepAdded = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Dirty(uid, component);
|
Dirty(uid, component);
|
||||||
@@ -87,8 +85,7 @@ public sealed class SSDIndicatorSystem : EntitySystem
|
|||||||
ssd.FallAsleepTime <= _timing.CurTime &&
|
ssd.FallAsleepTime <= _timing.CurTime &&
|
||||||
!TerminatingOrDeleted(uid))
|
!TerminatingOrDeleted(uid))
|
||||||
{
|
{
|
||||||
_statusEffects.TryAddStatusEffect(uid, SleepingSystem.StatusEffectForcedSleeping);
|
_statusEffects.TryAddStatusEffect(uid, StatusEffectSSDSleeping);
|
||||||
ssd.ForcedSleepAdded = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using Content.Shared.StatusEffectNew.Components;
|
|||||||
using Content.Shared.Whitelist;
|
using Content.Shared.Whitelist;
|
||||||
using Robust.Shared.GameStates;
|
using Robust.Shared.GameStates;
|
||||||
using Robust.Shared.Network;
|
using Robust.Shared.Network;
|
||||||
using Robust.Shared.Player;
|
|
||||||
using Robust.Shared.Prototypes;
|
using Robust.Shared.Prototypes;
|
||||||
using Robust.Shared.Timing;
|
using Robust.Shared.Timing;
|
||||||
|
|
||||||
@@ -30,11 +29,11 @@ public abstract partial class SharedStatusEffectsSystem : EntitySystem
|
|||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
|
|
||||||
|
InitializeRelay();
|
||||||
|
|
||||||
SubscribeLocalEvent<StatusEffectComponent, StatusEffectAppliedEvent>(OnStatusEffectApplied);
|
SubscribeLocalEvent<StatusEffectComponent, StatusEffectAppliedEvent>(OnStatusEffectApplied);
|
||||||
SubscribeLocalEvent<StatusEffectComponent, StatusEffectRemovedEvent>(OnStatusEffectRemoved);
|
SubscribeLocalEvent<StatusEffectComponent, StatusEffectRemovedEvent>(OnStatusEffectRemoved);
|
||||||
|
|
||||||
SubscribeLocalEvent<StatusEffectContainerComponent, LocalPlayerAttachedEvent>(OnStatusEffectContainerAttached);
|
|
||||||
SubscribeLocalEvent<StatusEffectContainerComponent, LocalPlayerDetachedEvent>(OnStatusEffectContainerDetached);
|
|
||||||
SubscribeLocalEvent<StatusEffectContainerComponent, ComponentGetState>(OnGetState);
|
SubscribeLocalEvent<StatusEffectContainerComponent, ComponentGetState>(OnGetState);
|
||||||
|
|
||||||
_containerQuery = GetEntityQuery<StatusEffectContainerComponent>();
|
_containerQuery = GetEntityQuery<StatusEffectContainerComponent>();
|
||||||
@@ -46,24 +45,6 @@ public abstract partial class SharedStatusEffectsSystem : EntitySystem
|
|||||||
args.State = new StatusEffectContainerComponentState(GetNetEntitySet(ent.Comp.ActiveStatusEffects));
|
args.State = new StatusEffectContainerComponentState(GetNetEntitySet(ent.Comp.ActiveStatusEffects));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnStatusEffectContainerAttached(Entity<StatusEffectContainerComponent> ent, ref LocalPlayerAttachedEvent args)
|
|
||||||
{
|
|
||||||
foreach (var effect in ent.Comp.ActiveStatusEffects)
|
|
||||||
{
|
|
||||||
var ev = new StatusEffectPlayerAttachedEvent(ent);
|
|
||||||
RaiseLocalEvent(effect, ref ev);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnStatusEffectContainerDetached(Entity<StatusEffectContainerComponent> ent, ref LocalPlayerDetachedEvent args)
|
|
||||||
{
|
|
||||||
foreach (var effect in ent.Comp.ActiveStatusEffects)
|
|
||||||
{
|
|
||||||
var ev = new StatusEffectPlayerDetachedEvent(ent);
|
|
||||||
RaiseLocalEvent(effect, ref ev);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Update(float frameTime)
|
public override void Update(float frameTime)
|
||||||
{
|
{
|
||||||
base.Update(frameTime);
|
base.Update(frameTime);
|
||||||
@@ -187,20 +168,6 @@ public readonly record struct StatusEffectAppliedEvent(EntityUid Target);
|
|||||||
[ByRefEvent]
|
[ByRefEvent]
|
||||||
public readonly record struct StatusEffectRemovedEvent(EntityUid Target);
|
public readonly record struct StatusEffectRemovedEvent(EntityUid Target);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called on a status effect entity inside <see cref="StatusEffectContainerComponent"/>
|
|
||||||
/// after a player has been <see cref="LocalPlayerAttachedEvent"/> to this container entity.
|
|
||||||
/// </summary>
|
|
||||||
[ByRefEvent]
|
|
||||||
public readonly record struct StatusEffectPlayerAttachedEvent(EntityUid Target);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called on a status effect entity inside <see cref="StatusEffectContainerComponent"/>
|
|
||||||
/// after a player has been <see cref="LocalPlayerDetachedEvent"/> to this container entity.
|
|
||||||
/// </summary>
|
|
||||||
[ByRefEvent]
|
|
||||||
public readonly record struct StatusEffectPlayerDetachedEvent(EntityUid Target);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raised on an entity before a status effect is added to determine if adding it should be cancelled.
|
/// Raised on an entity before a status effect is added to determine if adding it should be cancelled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
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);
|
||||||
@@ -27,6 +27,14 @@
|
|||||||
components:
|
components:
|
||||||
- type: ForcedSleepingStatusEffect
|
- type: ForcedSleepingStatusEffect
|
||||||
|
|
||||||
|
# This creature is asleep because it's disconnected from the game.
|
||||||
|
- type: entity
|
||||||
|
parent: MobStatusEffectBase
|
||||||
|
id: StatusEffectSSDSleeping
|
||||||
|
name: forced sleep
|
||||||
|
components:
|
||||||
|
- type: ForcedSleepingStatusEffect
|
||||||
|
|
||||||
# Blurs your vision and makes you randomly fall asleep
|
# Blurs your vision and makes you randomly fall asleep
|
||||||
- type: entity
|
- type: entity
|
||||||
parent: MobStatusEffectBase
|
parent: MobStatusEffectBase
|
||||||
|
|||||||
Reference in New Issue
Block a user