Files
tbd-station-14/Content.Server/GameObjects/Components/Destructible/Thresholds/Threshold.cs
DrSmugleaf b62cc84e8c Add destructible component threshold triggers (#3080)
* WIP changes

* Fix tests, merge conflict and trigger once behavior

* Update yml

* Change test strings to be consts

* Fix total damage types and classes triggers

* Simplify damage trigger logic, move state to Threshold

* Update outdated code and docs

* Change the name of IBehavior back to IThresholdBehavior

* Change human gibbing to trigger at 400 brute damage

* Change gibbing from brute to blunt damage

* Fix one (1) typo

* Add damage class trigger test

* Add missing nullable enable to thresholds
2021-02-05 13:41:05 +01:00

105 lines
3.6 KiB
C#

#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<IThresholdBehavior> _behaviors = new();
/// <summary>
/// Whether or not this threshold was triggered in the previous call to
/// <see cref="Reached"/>.
/// </summary>
[ViewVariables] public bool OldTriggered { get; private set; }
/// <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>
/// The trigger that decides if this threshold has been reached.
/// </summary>
[ViewVariables] public IThresholdTrigger? Trigger { 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(this, x => x.Trigger, "trigger", null);
serializer.DataField(ref _behaviors, "behaviors", new List<IThresholdBehavior>());
}
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;
}
/// <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 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);
}
}
}
}