DamageOnInteractSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Content.Shared.Administration.Logs;
  2. using Content.Shared.Damage.Components;
  3. using Content.Shared.Database;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Inventory;
  6. using Content.Shared.Popups;
  7. using Robust.Shared.Audio.Systems;
  8. using Robust.Shared.Network;
  9. using Robust.Shared.Timing;
  10. namespace Content.Shared.Damage.Systems;
  11. public sealed class DamageOnInteractSystem : EntitySystem
  12. {
  13. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  14. [Dependency] private readonly DamageableSystem _damageableSystem = default!;
  15. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  16. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  17. [Dependency] private readonly InventorySystem _inventorySystem = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<DamageOnInteractComponent, InteractHandEvent>(OnHandInteract);
  22. }
  23. /// <summary>
  24. /// Damages the user that interacts with the entity with an empty hand and
  25. /// plays a sound or pops up text in response. If the user does not have
  26. /// proper protection, the user will only be damaged and other interactions
  27. /// will be cancelled.
  28. /// </summary>
  29. /// <param name="entity">The entity being interacted with</param>
  30. /// <param name="args">Contains the user that interacted with the entity</param>
  31. private void OnHandInteract(Entity<DamageOnInteractComponent> entity, ref InteractHandEvent args)
  32. {
  33. if (!entity.Comp.IsDamageActive)
  34. return;
  35. var totalDamage = entity.Comp.Damage;
  36. if (!entity.Comp.IgnoreResistances)
  37. {
  38. // try to get damage on interact protection from either the inventory slots of the entity
  39. _inventorySystem.TryGetInventoryEntity<DamageOnInteractProtectionComponent>(args.User, out var protectiveEntity);
  40. // or checking the entity for the comp itself if the inventory didn't work
  41. if (protectiveEntity.Comp == null && TryComp<DamageOnInteractProtectionComponent>(args.User, out var protectiveComp))
  42. {
  43. protectiveEntity = (args.User, protectiveComp);
  44. }
  45. // if protectiveComp isn't null after all that, it means the user has protection,
  46. // so let's calculate how much they resist
  47. if (protectiveEntity.Comp != null)
  48. {
  49. totalDamage = DamageSpecifier.ApplyModifierSet(totalDamage, protectiveEntity.Comp.DamageProtection);
  50. }
  51. }
  52. totalDamage = _damageableSystem.TryChangeDamage(args.User, totalDamage, origin: args.Target);
  53. if (totalDamage != null && totalDamage.AnyPositive())
  54. {
  55. args.Handled = true;
  56. _adminLogger.Add(LogType.Damaged, $"{ToPrettyString(args.User):user} injured their hand by interacting with {ToPrettyString(args.Target):target} and received {totalDamage.GetTotal():damage} damage");
  57. _audioSystem.PlayPredicted(entity.Comp.InteractSound, args.Target, args.User);
  58. if (entity.Comp.PopupText != null)
  59. _popupSystem.PopupClient(Loc.GetString(entity.Comp.PopupText), args.User, args.User);
  60. }
  61. }
  62. public void SetIsDamageActiveTo(Entity<DamageOnInteractComponent> entity, bool mode)
  63. {
  64. if (entity.Comp.IsDamageActive == mode)
  65. return;
  66. entity.Comp.IsDamageActive = mode;
  67. Dirty(entity);
  68. }
  69. }