EyeProtectionSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Content.Shared.StatusEffect;
  2. using Content.Shared.Inventory;
  3. using Content.Shared.Eye.Blinding.Components;
  4. using Content.Shared.Tools.Components;
  5. using Content.Shared.Item.ItemToggle.Components;
  6. namespace Content.Shared.Eye.Blinding.Systems
  7. {
  8. public sealed class EyeProtectionSystem : EntitySystem
  9. {
  10. [Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;
  11. [Dependency] private readonly BlindableSystem _blindingSystem = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<RequiresEyeProtectionComponent, ToolUseAttemptEvent>(OnUseAttempt);
  16. SubscribeLocalEvent<RequiresEyeProtectionComponent, ItemToggledEvent>(OnWelderToggled);
  17. SubscribeLocalEvent<EyeProtectionComponent, GetEyeProtectionEvent>(OnGetProtection);
  18. SubscribeLocalEvent<EyeProtectionComponent, InventoryRelayedEvent<GetEyeProtectionEvent>>(OnGetRelayedProtection);
  19. }
  20. private void OnGetRelayedProtection(EntityUid uid, EyeProtectionComponent component,
  21. InventoryRelayedEvent<GetEyeProtectionEvent> args)
  22. {
  23. OnGetProtection(uid, component, args.Args);
  24. }
  25. private void OnGetProtection(EntityUid uid, EyeProtectionComponent component, GetEyeProtectionEvent args)
  26. {
  27. args.Protection += component.ProtectionTime;
  28. }
  29. private void OnUseAttempt(EntityUid uid, RequiresEyeProtectionComponent component, ToolUseAttemptEvent args)
  30. {
  31. if (!component.Toggled)
  32. return;
  33. if (!TryComp<BlindableComponent>(args.User, out var blindable) || blindable.IsBlind)
  34. return;
  35. var ev = new GetEyeProtectionEvent();
  36. RaiseLocalEvent(args.User, ev);
  37. var time = (float) (component.StatusEffectTime - ev.Protection).TotalSeconds;
  38. if (time <= 0)
  39. return;
  40. // Add permanent eye damage if they had zero protection, also somewhat scale their temporary blindness by
  41. // how much damage they already accumulated.
  42. _blindingSystem.AdjustEyeDamage((args.User, blindable), 1);
  43. var statusTimeSpan = TimeSpan.FromSeconds(time * MathF.Sqrt(blindable.EyeDamage));
  44. _statusEffectsSystem.TryAddStatusEffect(args.User, TemporaryBlindnessSystem.BlindingStatusEffect,
  45. statusTimeSpan, false, TemporaryBlindnessSystem.BlindingStatusEffect);
  46. }
  47. private void OnWelderToggled(EntityUid uid, RequiresEyeProtectionComponent component, ItemToggledEvent args)
  48. {
  49. component.Toggled = args.Activated;
  50. Dirty(uid, component);
  51. }
  52. }
  53. }