Files
tbd-station-14/Content.Server/GameObjects/Components/Destructible/Thresholds/Threshold.cs
Vera Aguilera Puerto 85add420b0 Destructible spawning fix redux (#2892)
* Fix SpawnEntitiesBehavior crash and add test

* Fix comparer, add duplicated behavior

Turns out this isn't Java

* Threshold behaviors are now "linearly" executed

* Fixes YAML threshold behaviors to be linear

Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
2021-01-02 20:06:00 +01:00

64 lines
2.4 KiB
C#

#nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Destructible.Thresholds.Behavior;
using Content.Server.GameObjects.EntitySystems;
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<IThresholdBehavior> _behaviors = new();
/// <summary>
/// Whether or not this threshold has already been triggered.
/// </summary>
[ViewVariables] public bool Triggered { get; private set; }
/// <summary>
/// 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.
/// </summary>
[ViewVariables] public bool TriggersOnce { get; set; }
/// <summary>
/// Behaviors to activate once this threshold is triggered.
/// </summary>
[ViewVariables] public IReadOnlyList<IThresholdBehavior> Behaviors => _behaviors;
public void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(this, x => x.Triggered, "triggered", false);
serializer.DataField(this, x => x.TriggersOnce, "triggersOnce", false);
serializer.DataField(ref _behaviors, "behaviors", new List<IThresholdBehavior>());
}
/// <summary>
/// Triggers this threshold.
/// </summary>
/// <param name="owner">The entity that owns this threshold.</param>
/// <param name="system">
/// An instance of <see cref="DestructibleSystem"/> to get dependency and
/// system references from, if relevant.
/// </param>
public void Trigger(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.Trigger(owner, system);
}
}
}
}