1
0

BlockingSystem.User.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Content.Shared.Damage;
  2. using Content.Shared.Damage.Prototypes;
  3. using Robust.Shared.Audio;
  4. using Robust.Shared.Audio.Systems;
  5. using Robust.Shared.Containers;
  6. namespace Content.Shared.Blocking;
  7. public sealed partial class BlockingSystem
  8. {
  9. [Dependency] private readonly DamageableSystem _damageable = default!;
  10. [Dependency] private readonly SharedAudioSystem _audio = default!;
  11. private void InitializeUser()
  12. {
  13. SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
  14. SubscribeLocalEvent<BlockingComponent, DamageModifyEvent>(OnDamageModified);
  15. SubscribeLocalEvent<BlockingUserComponent, EntParentChangedMessage>(OnParentChanged);
  16. SubscribeLocalEvent<BlockingUserComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt);
  17. SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged);
  18. SubscribeLocalEvent<BlockingUserComponent, EntityTerminatingEvent>(OnEntityTerminating);
  19. }
  20. private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args)
  21. {
  22. UserStopBlocking(uid, component);
  23. }
  24. private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args)
  25. {
  26. UserStopBlocking(uid, component);
  27. }
  28. private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args)
  29. {
  30. if (args.Anchored)
  31. return;
  32. UserStopBlocking(uid, component);
  33. }
  34. private void OnUserDamageModified(EntityUid uid, BlockingUserComponent component, DamageModifyEvent args)
  35. {
  36. if (TryComp<BlockingComponent>(component.BlockingItem, out var blocking))
  37. {
  38. if (args.Damage.GetTotal() <= 0)
  39. return;
  40. // A shield should only block damage it can itself absorb. To determine that we need the Damageable component on it.
  41. if (!TryComp<DamageableComponent>(component.BlockingItem, out var dmgComp))
  42. return;
  43. var blockFraction = blocking.IsBlocking ? blocking.ActiveBlockFraction : blocking.PassiveBlockFraction;
  44. blockFraction = Math.Clamp(blockFraction, 0, 1);
  45. _damageable.TryChangeDamage(component.BlockingItem, blockFraction * args.OriginalDamage);
  46. var modify = new DamageModifierSet();
  47. foreach (var key in dmgComp.Damage.DamageDict.Keys)
  48. {
  49. modify.Coefficients.TryAdd(key, 1 - blockFraction);
  50. }
  51. args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, modify);
  52. if (blocking.IsBlocking && !args.Damage.Equals(args.OriginalDamage))
  53. {
  54. _audio.PlayPvs(blocking.BlockSound, uid);
  55. }
  56. }
  57. }
  58. private void OnDamageModified(EntityUid uid, BlockingComponent component, DamageModifyEvent args)
  59. {
  60. var modifier = component.IsBlocking ? component.ActiveBlockDamageModifier : component.PassiveBlockDamageModifer;
  61. if (modifier == null)
  62. {
  63. return;
  64. }
  65. args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, modifier);
  66. }
  67. private void OnEntityTerminating(EntityUid uid, BlockingUserComponent component, ref EntityTerminatingEvent args)
  68. {
  69. if (!TryComp<BlockingComponent>(component.BlockingItem, out var blockingComponent))
  70. return;
  71. StopBlockingHelper(component.BlockingItem.Value, blockingComponent, uid);
  72. }
  73. /// <summary>
  74. /// Check for the shield and has the user stop blocking
  75. /// Used where you'd like the user to stop blocking, but also don't want to remove the <see cref="BlockingUserComponent"/>
  76. /// </summary>
  77. /// <param name="uid">The user blocking</param>
  78. /// <param name="component">The <see cref="BlockingUserComponent"/></param>
  79. private void UserStopBlocking(EntityUid uid, BlockingUserComponent component)
  80. {
  81. if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking)
  82. StopBlocking(component.BlockingItem.Value, blockComp, uid);
  83. }
  84. }