2021-09-15 03:07:37 +10:00
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Destructible.Thresholds.Behaviors;
|
|
|
|
|
using Content.Server.Destructible.Thresholds.Triggers;
|
2021-09-15 03:07:37 +10:00
|
|
|
using Content.Shared.Damage;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-12-23 13:34:57 +01:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Destructible.Thresholds
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataDefinition]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class DamageThreshold
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("behaviors")]
|
2021-01-02 20:06:00 +01:00
|
|
|
private List<IThresholdBehavior> _behaviors = new();
|
|
|
|
|
|
2021-02-05 13:41:05 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Whether or not this threshold was triggered in the previous call to
|
|
|
|
|
/// <see cref="Reached"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables] public bool OldTriggered { get; private set; }
|
|
|
|
|
|
2020-12-23 13:34:57 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Whether or not this threshold has already been triggered.
|
|
|
|
|
/// </summary>
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
|
|
|
|
[DataField("triggered")]
|
|
|
|
|
public bool Triggered { get; private set; }
|
2020-12-23 13:34:57 +01:00
|
|
|
|
|
|
|
|
/// <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>
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
|
|
|
|
[DataField("triggersOnce")]
|
|
|
|
|
public bool TriggersOnce { get; set; }
|
2020-12-23 13:34:57 +01:00
|
|
|
|
2021-02-05 13:41:05 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// The trigger that decides if this threshold has been reached.
|
|
|
|
|
/// </summary>
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
|
|
|
|
[DataField("trigger")]
|
|
|
|
|
public IThresholdTrigger? Trigger { get; set; }
|
2021-02-05 13:41:05 +01:00
|
|
|
|
2020-12-23 13:34:57 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Behaviors to activate once this threshold is triggered.
|
|
|
|
|
/// </summary>
|
2021-01-02 20:06:00 +01:00
|
|
|
[ViewVariables] public IReadOnlyList<IThresholdBehavior> Behaviors => _behaviors;
|
2020-12-23 13:34:57 +01:00
|
|
|
|
2021-09-15 03:07:37 +10:00
|
|
|
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
|
2021-02-05 13:41:05 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 13:34:57 +01:00
|
|
|
/// <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>
|
2021-11-09 12:06:00 +01:00
|
|
|
public void Execute(EntityUid owner, DestructibleSystem system, IEntityManager entityManager)
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
|
|
|
|
Triggered = true;
|
|
|
|
|
|
|
|
|
|
foreach (var behavior in Behaviors)
|
|
|
|
|
{
|
2021-01-02 20:06:00 +01:00
|
|
|
// The owner has been deleted. We stop execution of behaviors here.
|
2021-11-09 12:06:00 +01:00
|
|
|
if (!entityManager.EntityExists(owner))
|
2021-01-02 20:06:00 +01:00
|
|
|
return;
|
|
|
|
|
|
2021-11-09 21:24:35 +01:00
|
|
|
behavior.Execute(owner, system);
|
2020-12-23 13:34:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|