#nullable enable using System.Collections.Generic; using Content.Server.GameObjects.Components.Destructible.Thresholds.Behaviors; using Content.Server.GameObjects.Components.Destructible.Thresholds.Triggers; using Content.Server.GameObjects.EntitySystems; using Content.Shared.GameObjects.Components.Damage; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Serialization; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; namespace Content.Server.GameObjects.Components.Destructible.Thresholds { public class Threshold : IExposeData { private List _behaviors = new(); /// /// Whether or not this threshold was triggered in the previous call to /// . /// [ViewVariables] public bool OldTriggered { get; private set; } /// /// Whether or not this threshold has already been triggered. /// [ViewVariables] public bool Triggered { get; private set; } /// /// Whether or not this threshold only triggers once. /// If false, it will trigger again once the entity is healed /// and then damaged to reach this threshold once again. /// It will not repeatedly trigger as damage rises beyond that. /// [ViewVariables] public bool TriggersOnce { get; set; } /// /// The trigger that decides if this threshold has been reached. /// [ViewVariables] public IThresholdTrigger? Trigger { get; set; } /// /// Behaviors to activate once this threshold is triggered. /// [ViewVariables] public IReadOnlyList Behaviors => _behaviors; void IExposeData.ExposeData(ObjectSerializer serializer) { serializer.DataField(this, x => x.Triggered, "triggered", false); serializer.DataField(this, x => x.TriggersOnce, "triggersOnce", false); serializer.DataField(this, x => x.Trigger, "trigger", null); serializer.DataField(ref _behaviors, "behaviors", new List()); } public bool Reached(IDamageableComponent damageable, DestructibleSystem system) { if (Trigger == null) { return false; } if (Triggered && TriggersOnce) { return false; } if (OldTriggered) { OldTriggered = Trigger.Reached(damageable, system); return false; } if (!Trigger.Reached(damageable, system)) { return false; } OldTriggered = true; return true; } /// /// Triggers this threshold. /// /// The entity that owns this threshold. /// /// An instance of to get dependency and /// system references from, if relevant. /// public void Execute(IEntity owner, DestructibleSystem system) { Triggered = true; foreach (var behavior in Behaviors) { // The owner has been deleted. We stop execution of behaviors here. if (owner.Deleted) return; behavior.Execute(owner, system); } } } }