Files
tbd-station-14/Content.Server/Destructible/Thresholds/DamageThreshold.cs
Leon Friedrich df584ad446 ECS damageable (#4529)
* ECS and damage Data

* Comments and newlines

* Added Comments

* Make TryChangeDamageEvent immutable

* Remove SetAllDamage event

Use public SetAllDamage function instead

* Undo destructible mistakes

That was some shit code.

* Rename DamageData to DamageSpecifier

And misc small edits

misc

* Cache trigger prototypes.

* Renaming destructible classes & functions

* Revert "Cache trigger prototypes."

This reverts commit 86bae15ba6616884dba75f552dfdfbe2d1fb6586.

* Replace prototypes with prototype IDs.

* Split damage.yml into individual files

* move get/handle component state to system

* Update HealthChange doc

* Make godmode call Dirty() on damageable component

* Add Initialize() to fix damage test

* Make non-static

* uncache resistance set prototype and trim DamageableComponentState

* Remove unnecessary Dirty() calls during initialization

* RemoveTryChangeDamageEvent

* revert Dirty()

* Fix MobState relying on DamageableComponent.Dirty()

* Fix DisposalUnit Tests.

These were previously failing, but because the async was not await-ed, this never raised the exception.

After I fixed MobState component, this exception stopped happening and instead the assertions started being tested & failing

* Disposal test 2: electric boogaloo

* Fix typos/mistakes

also add comments and fix spacing.

* Use Uids instead of IEntity

* fix merge

* Comments, a merge issue, and making some damage ignore resistances

* Extend DamageSpecifier and use it for DamageableComponent

* fix master merge

* Fix Disposal unit test. Again.

Snapgrids were removed in master

* Execute Exectute
2021-09-14 10:07:37 -07:00

102 lines
3.2 KiB
C#

using System.Collections.Generic;
using Content.Server.Destructible.Thresholds.Behaviors;
using Content.Server.Destructible.Thresholds.Triggers;
using Content.Shared.Damage;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Destructible.Thresholds
{
[DataDefinition]
public class DamageThreshold
{
[DataField("behaviors")]
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]
[DataField("triggered")]
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]
[DataField("triggersOnce")]
public bool TriggersOnce { get; set; }
/// <summary>
/// The trigger that decides if this threshold has been reached.
/// </summary>
[ViewVariables]
[DataField("trigger")]
public IThresholdTrigger? Trigger { get; set; }
/// <summary>
/// Behaviors to activate once this threshold is triggered.
/// </summary>
[ViewVariables] public IReadOnlyList<IThresholdBehavior> Behaviors => _behaviors;
public bool Reached(DamageableComponent 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);
}
}
}
}