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

@@ -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();
}
}