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

View File

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

View File

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

View File

@@ -3,7 +3,8 @@ using Robust.Shared.GameStates;
namespace Content.Shared.Drugs;
/// <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>
[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)
{
if (!TryComp<StatusEffectComponent>(effect, out var statusComp))
if (!_effectQuery.TryComp(effect, out var statusComp))
continue;
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
- SlowedDown
- Stutter
- SeeingRainbows
- Electrocution
- Drunk
- SlurredSpeech

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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