Refactor SeeingRainbows to new status effect system (#38620)

This commit is contained in:
Red
2025-06-27 21:19:04 +03:00
committed by GitHub
parent ad2616a53f
commit bb116e3219
14 changed files with 110 additions and 100 deletions

View File

@@ -1,7 +1,5 @@
using Content.Shared.Bed.Sleep;
using Content.Shared.Drowsiness; using Content.Shared.Drowsiness;
using Content.Shared.StatusEffectNew; using Content.Shared.StatusEffectNew;
using Content.Shared.StatusEffectNew.Components;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Shared.Enums; using Robust.Shared.Enums;
@@ -23,8 +21,6 @@ public sealed class DrowsinessOverlay : Overlay
public override bool RequestScreenTexture => true; public override bool RequestScreenTexture => true;
private readonly ShaderInstance _drowsinessShader; private readonly ShaderInstance _drowsinessShader;
private EntityQuery<StatusEffectComponent> _statusQuery;
public float CurrentPower = 0.0f; public float CurrentPower = 0.0f;
private const float PowerDivisor = 250.0f; private const float PowerDivisor = 250.0f;
@@ -34,9 +30,9 @@ public sealed class DrowsinessOverlay : Overlay
public DrowsinessOverlay() public DrowsinessOverlay()
{ {
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
_statusEffects = _sysMan.GetEntitySystem<SharedStatusEffectsSystem>(); _statusEffects = _sysMan.GetEntitySystem<SharedStatusEffectsSystem>();
_statusQuery = _entityManager.GetEntityQuery<StatusEffectComponent>();
_drowsinessShader = _prototypeManager.Index<ShaderPrototype>("Drowsiness").InstanceUnique(); _drowsinessShader = _prototypeManager.Index<ShaderPrototype>("Drowsiness").InstanceUnique();
} }
@@ -47,22 +43,11 @@ public sealed class DrowsinessOverlay : Overlay
if (playerEntity == null) if (playerEntity == null)
return; return;
if (!_statusEffects.TryEffectsWithComp<DrowsinessStatusEffectComponent>(playerEntity, out var drowsinessEffects)) if (!_statusEffects.TryGetEffectsEndTimeWithComp<DrowsinessStatusEffectComponent>(playerEntity, out var endTime))
return; return;
TimeSpan? remainingTime = TimeSpan.Zero; endTime ??= TimeSpan.MaxValue;
foreach (var (_, _, statusEffectComp) in drowsinessEffects) var timeLeft = (float)(endTime - _timing.CurTime).Value.TotalSeconds;
{
if (statusEffectComp.EndEffectTime > remainingTime)
remainingTime = statusEffectComp.EndEffectTime;
}
if (remainingTime is null)
return;
var curTime = _timing.CurTime;
var timeLeft = (float)(remainingTime - curTime).Value.TotalSeconds;
CurrentPower += 8f * (0.5f * timeLeft - CurrentPower) * args.DeltaSeconds / (timeLeft + 1); CurrentPower += 8f * (0.5f * timeLeft - CurrentPower) * args.DeltaSeconds / (timeLeft + 1);
} }

View File

@@ -1,4 +1,5 @@
using Content.Shared.Drugs; using Content.Shared.Drugs;
using Content.Shared.StatusEffectNew;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Shared.Player; using Robust.Shared.Player;
@@ -17,49 +18,47 @@ public sealed class DrugOverlaySystem : EntitySystem
private RainbowOverlay _overlay = default!; private RainbowOverlay _overlay = default!;
public static string RainbowKey = "SeeingRainbows";
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SeeingRainbowsComponent, ComponentInit>(OnInit); SubscribeLocalEvent<SeeingRainbowsStatusEffectComponent, StatusEffectAppliedEvent>(OnApplied);
SubscribeLocalEvent<SeeingRainbowsComponent, ComponentShutdown>(OnShutdown); SubscribeLocalEvent<SeeingRainbowsStatusEffectComponent, StatusEffectRemovedEvent>(OnRemoved);
SubscribeLocalEvent<SeeingRainbowsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached); SubscribeLocalEvent<SeeingRainbowsStatusEffectComponent, StatusEffectRelayedEvent<LocalPlayerAttachedEvent>>(OnPlayerAttached);
SubscribeLocalEvent<SeeingRainbowsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached); SubscribeLocalEvent<SeeingRainbowsStatusEffectComponent, StatusEffectRelayedEvent<LocalPlayerDetachedEvent>>(OnPlayerDetached);
_overlay = new(); _overlay = new();
} }
private void OnPlayerAttached(EntityUid uid, SeeingRainbowsComponent component, LocalPlayerAttachedEvent args) private void OnRemoved(Entity<SeeingRainbowsStatusEffectComponent> ent, ref StatusEffectRemovedEvent args)
{ {
_overlayMan.AddOverlay(_overlay); if (_player.LocalEntity != args.Target)
} return;
private void OnPlayerDetached(EntityUid uid, SeeingRainbowsComponent component, LocalPlayerDetachedEvent args)
{
_overlay.Intoxication = 0; _overlay.Intoxication = 0;
_overlay.TimeTicker = 0; _overlay.TimeTicker = 0;
_overlayMan.RemoveOverlay(_overlay); _overlayMan.RemoveOverlay(_overlay);
} }
private void OnInit(EntityUid uid, SeeingRainbowsComponent component, ComponentInit args) private void OnApplied(Entity<SeeingRainbowsStatusEffectComponent> ent, ref StatusEffectAppliedEvent args)
{
if (_player.LocalEntity == uid)
{ {
if (_player.LocalEntity != args.Target)
return;
_overlay.Phase = _random.NextFloat(MathF.Tau); // random starting phase for movement effect _overlay.Phase = _random.NextFloat(MathF.Tau); // random starting phase for movement effect
_overlayMan.AddOverlay(_overlay); _overlayMan.AddOverlay(_overlay);
} }
private void OnPlayerAttached(Entity<SeeingRainbowsStatusEffectComponent> ent, ref StatusEffectRelayedEvent<LocalPlayerAttachedEvent> args)
{
_overlayMan.AddOverlay(_overlay);
} }
private void OnShutdown(EntityUid uid, SeeingRainbowsComponent component, ComponentShutdown args) private void OnPlayerDetached(Entity<SeeingRainbowsStatusEffectComponent> ent, ref StatusEffectRelayedEvent<LocalPlayerDetachedEvent> args)
{
if (_player.LocalEntity == uid)
{ {
_overlay.Intoxication = 0; _overlay.Intoxication = 0;
_overlay.TimeTicker = 0; _overlay.TimeTicker = 0;
_overlayMan.RemoveOverlay(_overlay); _overlayMan.RemoveOverlay(_overlay);
} }
} }
}

View File

@@ -1,6 +1,6 @@
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Content.Shared.Drugs; using Content.Shared.Drugs;
using Content.Shared.StatusEffect; using Content.Shared.StatusEffectNew;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
@@ -17,6 +17,8 @@ public sealed class RainbowOverlay : Overlay
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!; [Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IEntitySystemManager _sysMan = default!; [Dependency] private readonly IEntitySystemManager _sysMan = default!;
[Dependency] private readonly IGameTiming _timing = default!;
private readonly SharedStatusEffectsSystem _statusEffects = default!;
public override OverlaySpace Space => OverlaySpace.WorldSpace; public override OverlaySpace Space => OverlaySpace.WorldSpace;
public override bool RequestScreenTexture => true; public override bool RequestScreenTexture => true;
@@ -37,6 +39,8 @@ public sealed class RainbowOverlay : Overlay
{ {
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
_statusEffects = _sysMan.GetEntitySystem<SharedStatusEffectsSystem>();
_rainbowShader = _prototypeManager.Index<ShaderPrototype>("Rainbow").InstanceUnique(); _rainbowShader = _prototypeManager.Index<ShaderPrototype>("Rainbow").InstanceUnique();
_config.OnValueChanged(CCVars.ReducedMotion, OnReducedMotionChanged, invokeImmediately: true); _config.OnValueChanged(CCVars.ReducedMotion, OnReducedMotionChanged, invokeImmediately: true);
} }
@@ -54,18 +58,13 @@ public sealed class RainbowOverlay : Overlay
if (playerEntity == null) if (playerEntity == null)
return; return;
if (!_entityManager.HasComponent<SeeingRainbowsComponent>(playerEntity) if (!_statusEffects.TryGetEffectsEndTimeWithComp<SeeingRainbowsStatusEffectComponent>(playerEntity, out var endTime))
|| !_entityManager.TryGetComponent<StatusEffectsComponent>(playerEntity, out var status))
return; return;
var statusSys = _sysMan.GetEntitySystem<StatusEffectsSystem>(); endTime ??= TimeSpan.MaxValue;
if (!statusSys.TryGetTime(playerEntity.Value, DrugOverlaySystem.RainbowKey, out var time, status)) var timeLeft = (float)(endTime - _timing.CurTime).Value.TotalSeconds;
return;
var timeLeft = (float)(time.Value.Item2 - time.Value.Item1).TotalSeconds;
TimeTicker += args.DeltaSeconds; TimeTicker += args.DeltaSeconds;
if (timeLeft - TimeTicker > timeLeft / 16f) if (timeLeft - TimeTicker > timeLeft / 16f)
{ {
Intoxication += (timeLeft - Intoxication) * args.DeltaSeconds / 16f; Intoxication += (timeLeft - Intoxication) * args.DeltaSeconds / 16f;

View File

@@ -3,7 +3,8 @@ using Robust.Shared.GameStates;
namespace Content.Shared.Drugs; namespace Content.Shared.Drugs;
/// <summary> /// <summary>
/// Exists for use as a status effect. Adds a shader to the client that scales with the effect duration. /// Adds a shader to the client that scales with the effect duration.
/// Use only in conjunction with <see cref="StatusEffectComponent"/>, on the status effect entity.
/// </summary> /// </summary>
[RegisterComponent, NetworkedComponent] [RegisterComponent, NetworkedComponent]
public sealed partial class SeeingRainbowsComponent : Component { } public sealed partial class SeeingRainbowsStatusEffectComponent : Component;

View File

@@ -269,7 +269,7 @@ public abstract partial class SharedStatusEffectsSystem
foreach (var effect in container.ActiveStatusEffects) foreach (var effect in container.ActiveStatusEffects)
{ {
if (!TryComp<StatusEffectComponent>(effect, out var statusComp)) if (!_effectQuery.TryComp(effect, out var statusComp))
continue; continue;
if (TryComp<T>(effect, out var comp)) if (TryComp<T>(effect, out var comp))
@@ -279,6 +279,39 @@ public abstract partial class SharedStatusEffectsSystem
} }
} }
return effects != null; return effects is not null;
}
/// <summary>
/// Helper function for calculating how long it takes for all effects with a particular component to disappear. Useful for overlays.
/// </summary>
/// <param name="target">An entity from which status effects are checked.</param>
/// <param name="endTime">The farthest end time of effects with this component is returned. Can be null if one of the effects is infinite.</param>
/// <returns>True if effects with the specified component were found, or False if there are no such effects.</returns>
public bool TryGetEffectsEndTimeWithComp<T>(EntityUid? target, out TimeSpan? endTime) where T : IComponent
{
endTime = _timing.CurTime;
if (!_containerQuery.TryComp(target, out var container))
return false;
foreach (var effect in container.ActiveStatusEffects)
{
if (!HasComp<T>(effect))
continue;
if (!_effectQuery.TryComp(effect, out var statusComp))
continue;
if (statusComp.EndEffectTime is null)
{
endTime = null;
return true; //This effect never ends, so we return null at endTime, but return true that there is time.
}
if (statusComp.EndEffectTime > endTime)
endTime = statusComp.EndEffectTime;
}
return endTime is not null;
} }
} }

View File

@@ -123,7 +123,6 @@
- KnockedDown - KnockedDown
- SlowedDown - SlowedDown
- Stutter - Stutter
- SeeingRainbows
- Electrocution - Electrocution
- Drunk - Drunk
- SlurredSpeech - SlurredSpeech

View File

@@ -42,3 +42,11 @@
name: drowsiness name: drowsiness
components: components:
- type: DrowsinessStatusEffect - type: DrowsinessStatusEffect
# Adds drugs overlay
- type: entity
parent: MobStatusEffectBase
id: StatusEffectSeeingRainbow
name: hallucinations
components:
- type: SeeingRainbowsStatusEffect

View File

@@ -27,12 +27,11 @@
- !type:AdjustReagent - !type:AdjustReagent
reagent: Ethanol reagent: Ethanol
amount: 0.05 amount: 0.05
- !type:GenericStatusEffect - !type:ModifyStatusEffect
effectProto: StatusEffectSeeingRainbow
conditions: conditions:
- !type:ReagentThreshold - !type:ReagentThreshold
min: 10 min: 10
key: SeeingRainbows
component: SeeingRainbows
type: Add type: Add
time: 5 time: 5
refresh: false refresh: false

View File

@@ -107,11 +107,10 @@
metabolisms: metabolisms:
Narcotic: Narcotic:
effects: effects:
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add
time: 5 time: 5
type: Add
refresh: false refresh: false
Drink: Drink:
effects: effects:

View File

@@ -402,9 +402,8 @@
damage: damage:
types: types:
Cellular: 1 Cellular: 1
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 15 time: 15
refresh: false refresh: false
@@ -439,13 +438,12 @@
damage: damage:
types: types:
Cellular: 0.5 Cellular: 0.5
- !type:GenericStatusEffect - !type:ModifyStatusEffect
conditions: conditions:
- !type:ReagentThreshold - !type:ReagentThreshold
reagent: Frezon reagent: Frezon
min: 1 min: 1
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 500 time: 500
refresh: false refresh: false

View File

@@ -749,8 +749,8 @@
key: KnockedDown key: KnockedDown
time: 3.0 time: 3.0
type: Remove type: Remove
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
time: 15.0 time: 15.0
type: Remove type: Remove
@@ -1323,12 +1323,11 @@
damage: damage:
types: types:
Poison: 2 Poison: 2
- !type:GenericStatusEffect - !type:ModifyStatusEffect
effectProto: StatusEffectSeeingRainbow
conditions: conditions:
- !type:ReagentThreshold - !type:ReagentThreshold
min: 30 min: 30
key: SeeingRainbows
component: SeeingRainbows
type: Add type: Add
time: 8 time: 8
refresh: false refresh: false
@@ -1398,8 +1397,8 @@
key: Jitter key: Jitter
time: 4.0 time: 4.0
type: Remove type: Remove
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
time: 10.0 time: 10.0
type: Remove type: Remove
- !type:AdjustReagent - !type:AdjustReagent

View File

@@ -198,11 +198,10 @@
metabolisms: metabolisms:
Narcotic: Narcotic:
effects: effects:
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add
time: 16 time: 16
type: Add
refresh: false refresh: false
- type: reagent - type: reagent
@@ -236,11 +235,10 @@
damage: damage:
types: types:
Poison: 2 Poison: 2
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add
time: 10 time: 10
type: Add
refresh: false refresh: false
- !type:ChemVomit # Vomiting is a symptom of brain damage - !type:ChemVomit # Vomiting is a symptom of brain damage
probability: 0.05 probability: 0.05
@@ -258,9 +256,8 @@
metabolisms: metabolisms:
Narcotic: Narcotic:
effects: effects:
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 5 time: 5
refresh: false refresh: false
@@ -276,9 +273,8 @@
metabolisms: metabolisms:
Narcotic: Narcotic:
effects: effects:
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 5 time: 5
refresh: false refresh: false
@@ -467,9 +463,8 @@
conditions: conditions:
- !type:ReagentThreshold - !type:ReagentThreshold
max: 20 max: 20
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 5 time: 5
refresh: false refresh: false

View File

@@ -339,9 +339,8 @@
metabolisms: metabolisms:
Poison: Poison:
effects: effects:
- !type:GenericStatusEffect - !type:ModifyStatusEffect
key: SeeingRainbows effectProto: StatusEffectSeeingRainbow
component: SeeingRainbows
type: Add type: Add
time: 10 time: 10
refresh: false refresh: false

View File

@@ -25,9 +25,6 @@
- type: statusEffect - type: statusEffect
id: AllCaps id: AllCaps
- type: statusEffect
id: SeeingRainbows
- type: statusEffect - type: statusEffect
id: Electrocution id: Electrocution