2020-12-07 14:52:55 +01:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-12-23 13:34:57 +01:00
|
|
|
|
using Content.Server.GameObjects.Components.Destructible.Thresholds;
|
|
|
|
|
|
using Content.Server.GameObjects.EntitySystems;
|
2020-12-07 14:52:55 +01:00
|
|
|
|
using Content.Shared.GameObjects.Components.Damage;
|
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-12-07 14:52:55 +01:00
|
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Destructible
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// When attached to an <see cref="IEntity"/>, allows it to take damage
|
|
|
|
|
|
/// and triggers thresholds when reached.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
|
public class DestructibleComponent : Component
|
|
|
|
|
|
{
|
2020-12-23 13:34:57 +01:00
|
|
|
|
private DestructibleSystem _destructibleSystem = default!;
|
2020-12-07 14:52:55 +01:00
|
|
|
|
|
|
|
|
|
|
public override string Name => "Destructible";
|
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
|
[ViewVariables]
|
|
|
|
|
|
[DataField("thresholds")]
|
|
|
|
|
|
private List<Threshold> _thresholds = new();
|
2020-12-07 14:52:55 +01:00
|
|
|
|
|
2021-02-05 13:41:05 +01:00
|
|
|
|
public IReadOnlyList<Threshold> Thresholds => _thresholds;
|
2020-12-07 14:52:55 +01:00
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
2020-12-23 13:34:57 +01:00
|
|
|
|
_destructibleSystem = EntitySystem.Get<DestructibleSystem>();
|
2020-12-07 14:52:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.HandleMessage(message, component);
|
|
|
|
|
|
|
|
|
|
|
|
switch (message)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DamageChangedMessage msg:
|
|
|
|
|
|
{
|
|
|
|
|
|
if (msg.Damageable.Owner != Owner)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-05 13:41:05 +01:00
|
|
|
|
foreach (var threshold in _thresholds)
|
2020-12-07 14:52:55 +01:00
|
|
|
|
{
|
2021-02-05 13:41:05 +01:00
|
|
|
|
if (threshold.Reached(msg.Damageable, _destructibleSystem))
|
2020-12-07 14:52:55 +01:00
|
|
|
|
{
|
2021-02-05 13:41:05 +01:00
|
|
|
|
var thresholdMessage = new DestructibleThresholdReachedMessage(this, threshold);
|
2020-12-07 14:52:55 +01:00
|
|
|
|
SendMessage(thresholdMessage);
|
|
|
|
|
|
|
2021-02-05 13:41:05 +01:00
|
|
|
|
threshold.Execute(Owner, _destructibleSystem);
|
2020-12-07 14:52:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|