DamageOnAttackedSystem.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Content.Shared.Administration.Logs;
  2. using Content.Shared.Damage.Components;
  3. using Content.Shared.Database;
  4. using Content.Shared.Hands.Components;
  5. using Content.Shared.Hands.EntitySystems;
  6. using Content.Shared.Inventory;
  7. using Content.Shared.Popups;
  8. using Content.Shared.Weapons.Melee.Events;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Network;
  11. using Robust.Shared.Timing;
  12. namespace Content.Shared.Damage.Systems;
  13. public sealed class DamageOnAttackedSystem : EntitySystem
  14. {
  15. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  16. [Dependency] private readonly DamageableSystem _damageableSystem = default!;
  17. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  18. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  19. [Dependency] private readonly InventorySystem _inventorySystem = default!;
  20. [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<DamageOnAttackedComponent, AttackedEvent>(OnAttacked);
  25. }
  26. /// <summary>
  27. /// Damages the user that attacks the entity and potentially
  28. /// plays a sound or pops up text in response
  29. /// </summary>
  30. /// <param name="entity">The entity being hit</param>
  31. /// <param name="args">Contains the user that hit the entity</param>
  32. private void OnAttacked(Entity<DamageOnAttackedComponent> entity, ref AttackedEvent args)
  33. {
  34. if (!entity.Comp.IsDamageActive)
  35. return;
  36. var totalDamage = entity.Comp.Damage;
  37. if (!entity.Comp.IgnoreResistances)
  38. {
  39. // try to get the damage on attacked protection component from something the entity has in their inventory
  40. _inventorySystem.TryGetInventoryEntity<DamageOnAttackedProtectionComponent>(args.User, out var protectiveEntity);
  41. // if comp is null that means the user didn't have anything equipped that protected them
  42. // let's check their hands to see if the thing they attacked with gives them protection, like the GORILLA gauntlet
  43. if (protectiveEntity.Comp == null && TryComp<HandsComponent>(args.User, out var handsComp))
  44. {
  45. if (_handsSystem.TryGetActiveItem((args.User, handsComp), out var itemInHand) &&
  46. TryComp<DamageOnAttackedProtectionComponent>(itemInHand, out var itemProtectComp)
  47. && itemProtectComp.Slots == SlotFlags.NONE)
  48. {
  49. protectiveEntity = (itemInHand.Value, itemProtectComp);
  50. }
  51. }
  52. // if comp is null, that means both the inventory and hands had nothing to protect them
  53. // let's check if the entity itself has the protective comp, like with borgs
  54. if (protectiveEntity.Comp == null &&
  55. TryComp<DamageOnAttackedProtectionComponent>(args.User, out var protectiveComp))
  56. {
  57. protectiveEntity = (args.User, protectiveComp);
  58. }
  59. // if comp is NOT NULL that means they have damage protection!
  60. if (protectiveEntity.Comp != null)
  61. {
  62. totalDamage = DamageSpecifier.ApplyModifierSet(totalDamage, protectiveEntity.Comp.DamageProtection);
  63. }
  64. }
  65. totalDamage = _damageableSystem.TryChangeDamage(args.User, totalDamage, entity.Comp.IgnoreResistances, origin: entity);
  66. if (totalDamage != null && totalDamage.AnyPositive())
  67. {
  68. _adminLogger.Add(LogType.Damaged, $"{ToPrettyString(args.User):user} injured themselves by attacking {ToPrettyString(entity):target} and received {totalDamage.GetTotal():damage} damage");
  69. _audioSystem.PlayPredicted(entity.Comp.InteractSound, entity, args.User);
  70. if (entity.Comp.PopupText != null)
  71. _popupSystem.PopupClient(Loc.GetString(entity.Comp.PopupText), args.User, args.User);
  72. }
  73. }
  74. public void SetIsDamageActiveTo(Entity<DamageOnAttackedComponent> entity, bool mode)
  75. {
  76. if (entity.Comp.IsDamageActive == mode)
  77. return;
  78. entity.Comp.IsDamageActive = mode;
  79. Dirty(entity);
  80. }
  81. }