Blindness refactor (#15705)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Leon Friedrich
2023-04-29 17:32:14 +12:00
committed by GitHub
parent e0b809b62d
commit 84299cae63
34 changed files with 460 additions and 456 deletions

View File

@@ -1,56 +0,0 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Eye.Blinding
{
[RegisterComponent]
[NetworkedComponent]
public sealed class BlindableComponent : Component
{
/// <description>
/// How many sources of blindness are affecting us?
/// </description>
[DataField("sources")]
public int Sources = 0;
/// <summary>
/// How many seconds will be subtracted from each attempt to add blindness to us?
/// </summary>
[DataField("blindResistance")]
public float BlindResistance = 0;
/// <summary>
/// Replace with actual eye damage after bobby I guess
/// </summary>
[ViewVariables]
public int EyeDamage = 0;
/// <summary>
/// Whether eye damage has accumulated enough to blind them.
/// </summary>
[ViewVariables]
public bool EyeTooDamaged = false;
/// <description>
/// Used to ensure that this doesn't break with sandbox or admin tools.
/// This is not "enabled/disabled".
/// </description>
public bool LightSetup = false;
/// <description>
/// Gives an extra frame of blindness to reenable light manager during
/// </description>
public bool GraceFrame = false;
}
[Serializable, NetSerializable]
public sealed class BlindableComponentState : ComponentState
{
public readonly int Sources;
public BlindableComponentState(int sources)
{
Sources = sources;
}
}
}

View File

@@ -1,12 +0,0 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding
{
[RegisterComponent]
[NetworkedComponent]
public sealed class BlindfoldComponent : Component
{
[ViewVariables]
public bool IsActive = false;
}
}

View File

@@ -1,14 +0,0 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding
{
[RegisterComponent]
[NetworkedComponent]
public sealed class BlurryVisionComponent : Component
{
[DataField("mangitude")]
public float Magnitude = 1f;
public bool Active => Magnitude < 10f;
}
}

View File

@@ -0,0 +1,41 @@
using Content.Shared.Eye.Blinding.Systems;
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
[RegisterComponent]
[NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(BlindableSystem))]
public sealed partial class BlindableComponent : Component
{
/// <summary>
/// How many seconds will be subtracted from each attempt to add blindness to us?
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("isBlind"), AutoNetworkedField]
public bool IsBlind;
/// <summary>
/// Eye damage due to things like staring directly at welders. Causes blurry vision or outright
/// blindness if greater than or equal to <see cref="MaxDamage"/>.
/// </summary>
/// <remarks>
/// Should eventually be replaced with a proper eye health system when we have bobby.
/// </remarks>
[ViewVariables(VVAccess.ReadWrite), DataField("EyeDamage"), AutoNetworkedField]
public int EyeDamage = 0;
public const int MaxDamage = 8;
/// <description>
/// Used to ensure that this doesn't break with sandbox or admin tools.
/// This is not "enabled/disabled".
/// </description>
[Access(Other = AccessPermissions.ReadWriteExecute)]
public bool LightSetup = false;
/// <description>
/// Gives an extra frame of blindness to reenable light manager during
/// </description>
[Access(Other = AccessPermissions.ReadWriteExecute)]
public bool GraceFrame = false;
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// Blinds a person when an item with this component is equipped to the eye, head, or mask slot.
/// </summary>
[RegisterComponent]
[NetworkedComponent]
public sealed class BlindfoldComponent : Component
{
}

View File

@@ -0,0 +1,21 @@
using Content.Shared.Eye.Blinding.Systems;
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// This component adds a white overlay to the viewport. It does not actually cause blurring.
/// </summary>
[RegisterComponent]
[NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(BlurryVisionSystem))]
public sealed partial class BlurryVisionComponent : Component
{
/// <summary>
/// Amount of "blurring". Also modifies examine ranges.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("magnitude"), AutoNetworkedField]
public float Magnitude;
public const float MaxMagnitude = 10;
}

View File

@@ -0,0 +1,15 @@
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// For welding masks, sunglasses, etc.
/// </summary>
[RegisterComponent]
public sealed class EyeProtectionComponent : Component
{
/// <summary>
/// How many seconds to subtract from the status effect. If it's greater than the source
/// of blindness, do not blind.
/// </summary>
[DataField("protectionTime")]
public readonly TimeSpan ProtectionTime = TimeSpan.FromSeconds(10);
}

View File

@@ -0,0 +1,22 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// For tools like welders that will damage your eyes when you use them.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class RequiresEyeProtectionComponent : Component
{
/// <summary>
/// How long to apply temporary blindness to the user.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("statusEffectTime"), AutoNetworkedField]
public TimeSpan StatusEffectTime = TimeSpan.FromSeconds(10);
/// <summary>
/// You probably want to turn this on in yaml if it's something always on and not a welder.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("toggled"), AutoNetworkedField]
public bool Toggled;
}

View File

@@ -0,0 +1,11 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// Component used for the blind status effect.
/// </summary>
[NetworkedComponent, RegisterComponent]
public sealed class TemporaryBlindnessComponent : Component
{
}

View File

@@ -0,0 +1,14 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding.Components;
/// <summary>
/// This component allows equipment to offset blurry vision.
/// </summary>
[RegisterComponent]
[NetworkedComponent, AutoGenerateComponentState]
public sealed partial class VisionCorrectionComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField("visionBonus"), AutoNetworkedField]
public float VisionBonus = 3f;
}

View File

@@ -1,18 +0,0 @@
namespace Content.Shared.Eye.Blinding.EyeProtection
{
/// <summary>
/// For welding masks, sunglasses, etc.
/// </summary>
[RegisterComponent]
public sealed class EyeProtectionComponent : Component
{
/// <summary>
/// How many seconds to subtract from the status effect. If it's greater than the source
/// of blindness, do not blind.
/// </summary>
[DataField("protectionTime")]
public TimeSpan ProtectionTime = TimeSpan.FromSeconds(10);
public bool IsActive = false;
}
}

View File

@@ -1,21 +0,0 @@
namespace Content.Shared.Eye.Blinding.EyeProtection
{
/// <summary>
/// For tools like welders that will damage your eyes when you use them.
/// </summary>
[RegisterComponent]
public sealed class RequiresEyeProtectionComponent : Component
{
/// <summary>
/// How long to apply temporary blindness to the user.
/// </summary>
[DataField("statusEffectTime")]
public TimeSpan StatusEffectTime = TimeSpan.FromSeconds(10);
/// <summary>
/// You probably want to turn this on in yaml if it's something always on and not a welder.
/// </summary>
[DataField("toggled")]
public bool Toggled = false;
}
}

View File

@@ -1,198 +0,0 @@
using Content.Shared.Clothing.Components;
using Content.Shared.Inventory.Events;
using Content.Shared.Inventory;
using Content.Shared.Rejuvenate;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using JetBrains.Annotations;
namespace Content.Shared.Eye.Blinding
{
public sealed class SharedBlindingSystem : EntitySystem
{
public const string BlindingStatusEffect = "TemporaryBlindness";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlindableComponent, ComponentGetState>(OnGetBlindableState);
SubscribeLocalEvent<BlindableComponent, ComponentHandleState>(OnHandleBlindableState);
SubscribeLocalEvent<BlindfoldComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<BlindfoldComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<VisionCorrectionComponent, GotEquippedEvent>(OnGlassesEquipped);
SubscribeLocalEvent<VisionCorrectionComponent, GotUnequippedEvent>(OnGlassesUnequipped);
SubscribeLocalEvent<BlurryVisionComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<TemporaryBlindnessComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<TemporaryBlindnessComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<BlindableComponent, RejuvenateEvent>(OnRejuvenate);
}
private void OnGetBlindableState(EntityUid uid, BlindableComponent component, ref ComponentGetState args)
{
args.State = new BlindableComponentState(component.Sources);
}
private void OnHandleBlindableState(EntityUid uid, BlindableComponent component, ref ComponentHandleState args)
{
if (args.Current is not BlindableComponentState cast)
return;
component.Sources = cast.Sources;
}
private void OnEquipped(EntityUid uid, BlindfoldComponent component, GotEquippedEvent args)
{
if (!TryComp<ClothingComponent>(uid, out var clothing) || clothing.Slots == SlotFlags.PREVENTEQUIP) // we live in a society
return;
// Is the clothing in its actual slot?
if (!clothing.Slots.HasFlag(args.SlotFlags))
return;
component.IsActive = true;
if (!TryComp<BlindableComponent>(args.Equipee, out var blindComp))
return;
AdjustBlindSources(args.Equipee, 1, blindComp);
}
private void OnUnequipped(EntityUid uid, BlindfoldComponent component, GotUnequippedEvent args)
{
if (!component.IsActive)
return;
component.IsActive = false;
if (!TryComp<BlindableComponent>(args.Equipee, out var blindComp))
return;
AdjustBlindSources(args.Equipee, -1, blindComp);
}
private void OnGlassesEquipped(EntityUid uid, VisionCorrectionComponent component, GotEquippedEvent args)
{
if (!TryComp<ClothingComponent>(uid, out var clothing) || clothing.Slots == SlotFlags.PREVENTEQUIP) // we live in a society
return;
// Is the clothing in its actual slot?
if (!clothing.Slots.HasFlag(args.SlotFlags))
return;
if (!TryComp<BlurryVisionComponent>(args.Equipee, out var blur))
return;
component.IsActive = true;
blur.Magnitude += component.VisionBonus;
Dirty(blur);
}
private void OnGlassesUnequipped(EntityUid uid, VisionCorrectionComponent component, GotUnequippedEvent args)
{
if (!component.IsActive || !TryComp<BlurryVisionComponent>(args.Equipee, out var blur))
return;
component.IsActive = false;
blur.Magnitude -= component.VisionBonus;
Dirty(blur);
}
private void OnGetState(EntityUid uid, BlurryVisionComponent component, ref ComponentGetState args)
{
args.State = new BlurryVisionComponentState(component.Magnitude);
}
private void OnInit(EntityUid uid, TemporaryBlindnessComponent component, ComponentInit args)
{
AdjustBlindSources(uid, 1);
}
private void OnShutdown(EntityUid uid, TemporaryBlindnessComponent component, ComponentShutdown args)
{
AdjustBlindSources(uid, -1);
}
private void OnRejuvenate(EntityUid uid, BlindableComponent component, RejuvenateEvent args)
{
AdjustEyeDamage(uid, -component.EyeDamage, component);
}
[PublicAPI]
public void AdjustBlindSources(EntityUid uid, int amount, BlindableComponent? blindable = null)
{
if (!Resolve(uid, ref blindable, false))
return;
var oldSources = blindable.Sources;
blindable.Sources += amount;
blindable.Sources = Math.Max(blindable.Sources, 0);
if (oldSources == 0 && blindable.Sources > 0)
{
var ev = new BlindnessChangedEvent(true);
RaiseLocalEvent(uid, ev, false);
}
else if (blindable.Sources == 0 && oldSources > 0)
{
var ev = new BlindnessChangedEvent(false);
RaiseLocalEvent(uid, ev, false);
}
Dirty(blindable);
}
public void AdjustEyeDamage(EntityUid uid, int amount, BlindableComponent? blindable = null)
{
if (!Resolve(uid, ref blindable, false))
return;
blindable.EyeDamage += amount;
if (blindable.EyeDamage > 0)
{
var blurry = EnsureComp<BlurryVisionComponent>(uid);
blurry.Magnitude = (9 - blindable.EyeDamage);
Dirty(blurry);
}
else
{
RemComp<BlurryVisionComponent>(uid);
}
if (!blindable.EyeTooDamaged && blindable.EyeDamage >= 8)
{
blindable.EyeTooDamaged = true;
AdjustBlindSources(uid, 1, blindable);
}
if (blindable.EyeTooDamaged && blindable.EyeDamage < 8)
{
blindable.EyeTooDamaged = false;
AdjustBlindSources(uid, -1, blindable);
}
blindable.EyeDamage = Math.Clamp(blindable.EyeDamage, 0, 8);
}
}
// I have no idea why blurry vision needs this but blindness doesn't
[Serializable, NetSerializable]
public sealed class BlurryVisionComponentState : ComponentState
{
public float Magnitude;
public BlurryVisionComponentState(float magnitude)
{
Magnitude = magnitude;
}
}
/// <summary>
/// You became blind or lost blindess, not just changed # of sources.
/// </summary>
public sealed class BlindnessChangedEvent : EntityEventArgs
{
public bool Blind;
public BlindnessChangedEvent(bool blind)
{
Blind = blind;
}
}
}

View File

@@ -0,0 +1,93 @@
using Content.Shared.Eye.Blinding.Components;
using Content.Shared.Inventory;
using Content.Shared.Rejuvenate;
using JetBrains.Annotations;
namespace Content.Shared.Eye.Blinding.Systems;
public sealed class BlindableSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlindableComponent, RejuvenateEvent>(OnRejuvenate);
}
private void OnRejuvenate(EntityUid uid, BlindableComponent component, RejuvenateEvent args)
{
AdjustEyeDamage(uid, -component.EyeDamage, component);
}
[PublicAPI]
public void UpdateIsBlind(EntityUid uid, BlindableComponent? blindable = null)
{
if (!Resolve(uid, ref blindable, false))
return;
var old = blindable.IsBlind;
// Don't bother raising an event if the eye is too damaged.
if (blindable.EyeDamage >= BlindableComponent.MaxDamage)
{
blindable.IsBlind = true;
}
else
{
var ev = new CanSeeAttemptEvent();
RaiseLocalEvent(uid, ev);
blindable.IsBlind = ev.Blind;
}
if (old == blindable.IsBlind)
return;
var changeEv = new BlindnessChangedEvent(blindable.IsBlind);
RaiseLocalEvent(uid, ref changeEv);
Dirty(blindable);
}
public void AdjustEyeDamage(EntityUid uid, int amount, BlindableComponent? blindable = null)
{
if (!Resolve(uid, ref blindable, false) || amount == 0)
return;
blindable.EyeDamage += amount;
blindable.EyeDamage = Math.Clamp(blindable.EyeDamage, 0, BlindableComponent.MaxDamage);
Dirty(blindable);
UpdateIsBlind(uid, blindable);
var ev = new EyeDamageChangedEvent(blindable.EyeDamage);
RaiseLocalEvent(uid, ref ev);
}
}
/// <summary>
/// This event is raised when an entity's blindness changes
/// </summary>
[ByRefEvent]
public record struct BlindnessChangedEvent(bool Blind);
/// <summary>
/// This event is raised when an entity's eye damage changes
/// </summary>
[ByRefEvent]
public record struct EyeDamageChangedEvent(int Damage);
/// <summary>
/// Raised directed at an entity to see whether the entity is currently blind or not.
/// </summary>
public sealed class CanSeeAttemptEvent : CancellableEntityEventArgs, IInventoryRelayEvent
{
public bool Blind => Cancelled;
public SlotFlags TargetSlots => SlotFlags.EYES | SlotFlags.MASK | SlotFlags.HEAD;
}
public sealed class GetEyeProtectionEvent : EntityEventArgs, IInventoryRelayEvent
{
/// <summary>
/// Time to subtract from any temporary blindness sources.
/// </summary>
public TimeSpan Protection;
public SlotFlags TargetSlots => SlotFlags.EYES | SlotFlags.MASK | SlotFlags.HEAD;
}

View File

@@ -0,0 +1,34 @@
using Content.Shared.Eye.Blinding.Components;
using Content.Shared.Inventory.Events;
using Content.Shared.Inventory;
namespace Content.Shared.Eye.Blinding.Systems;
public sealed class BlindfoldSystem : EntitySystem
{
[Dependency] private readonly BlindableSystem _blindableSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlindfoldComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<BlindfoldComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<BlindfoldComponent, InventoryRelayedEvent<CanSeeAttemptEvent>>(OnBlindfoldTrySee);
}
private void OnBlindfoldTrySee(EntityUid uid, BlindfoldComponent component, InventoryRelayedEvent<CanSeeAttemptEvent> args)
{
args.Args.Cancel();
}
private void OnEquipped(EntityUid uid, BlindfoldComponent component, GotEquippedEvent args)
{
_blindableSystem.UpdateIsBlind(args.Equipee);
}
private void OnUnequipped(EntityUid uid, BlindfoldComponent component, GotUnequippedEvent args)
{
_blindableSystem.UpdateIsBlind(args.Equipee);
}
}

View File

@@ -0,0 +1,72 @@
using Content.Shared.Eye.Blinding.Components;
using Content.Shared.Inventory.Events;
using Content.Shared.Inventory;
namespace Content.Shared.Eye.Blinding.Systems;
public sealed class BlurryVisionSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlindableComponent, EyeDamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<VisionCorrectionComponent, GotEquippedEvent>(OnGlassesEquipped);
SubscribeLocalEvent<VisionCorrectionComponent, GotUnequippedEvent>(OnGlassesUnequipped);
SubscribeLocalEvent<VisionCorrectionComponent, InventoryRelayedEvent<GetBlurEvent>>(OnGetBlur);
}
private void OnGetBlur(EntityUid uid, VisionCorrectionComponent component, InventoryRelayedEvent<GetBlurEvent> args)
{
args.Args.Blur += component.VisionBonus;
}
private void OnDamageChanged(EntityUid uid, BlindableComponent component, ref EyeDamageChangedEvent args)
{
UpdateBlurMagnitude(uid, component);
}
private void UpdateBlurMagnitude(EntityUid uid, BlindableComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
var ev = new GetBlurEvent(component.EyeDamage);
RaiseLocalEvent(uid, ev);
var blur = Math.Clamp(0, ev.Blur, BlurryVisionComponent.MaxMagnitude);
if (blur <= 0)
{
RemCompDeferred<BlurryVisionComponent>(uid);
return;
}
var blurry = EnsureComp<BlurryVisionComponent>(uid);
blurry.Magnitude = blur;
Dirty(blurry);
}
private void OnGlassesEquipped(EntityUid uid, VisionCorrectionComponent component, GotEquippedEvent args)
{
UpdateBlurMagnitude(uid);
}
private void OnGlassesUnequipped(EntityUid uid, VisionCorrectionComponent component, GotUnequippedEvent args)
{
UpdateBlurMagnitude(uid);
}
}
public sealed class GetBlurEvent : EntityEventArgs, IInventoryRelayEvent
{
public readonly float BaseBlur;
public float Blur;
public GetBlurEvent(float blur)
{
Blur = blur;
BaseBlur = blur;
}
public SlotFlags TargetSlots => SlotFlags.HEAD | SlotFlags.MASK | SlotFlags.EYES;
}

View File

@@ -0,0 +1,35 @@
using Content.Shared.Eye.Blinding.Components;
namespace Content.Shared.Eye.Blinding.Systems;
public sealed class TemporaryBlindnessSystem : EntitySystem
{
public const string BlindingStatusEffect = "TemporaryBlindness";
[Dependency] private readonly BlindableSystem _blindableSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TemporaryBlindnessComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<TemporaryBlindnessComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<TemporaryBlindnessComponent, CanSeeAttemptEvent>(OnBlindTrySee);
}
private void OnStartup(EntityUid uid, TemporaryBlindnessComponent component, ComponentStartup args)
{
_blindableSystem.UpdateIsBlind(uid);
}
private void OnShutdown(EntityUid uid, TemporaryBlindnessComponent component, ComponentShutdown args)
{
_blindableSystem.UpdateIsBlind(uid);
}
private void OnBlindTrySee(EntityUid uid, TemporaryBlindnessComponent component, CanSeeAttemptEvent args)
{
if (component.LifeStage <= ComponentLifeStage.Running)
args.Cancel();
}
}

View File

@@ -1,11 +0,0 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding
{
/// <summary>
/// Blind status effect.
/// </summary>
[NetworkedComponent, RegisterComponent]
public sealed class TemporaryBlindnessComponent : Component
{}
}

View File

@@ -1,15 +0,0 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Eye.Blinding
{
[RegisterComponent]
[NetworkedComponent]
public sealed class VisionCorrectionComponent : Component
{
[ViewVariables]
public bool IsActive = false;
[DataField("visionBonus")]
public float VisionBonus = 3f;
}
}