ECS and cleanup body system, merge body templates and presets into body prototypes (#11991)
Co-authored-by: Jezithyr <Jezithyr@gmail.com>
This commit is contained in:
36
Content.Shared/Body/Components/BodyComponent.cs
Normal file
36
Content.Shared/Body/Components/BodyComponent.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Body.Prototypes;
|
||||
using Content.Shared.Body.Systems;
|
||||
using Content.Shared.DragDrop;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Body.Components;
|
||||
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
[Access(typeof(SharedBodySystem))]
|
||||
public sealed class BodyComponent : Component, IDraggable
|
||||
{
|
||||
[ViewVariables]
|
||||
[DataField("prototype", customTypeSerializer: typeof(PrototypeIdSerializer<BodyPrototype>))]
|
||||
public readonly string? Prototype;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("root")]
|
||||
public BodyPartSlot Root = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("gibSound")]
|
||||
public SoundSpecifier GibSound = new SoundCollectionSpecifier("gib");
|
||||
|
||||
bool IDraggable.CanStartDrag(StartDragDropEvent args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IDraggable.CanDrop(CanDropEvent args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
18
Content.Shared/Body/Components/BodyComponentState.cs
Normal file
18
Content.Shared/Body/Components/BodyComponentState.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Content.Shared.Body.Part;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Body.Components;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class BodyComponentState : ComponentState
|
||||
{
|
||||
public readonly BodyPartSlot Root;
|
||||
public readonly SoundSpecifier GibSound;
|
||||
|
||||
public BodyComponentState(BodyPartSlot root, SoundSpecifier gibSound)
|
||||
{
|
||||
Root = root;
|
||||
GibSound = gibSound;
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
using Content.Shared.Body.Events;
|
||||
using Content.Shared.Body.Part;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class MechanismComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
private SharedBodyPartComponent? _part;
|
||||
|
||||
public SharedBodyComponent? Body => Part?.Body;
|
||||
|
||||
public SharedBodyPartComponent? Part
|
||||
{
|
||||
get => _part;
|
||||
set
|
||||
{
|
||||
if (_part == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var old = _part;
|
||||
_part = value;
|
||||
|
||||
if (old != null)
|
||||
{
|
||||
if (old.Body == null)
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(Owner, new RemovedFromPartEvent(old), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(Owner, new RemovedFromPartInBodyEvent(old.Body, old), true);
|
||||
}
|
||||
}
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
if (value.Body == null)
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(Owner, new AddedToPartEvent(value), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(Owner, new AddedToPartInBodyEvent(value.Body, value), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DataField("maxDurability")] public int MaxDurability { get; set; } = 10;
|
||||
|
||||
[DataField("currentDurability")] public int CurrentDurability { get; set; } = 10;
|
||||
|
||||
[DataField("destroyThreshold")] public int DestroyThreshold { get; set; } = -10;
|
||||
|
||||
// TODO BODY: Surgery description and adding a message to the examine tooltip of the entity that owns this mechanism
|
||||
// TODO BODY
|
||||
[DataField("resistance")] public int Resistance { get; set; } = 0;
|
||||
|
||||
// TODO BODY OnSizeChanged
|
||||
/// <summary>
|
||||
/// Determines whether this
|
||||
/// <see cref="MechanismComponent"/> can fit into a <see cref="SharedBodyPartComponent"/>.
|
||||
/// </summary>
|
||||
[DataField("size")] public int Size { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// What kind of <see cref="SharedBodyPartComponent"/> this
|
||||
/// <see cref="MechanismComponent"/> can be easily installed into.
|
||||
/// </summary>
|
||||
[DataField("compatibility")]
|
||||
public BodyPartCompatibility Compatibility { get; set; } = BodyPartCompatibility.Universal;
|
||||
}
|
||||
}
|
||||
@@ -1,457 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Body.Prototypes;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Damage.Prototypes;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Body.Components
|
||||
{
|
||||
// TODO BODY Damage methods for collections of IDamageableComponents
|
||||
|
||||
[NetworkedComponent()]
|
||||
public abstract class SharedBodyComponent : Component, ISerializationHooks
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("template", required: true)]
|
||||
private string? TemplateId { get; }
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("preset", required: true)]
|
||||
private string? PresetId { get; }
|
||||
|
||||
[ViewVariables]
|
||||
public BodyTemplatePrototype? Template => TemplateId == null
|
||||
? null
|
||||
: _prototypeManager.Index<BodyTemplatePrototype>(TemplateId);
|
||||
|
||||
[ViewVariables]
|
||||
public BodyPresetPrototype? Preset => PresetId == null
|
||||
? null
|
||||
: _prototypeManager.Index<BodyPresetPrototype>(PresetId);
|
||||
|
||||
[ViewVariables]
|
||||
private Dictionary<string, BodyPartSlot> SlotIds { get; } = new();
|
||||
|
||||
[ViewVariables]
|
||||
private Dictionary<SharedBodyPartComponent, BodyPartSlot> SlotParts { get; } = new();
|
||||
|
||||
[ViewVariables]
|
||||
public IEnumerable<BodyPartSlot> Slots => SlotIds.Values;
|
||||
|
||||
[ViewVariables]
|
||||
public IEnumerable<KeyValuePair<SharedBodyPartComponent, BodyPartSlot>> Parts => SlotParts;
|
||||
|
||||
public BodyPartSlot? CenterSlot =>
|
||||
Template?.CenterSlot is { } centerSlot
|
||||
? SlotIds.GetValueOrDefault(centerSlot)
|
||||
: null;
|
||||
|
||||
public SharedBodyPartComponent? CenterPart => CenterSlot?.Part;
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// TODO BODY BeforeDeserialization
|
||||
// TODO BODY Move to template or somewhere else
|
||||
if (TemplateId != null)
|
||||
{
|
||||
var template = _prototypeManager.Index<BodyTemplatePrototype>(TemplateId);
|
||||
|
||||
foreach (var (id, partType) in template.Slots)
|
||||
{
|
||||
SetSlot(id, partType);
|
||||
}
|
||||
|
||||
foreach (var (slotId, connectionIds) in template.Connections)
|
||||
{
|
||||
var connections = connectionIds.Select(id => SlotIds[id]);
|
||||
SlotIds[slotId].SetConnectionsInternal(connections);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRemove()
|
||||
{
|
||||
foreach (var slot in SlotIds.Values)
|
||||
{
|
||||
slot.Shutdown();
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
private BodyPartSlot SetSlot(string id, BodyPartType type)
|
||||
{
|
||||
var slot = new BodyPartSlot(id, type);
|
||||
|
||||
SlotIds[id] = slot;
|
||||
slot.PartAdded += part => OnAddPart(slot, part);
|
||||
slot.PartRemoved += part => OnRemovePart(slot, part);
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
private Dictionary<BodyPartSlot, SharedBodyPartComponent> GetHangingParts(BodyPartSlot from)
|
||||
{
|
||||
var hanging = new Dictionary<BodyPartSlot, SharedBodyPartComponent>();
|
||||
|
||||
foreach (var connection in from.Connections)
|
||||
{
|
||||
if (connection.Part != null &&
|
||||
!ConnectedToCenter(connection.Part))
|
||||
{
|
||||
hanging.Add(connection, connection.Part);
|
||||
}
|
||||
}
|
||||
|
||||
return hanging;
|
||||
}
|
||||
|
||||
protected virtual bool CanAddPart(string slotId, SharedBodyPartComponent part)
|
||||
{
|
||||
if (!SlotIds.TryGetValue(slotId, out var slot) ||
|
||||
slot.CanAddPart(part))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void OnAddPart(BodyPartSlot slot, SharedBodyPartComponent part)
|
||||
{
|
||||
SlotParts[part] = slot;
|
||||
part.Body = this;
|
||||
|
||||
var argsAdded = new BodyPartAddedEventArgs(slot.Id, part);
|
||||
|
||||
// TODO: Body refactor. Somebody is doing it
|
||||
// EntitySystem.Get<SharedHumanoidAppearanceSystem>().BodyPartAdded(Owner, argsAdded);
|
||||
foreach (var component in IoCManager.Resolve<IEntityManager>().GetComponents<IBodyPartAdded>(Owner).ToArray())
|
||||
{
|
||||
component.BodyPartAdded(argsAdded);
|
||||
}
|
||||
|
||||
// TODO BODY Sort this duplicate out
|
||||
OnBodyChanged();
|
||||
}
|
||||
|
||||
protected virtual void OnRemovePart(BodyPartSlot slot, SharedBodyPartComponent part)
|
||||
{
|
||||
SlotParts.Remove(part);
|
||||
|
||||
foreach (var connectedSlot in slot.Connections)
|
||||
{
|
||||
if (connectedSlot.Part != null &&
|
||||
!ConnectedToCenter(connectedSlot.Part))
|
||||
{
|
||||
RemovePart(connectedSlot.Part);
|
||||
}
|
||||
}
|
||||
|
||||
part.Body = null;
|
||||
|
||||
var args = new BodyPartRemovedEventArgs(slot.Id, part);
|
||||
|
||||
|
||||
// TODO: Body refactor. Somebody is doing it
|
||||
// EntitySystem.Get<SharedHumanoidAppearanceSystem>().BodyPartRemoved(Owner, args);
|
||||
foreach (var component in IoCManager.Resolve<IEntityManager>().GetComponents<IBodyPartRemoved>(Owner))
|
||||
{
|
||||
component.BodyPartRemoved(args);
|
||||
}
|
||||
|
||||
// creadth: fall down if no legs
|
||||
if (part.PartType == BodyPartType.Leg &&
|
||||
GetPartsOfType(BodyPartType.Leg).ToArray().Length == 0)
|
||||
{
|
||||
EntitySystem.Get<StandingStateSystem>().Down(Owner);
|
||||
}
|
||||
|
||||
if (part.IsVital && SlotParts.Count(x => x.Value.PartType == part.PartType) == 0)
|
||||
{
|
||||
// TODO BODY SYSTEM KILL : Find a more elegant way of killing em than just dumping bloodloss damage.
|
||||
var damage = new DamageSpecifier(_prototypeManager.Index<DamageTypePrototype>("Bloodloss"), 300);
|
||||
EntitySystem.Get<DamageableSystem>().TryChangeDamage(part.Owner, damage);
|
||||
}
|
||||
|
||||
OnBodyChanged();
|
||||
}
|
||||
|
||||
// TODO BODY Sensible templates
|
||||
public bool TryAddPart(string slotId, SharedBodyPartComponent part)
|
||||
{
|
||||
DebugTools.AssertNotNull(part);
|
||||
DebugTools.AssertNotNull(slotId);
|
||||
|
||||
if (!CanAddPart(slotId, part))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return SlotIds.TryGetValue(slotId, out var slot) &&
|
||||
slot.TryAddPart(part);
|
||||
}
|
||||
|
||||
public void SetPart(string slotId, SharedBodyPartComponent part)
|
||||
{
|
||||
if (!SlotIds.TryGetValue(slotId, out var slot))
|
||||
{
|
||||
slot = SetSlot(slotId, part.PartType);
|
||||
SlotIds[slotId] = slot;
|
||||
}
|
||||
|
||||
slot.SetPart(part);
|
||||
}
|
||||
|
||||
public bool HasPart(SharedBodyPartComponent part)
|
||||
{
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
return SlotParts.ContainsKey(part);
|
||||
}
|
||||
|
||||
public bool RemovePart(SharedBodyPartComponent part)
|
||||
{
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
return SlotParts.TryGetValue(part, out var slot) &&
|
||||
slot.RemovePart();
|
||||
}
|
||||
|
||||
public bool TryDropPart(BodyPartSlot slot, [NotNullWhen(true)] out Dictionary<BodyPartSlot, SharedBodyPartComponent>? dropped)
|
||||
{
|
||||
DebugTools.AssertNotNull(slot);
|
||||
|
||||
if (!SlotIds.TryGetValue(slot.Id, out var ownedSlot) ||
|
||||
ownedSlot != slot ||
|
||||
slot.Part == null)
|
||||
{
|
||||
dropped = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var oldPart = slot.Part;
|
||||
dropped = GetHangingParts(slot);
|
||||
|
||||
if (!slot.RemovePart())
|
||||
{
|
||||
dropped = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
dropped[slot] = oldPart;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ConnectedToCenter(SharedBodyPartComponent part)
|
||||
{
|
||||
return TryGetSlot(part, out var result) &&
|
||||
ConnectedToCenterPartRecursion(result);
|
||||
}
|
||||
|
||||
private bool ConnectedToCenterPartRecursion(BodyPartSlot slot, HashSet<BodyPartSlot>? searched = null)
|
||||
{
|
||||
searched ??= new HashSet<BodyPartSlot>();
|
||||
|
||||
if (Template?.CenterSlot == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (slot.Part == CenterPart)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
searched.Add(slot);
|
||||
|
||||
foreach (var connection in slot.Connections)
|
||||
{
|
||||
if (!searched.Contains(connection) &&
|
||||
ConnectedToCenterPartRecursion(connection, searched))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public BodyPartSlot? GetSlot(SharedBodyPartComponent part)
|
||||
{
|
||||
return SlotParts.GetValueOrDefault(part);
|
||||
}
|
||||
|
||||
public bool TryGetSlot(SharedBodyPartComponent part, [NotNullWhen(true)] out BodyPartSlot? slot)
|
||||
{
|
||||
return (slot = GetSlot(part)) != null;
|
||||
}
|
||||
|
||||
public IEnumerable<BodyPartSlot> GetSlotsOfType(BodyPartType type)
|
||||
{
|
||||
foreach (var slot in SlotIds.Values)
|
||||
{
|
||||
if (slot.PartType == type)
|
||||
{
|
||||
yield return slot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasPartOfType(BodyPartType type)
|
||||
{
|
||||
foreach (var _ in GetPartsOfType(type))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerable<SharedBodyPartComponent> GetPartsOfType(BodyPartType type)
|
||||
{
|
||||
foreach (var slot in GetSlotsOfType(type))
|
||||
{
|
||||
if (slot.Part != null)
|
||||
{
|
||||
yield return slot.Part;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBodyChanged()
|
||||
{
|
||||
Dirty();
|
||||
}
|
||||
|
||||
// TODO BODY optimize this
|
||||
public BodyPartSlot SlotAt(int index)
|
||||
{
|
||||
return SlotIds.Values.ElementAt(index);
|
||||
}
|
||||
|
||||
public KeyValuePair<SharedBodyPartComponent, BodyPartSlot> PartAt(int index)
|
||||
{
|
||||
return SlotParts.ElementAt(index);
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
var parts = new (string slot, EntityUid partId)[SlotParts.Count];
|
||||
|
||||
var i = 0;
|
||||
foreach (var (part, slot) in SlotParts)
|
||||
{
|
||||
parts[i] = (slot.Id, part.Owner);
|
||||
i++;
|
||||
}
|
||||
|
||||
return new BodyComponentState(parts);
|
||||
}
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
base.HandleComponentState(curState, nextState);
|
||||
|
||||
if (curState is not BodyComponentState state)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newParts = state.Parts();
|
||||
|
||||
foreach (var (oldPart, slot) in SlotParts)
|
||||
{
|
||||
if (!newParts.TryGetValue(slot.Id, out var newPart) ||
|
||||
newPart != oldPart)
|
||||
{
|
||||
RemovePart(oldPart);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (slotId, newPart) in newParts)
|
||||
{
|
||||
if (!SlotIds.TryGetValue(slotId, out var slot) ||
|
||||
slot.Part != newPart)
|
||||
{
|
||||
SetPart(slotId, newPart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual HashSet<EntityUid> Gib(bool gibParts = false)
|
||||
{
|
||||
var entMgr = IoCManager.Resolve<IEntityManager>();
|
||||
var metaQuery = entMgr.GetEntityQuery<MetaDataComponent>();
|
||||
var gibs = new HashSet<EntityUid>();
|
||||
foreach (var part in SlotParts.Keys)
|
||||
{
|
||||
if (!metaQuery.TryGetComponent(part.Owner, out var meta) ||
|
||||
meta.EntityLifeStage >= EntityLifeStage.Terminating)
|
||||
{
|
||||
SlotParts.Remove(part);
|
||||
continue;
|
||||
}
|
||||
gibs.Add(part.Owner);
|
||||
RemovePart(part);
|
||||
|
||||
if (gibParts)
|
||||
gibs.UnionWith(part.Gib());
|
||||
}
|
||||
|
||||
return gibs;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class BodyComponentState : ComponentState
|
||||
{
|
||||
private Dictionary<string, SharedBodyPartComponent>? _parts;
|
||||
|
||||
public readonly (string slot, EntityUid partId)[] PartIds;
|
||||
|
||||
public BodyComponentState((string slot, EntityUid partId)[] partIds)
|
||||
{
|
||||
PartIds = partIds;
|
||||
}
|
||||
|
||||
public Dictionary<string, SharedBodyPartComponent> Parts(IEntityManager? entityManager = null)
|
||||
{
|
||||
if (_parts != null)
|
||||
{
|
||||
return _parts;
|
||||
}
|
||||
|
||||
entityManager ??= IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
var parts = new Dictionary<string, SharedBodyPartComponent>(PartIds.Length);
|
||||
|
||||
foreach (var (slot, partId) in PartIds)
|
||||
{
|
||||
if (!entityManager.EntityExists(partId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entityManager.TryGetComponent(partId, out SharedBodyPartComponent? part))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
parts[slot] = part;
|
||||
}
|
||||
|
||||
return _parts = parts;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,345 +0,0 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Events;
|
||||
using Content.Shared.Body.Part;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Body.Components
|
||||
{
|
||||
[NetworkedComponent()]
|
||||
public abstract class SharedBodyPartComponent : Component
|
||||
{
|
||||
public const string ContainerId = "bodypart";
|
||||
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
private SharedBodyComponent? _body;
|
||||
|
||||
// TODO BODY Remove
|
||||
[DataField("mechanisms")]
|
||||
private readonly List<string> _mechanismIds = new();
|
||||
public IReadOnlyList<string> MechanismIds => _mechanismIds;
|
||||
|
||||
[ViewVariables]
|
||||
private readonly HashSet<MechanismComponent> _mechanisms = new();
|
||||
|
||||
[ViewVariables]
|
||||
public SharedBodyComponent? Body
|
||||
{
|
||||
get => _body;
|
||||
set
|
||||
{
|
||||
if (_body == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var old = _body;
|
||||
_body = value;
|
||||
|
||||
if (old != null)
|
||||
{
|
||||
RemovedFromBody(old);
|
||||
}
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
AddedToBody(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="BodyPartType"/> that this <see cref="IBodyPart"/> is considered
|
||||
/// to be.
|
||||
/// For example, <see cref="BodyPartType.Arm"/>.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("partType")]
|
||||
public BodyPartType PartType { get; private set; } = BodyPartType.Other;
|
||||
|
||||
/// <summary>
|
||||
/// Determines how many mechanisms can be fit inside this
|
||||
/// <see cref="SharedBodyPartComponent"/>.
|
||||
/// </summary>
|
||||
[ViewVariables] [DataField("size")] public int Size { get; private set; } = 1;
|
||||
|
||||
[ViewVariables] public int SizeUsed { get; private set; }
|
||||
|
||||
// TODO BODY size used
|
||||
// TODO BODY surgerydata
|
||||
|
||||
/// <summary>
|
||||
/// What types of BodyParts this <see cref="SharedBodyPartComponent"/> can easily attach to.
|
||||
/// For the most part, most limbs aren't universal and require extra work to
|
||||
/// attach between types.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("compatibility")]
|
||||
public BodyPartCompatibility Compatibility = BodyPartCompatibility.Universal;
|
||||
|
||||
// TODO BODY Mechanisms occupying different parts at the body level
|
||||
[ViewVariables]
|
||||
public IReadOnlyCollection<MechanismComponent> Mechanisms => _mechanisms;
|
||||
|
||||
// TODO BODY Replace with a simulation of organs
|
||||
/// <summary>
|
||||
/// Whether or not the owning <see cref="Body"/> will die if all
|
||||
/// <see cref="SharedBodyPartComponent"/>s of this type are removed from it.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("vital")]
|
||||
public bool IsVital = false;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("symmetry")]
|
||||
public BodyPartSymmetry Symmetry = BodyPartSymmetry.None;
|
||||
|
||||
protected virtual void OnAddMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
var prototypeId = _entMan.GetComponent<MetaDataComponent>(mechanism.Owner).EntityPrototype!.ID;
|
||||
|
||||
if (!_mechanismIds.Contains(prototypeId))
|
||||
{
|
||||
_mechanismIds.Add(prototypeId);
|
||||
}
|
||||
|
||||
mechanism.Part = this;
|
||||
SizeUsed += mechanism.Size;
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
protected virtual void OnRemoveMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
_mechanismIds.Remove(_entMan.GetComponent<MetaDataComponent>(mechanism.Owner).EntityPrototype!.ID);
|
||||
mechanism.Part = null;
|
||||
SizeUsed -= mechanism.Size;
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
var mechanismIds = new EntityUid[_mechanisms.Count];
|
||||
|
||||
var i = 0;
|
||||
foreach (var mechanism in _mechanisms)
|
||||
{
|
||||
mechanismIds[i] = mechanism.Owner;
|
||||
i++;
|
||||
}
|
||||
|
||||
return new BodyPartComponentState(mechanismIds);
|
||||
}
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
base.HandleComponentState(curState, nextState);
|
||||
|
||||
if (curState is not BodyPartComponentState state)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newMechanisms = state.Mechanisms();
|
||||
|
||||
foreach (var mechanism in _mechanisms.ToArray())
|
||||
{
|
||||
if (!newMechanisms.Contains(mechanism))
|
||||
{
|
||||
RemoveMechanism(mechanism);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var mechanism in newMechanisms)
|
||||
{
|
||||
if (!_mechanisms.Contains(mechanism))
|
||||
{
|
||||
TryAddMechanism(mechanism, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool CanAddMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
return SizeUsed + mechanism.Size <= Size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a <see cref="MechanismComponent"/> to this part.
|
||||
/// </summary>
|
||||
/// <param name="mechanism">The mechanism to add.</param>
|
||||
/// <param name="force">
|
||||
/// Whether or not to check if the mechanism is compatible.
|
||||
/// Passing true does not guarantee it to be added, for example if
|
||||
/// it was already added before.
|
||||
/// </param>
|
||||
/// <returns>true if added, false otherwise even if it was already added.</returns>
|
||||
public bool TryAddMechanism(MechanismComponent mechanism, bool force = false)
|
||||
{
|
||||
DebugTools.AssertNotNull(mechanism);
|
||||
|
||||
if (!force && !CanAddMechanism(mechanism))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_mechanisms.Add(mechanism))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
OnAddMechanism(mechanism);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to remove the given <see cref="mechanism"/> from this part.
|
||||
/// </summary>
|
||||
/// <param name="mechanism">The mechanism to remove.</param>
|
||||
/// <returns>True if it was removed, false otherwise.</returns>
|
||||
public bool RemoveMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
DebugTools.AssertNotNull(mechanism);
|
||||
|
||||
if (!_mechanisms.Remove(mechanism))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
OnRemoveMechanism(mechanism);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to remove the given <see cref="mechanism"/> from this
|
||||
/// part and drops it at the specified coordinates.
|
||||
/// </summary>
|
||||
/// <param name="mechanism">The mechanism to remove.</param>
|
||||
/// <param name="coordinates">The coordinates to drop it at.</param>
|
||||
/// <returns>True if it was removed, false otherwise.</returns>
|
||||
public bool RemoveMechanism(MechanismComponent mechanism, EntityCoordinates coordinates)
|
||||
{
|
||||
if (RemoveMechanism(mechanism))
|
||||
{
|
||||
_entMan.GetComponent<TransformComponent>(mechanism.Owner).Coordinates = coordinates;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to destroy the given <see cref="MechanismComponent"/> from
|
||||
/// this part.
|
||||
/// The mechanism won't be deleted if it is not in this body part.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if the mechanism was in this body part and destroyed,
|
||||
/// false otherwise.
|
||||
/// </returns>
|
||||
public bool DeleteMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
DebugTools.AssertNotNull(mechanism);
|
||||
|
||||
if (!RemoveMechanism(mechanism))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_entMan.DeleteEntity(mechanism.Owner);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void AddedToBody(SharedBodyComponent body)
|
||||
{
|
||||
OnAddedToBody(body);
|
||||
|
||||
foreach (var mechanism in _mechanisms)
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(mechanism.Owner, new AddedToBodyEvent(body), true);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemovedFromBody(SharedBodyComponent old)
|
||||
{
|
||||
if (_entMan.TryGetComponent<TransformComponent>(Owner, out var transformComponent))
|
||||
{
|
||||
transformComponent.AttachToGridOrMap();
|
||||
}
|
||||
|
||||
OnRemovedFromBody(old);
|
||||
|
||||
foreach (var mechanism in _mechanisms)
|
||||
{
|
||||
_entMan.EventBus.RaiseLocalEvent(mechanism.Owner, new RemovedFromBodyEvent(old), true);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnAddedToBody(SharedBodyComponent body) { }
|
||||
|
||||
protected virtual void OnRemovedFromBody(SharedBodyComponent old) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gibs the body part.
|
||||
/// </summary>
|
||||
public virtual HashSet<EntityUid> Gib()
|
||||
{
|
||||
var gibs = new HashSet<EntityUid>();
|
||||
|
||||
foreach (var mechanism in _mechanisms.ToArray())
|
||||
{
|
||||
gibs.Add(mechanism.Owner);
|
||||
RemoveMechanism(mechanism);
|
||||
}
|
||||
|
||||
return gibs;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class BodyPartComponentState : ComponentState
|
||||
{
|
||||
[NonSerialized] private List<MechanismComponent>? _mechanisms;
|
||||
|
||||
public readonly EntityUid[] MechanismIds;
|
||||
|
||||
public BodyPartComponentState(EntityUid[] mechanismIds)
|
||||
{
|
||||
MechanismIds = mechanismIds;
|
||||
}
|
||||
|
||||
public List<MechanismComponent> Mechanisms(IEntityManager? entityManager = null)
|
||||
{
|
||||
if (_mechanisms != null)
|
||||
{
|
||||
return _mechanisms;
|
||||
}
|
||||
|
||||
IoCManager.Resolve(ref entityManager);
|
||||
|
||||
var mechanisms = new List<MechanismComponent>(MechanismIds.Length);
|
||||
|
||||
foreach (var id in MechanismIds)
|
||||
{
|
||||
if (!entityManager.EntityExists(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entityManager.TryGetComponent(id, out MechanismComponent? mechanism))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
mechanisms.Add(mechanism);
|
||||
}
|
||||
|
||||
return _mechanisms = mechanisms;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user