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
This commit is contained in:
Leon Friedrich
2021-09-15 03:07:37 +10:00
committed by GitHub
parent 22cc42ff50
commit df584ad446
212 changed files with 2876 additions and 3441 deletions

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Server.Destructible.Thresholds;
using Content.Shared.Damage;
using Robust.Shared.GameObjects;
@@ -14,50 +14,11 @@ namespace Content.Server.Destructible
[RegisterComponent]
public class DestructibleComponent : Component
{
private DestructibleSystem _destructibleSystem = default!;
public override string Name => "Destructible";
[ViewVariables]
[DataField("thresholds")]
private List<Threshold> _thresholds = new();
public List<DamageThreshold> Thresholds = new();
public IReadOnlyList<Threshold> Thresholds => _thresholds;
protected override void Initialize()
{
base.Initialize();
_destructibleSystem = EntitySystem.Get<DestructibleSystem>();
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case DamageChangedMessage msg:
{
if (msg.Damageable.Owner != Owner)
{
break;
}
foreach (var threshold in _thresholds)
{
if (threshold.Reached(msg.Damageable, _destructibleSystem))
{
var thresholdMessage = new DestructibleThresholdReachedMessage(this, threshold);
SendMessage(thresholdMessage);
threshold.Execute(Owner, _destructibleSystem);
}
}
break;
}
}
}
}
}

View File

@@ -1,4 +1,6 @@
using Content.Shared.Acts;
using Content.Server.Destructible.Thresholds;
using Content.Shared.Acts;
using Content.Shared.Damage;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
@@ -13,5 +15,44 @@ namespace Content.Server.Destructible
[Dependency] public readonly IRobustRandom Random = default!;
[Dependency] public readonly AudioSystem AudioSystem = default!;
[Dependency] public readonly ActSystem ActSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DestructibleComponent, DamageChangedEvent>(Execute);
}
/// <summary>
/// Check if any thresholds were reached. if they were, execute them.
/// </summary>
public void Execute(EntityUid uid, DestructibleComponent component, DamageChangedEvent args)
{
foreach (var threshold in component.Thresholds)
{
if (threshold.Reached(args.Damageable, this))
{
RaiseLocalEvent(uid, new DamageThresholdReached(component, threshold));
threshold.Execute(component.Owner, this);
}
}
}
}
// Currently only used for destructible integration tests. Unless other uses are found for this, maybe this should just be removed and the tests redone.
/// <summary>
/// Event raised when a <see cref="DamageThreshold"/> is reached.
/// </summary>
public class DamageThresholdReached : EntityEventArgs
{
public readonly DestructibleComponent Parent;
public readonly DamageThreshold Threshold;
public DamageThresholdReached(DestructibleComponent parent, DamageThreshold threshold)
{
Parent = parent;
Threshold = threshold;
}
}
}

View File

@@ -1,18 +0,0 @@
using Content.Server.Destructible.Thresholds;
using Robust.Shared.GameObjects;
namespace Content.Server.Destructible
{
public class DestructibleThresholdReachedMessage : ComponentMessage
{
public DestructibleThresholdReachedMessage(DestructibleComponent parent, Threshold threshold)
{
Parent = parent;
Threshold = threshold;
}
public DestructibleComponent Parent { get; }
public Threshold Threshold { get; }
}
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Server.Destructible.Thresholds.Behaviors;
using Content.Server.Destructible.Thresholds.Triggers;
using Content.Shared.Damage.Components;
using Content.Shared.Damage;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -9,7 +9,7 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.Destructible.Thresholds
{
[DataDefinition]
public class Threshold
public class DamageThreshold
{
[DataField("behaviors")]
private List<IThresholdBehavior> _behaviors = new();
@@ -49,7 +49,7 @@ namespace Content.Server.Destructible.Thresholds
/// </summary>
[ViewVariables] public IReadOnlyList<IThresholdBehavior> Behaviors => _behaviors;
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
if (Trigger == null)
{

View File

@@ -1,6 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using Content.Shared.Damage.Components;
using Content.Shared.Damage;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Destructible.Thresholds.Triggers
@@ -15,7 +15,7 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataField("triggers")]
public List<IThresholdTrigger> Triggers { get; set; } = new();
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
foreach (var trigger in Triggers)
{

View File

@@ -1,9 +1,8 @@
using System;
using Content.Shared.Damage;
using Content.Shared.Damage.Components;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Prototypes;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Content.Shared.Damage.Prototypes;
namespace Content.Server.Destructible.Thresholds.Triggers
{
@@ -15,12 +14,8 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataDefinition]
public class DamageGroupTrigger : IThresholdTrigger
{
// TODO PROTOTYPE Replace this datafield variable with prototype references, once they are supported.
// While you're at it, maybe also combine damageGroup and damage into a dictionary, and allow it to test a sum
// of damage types?
[DataField("damageGroup", required: true)]
private string _damageGroupID { get; set; } = default!;
public DamageGroupPrototype DamageGroup => IoCManager.Resolve<IPrototypeManager>().Index<DamageGroupPrototype>(_damageGroupID);
[DataField("damageGroup", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<DamageGroupPrototype>))]
public string DamageGroup { get; set; } = default!;
/// <summary>
/// The amount of damage at which this threshold will trigger.
@@ -28,15 +23,9 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataField("damage", required: true)]
public int Damage { get; set; } = default!;
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
if (DamageGroup == null)
{
return false;
}
return damageable.TryGetDamage(DamageGroup, out var damageReceived) &&
damageReceived >= Damage;
return damageable.DamagePerGroup[DamageGroup] >= Damage;
}
}
}

View File

@@ -1,5 +1,5 @@
using System;
using Content.Shared.Damage.Components;
using Content.Shared.Damage;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Destructible.Thresholds.Triggers
@@ -18,7 +18,7 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataField("damage", required: true)]
public int Damage { get; set; } = default!;
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
return damageable.TotalDamage >= Damage;
}

View File

@@ -1,9 +1,8 @@
using System;
using Content.Shared.Damage;
using Content.Shared.Damage.Components;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Prototypes;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Content.Shared.Damage.Prototypes;
namespace Content.Server.Destructible.Thresholds.Triggers
{
@@ -15,24 +14,15 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataDefinition]
public class DamageTypeTrigger : IThresholdTrigger
{
// TODO PROTOTYPE Replace this datafield variable with prototype references, once they are supported.
// While you're at it, maybe also combine damageGroup and damage into a dictionary, and allow it to test a sum
// of damage types?
[DataField("damageType", required:true)]
public string _damageTypeID { get; set; } = default!;
public DamageTypePrototype DamageType => IoCManager.Resolve<IPrototypeManager>().Index<DamageTypePrototype>(_damageTypeID);
[DataField("damageType", required:true, customTypeSerializer: typeof(PrototypeIdSerializer<DamageTypePrototype>))]
public string DamageType { get; set; } = default!;
[DataField("damage", required: true)]
public int Damage { get; set; } = default!;
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
if (DamageType == null)
{
return false;
}
return damageable.TryGetDamage(DamageType, out var damageReceived) &&
return damageable.Damage.DamageDict.TryGetValue(DamageType, out var damageReceived) &&
damageReceived >= Damage;
}
}

View File

@@ -1,4 +1,4 @@
using Content.Shared.Damage.Components;
using Content.Shared.Damage;
namespace Content.Server.Destructible.Thresholds.Triggers
{
@@ -13,6 +13,6 @@ namespace Content.Server.Destructible.Thresholds.Triggers
/// dependencies from, if any.
/// </param>
/// <returns>true if this trigger has been reached, false otherwise.</returns>
bool Reached(IDamageableComponent damageable, DestructibleSystem system);
bool Reached(DamageableComponent damageable, DestructibleSystem system);
}
}

View File

@@ -1,6 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using Content.Shared.Damage.Components;
using Content.Shared.Damage;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Destructible.Thresholds.Triggers
@@ -15,7 +15,7 @@ namespace Content.Server.Destructible.Thresholds.Triggers
[DataField("triggers")]
public List<IThresholdTrigger> Triggers { get; } = new();
public bool Reached(IDamageableComponent damageable, DestructibleSystem system)
public bool Reached(DamageableComponent damageable, DestructibleSystem system)
{
foreach (var trigger in Triggers)
{