DamageThreshold.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Content.Server.Destructible.Thresholds.Behaviors;
  2. using Content.Server.Destructible.Thresholds.Triggers;
  3. using Content.Shared.Damage;
  4. namespace Content.Server.Destructible.Thresholds
  5. {
  6. [DataDefinition]
  7. public sealed partial class DamageThreshold
  8. {
  9. [DataField("behaviors")]
  10. private List<IThresholdBehavior> _behaviors = new();
  11. /// <summary>
  12. /// Whether or not this threshold was triggered in the previous call to
  13. /// <see cref="Reached"/>.
  14. /// </summary>
  15. [ViewVariables] public bool OldTriggered { get; private set; }
  16. /// <summary>
  17. /// Whether or not this threshold has already been triggered.
  18. /// </summary>
  19. [DataField("triggered")]
  20. public bool Triggered { get; private set; }
  21. /// <summary>
  22. /// Whether or not this threshold only triggers once.
  23. /// If false, it will trigger again once the entity is healed
  24. /// and then damaged to reach this threshold once again.
  25. /// It will not repeatedly trigger as damage rises beyond that.
  26. /// </summary>
  27. [DataField("triggersOnce")]
  28. public bool TriggersOnce { get; set; }
  29. /// <summary>
  30. /// The trigger that decides if this threshold has been reached.
  31. /// </summary>
  32. [DataField("trigger")]
  33. public IThresholdTrigger? Trigger { get; set; }
  34. /// <summary>
  35. /// Behaviors to activate once this threshold is triggered.
  36. /// </summary>
  37. [ViewVariables] public IReadOnlyList<IThresholdBehavior> Behaviors => _behaviors;
  38. public bool Reached(DamageableComponent damageable, DestructibleSystem system)
  39. {
  40. if (Trigger == null)
  41. {
  42. return false;
  43. }
  44. if (Triggered && TriggersOnce)
  45. {
  46. return false;
  47. }
  48. if (OldTriggered)
  49. {
  50. OldTriggered = Trigger.Reached(damageable, system);
  51. return false;
  52. }
  53. if (!Trigger.Reached(damageable, system))
  54. {
  55. return false;
  56. }
  57. OldTriggered = true;
  58. return true;
  59. }
  60. /// <summary>
  61. /// Triggers this threshold.
  62. /// </summary>
  63. /// <param name="owner">The entity that owns this threshold.</param>
  64. /// <param name="system">
  65. /// An instance of <see cref="DestructibleSystem"/> to get dependency and
  66. /// system references from, if relevant.
  67. /// </param>
  68. /// <param name="entityManager"></param>
  69. /// <param name="cause"></param>
  70. public void Execute(EntityUid owner, DestructibleSystem system, IEntityManager entityManager, EntityUid? cause)
  71. {
  72. Triggered = true;
  73. foreach (var behavior in Behaviors)
  74. {
  75. // The owner has been deleted. We stop execution of behaviors here.
  76. if (!entityManager.EntityExists(owner))
  77. return;
  78. behavior.Execute(owner, system, cause);
  79. }
  80. }
  81. }
  82. }