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:
213
Content.Shared/Body/Systems/SharedBodySystem.Body.cs
Normal file
213
Content.Shared/Body/Systems/SharedBodySystem.Body.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Organ;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Body.Prototypes;
|
||||
using Content.Shared.Coordinates;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Body.Systems;
|
||||
|
||||
public partial class SharedBodySystem
|
||||
{
|
||||
public void InitializeBody()
|
||||
{
|
||||
SubscribeLocalEvent<BodyComponent, MapInitEvent>(OnBodyMapInit);
|
||||
SubscribeLocalEvent<BodyComponent, ComponentInit>(OnBodyInit);
|
||||
|
||||
SubscribeLocalEvent<BodyComponent, ComponentGetState>(OnBodyGetState);
|
||||
SubscribeLocalEvent<BodyComponent, ComponentHandleState>(OnBodyHandleState);
|
||||
}
|
||||
|
||||
private void OnBodyMapInit(EntityUid bodyId, BodyComponent body, MapInitEvent args)
|
||||
{
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (body.Prototype == null || body.Root != null)
|
||||
return;
|
||||
|
||||
var prototype = Prototypes.Index<BodyPrototype>(body.Prototype);
|
||||
InitBody(body, prototype);
|
||||
}
|
||||
|
||||
private void OnBodyInit(EntityUid bodyId, BodyComponent body, ComponentInit args)
|
||||
{
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (body.Prototype == null || body.Root != null)
|
||||
return;
|
||||
|
||||
var prototype = Prototypes.Index<BodyPrototype>(body.Prototype);
|
||||
InitBody(body, prototype);
|
||||
}
|
||||
|
||||
private void OnBodyGetState(EntityUid uid, BodyComponent body, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new BodyComponentState(body.Root, body.GibSound);
|
||||
}
|
||||
|
||||
private void OnBodyHandleState(EntityUid uid, BodyComponent body, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not BodyComponentState state)
|
||||
return;
|
||||
|
||||
body.Root = state.Root;
|
||||
body.GibSound = state.GibSound;
|
||||
}
|
||||
|
||||
public bool TryCreateBodyRootSlot(
|
||||
EntityUid? bodyId,
|
||||
string slotId,
|
||||
[NotNullWhen(true)] out BodyPartSlot? slot,
|
||||
BodyComponent? body = null)
|
||||
{
|
||||
slot = null;
|
||||
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (bodyId == null ||
|
||||
!Resolve(bodyId.Value, ref body, false) ||
|
||||
body.Root != null)
|
||||
return false;
|
||||
|
||||
slot = new BodyPartSlot(slotId, bodyId.Value, null);
|
||||
body.Root = slot;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void InitBody(BodyComponent body, BodyPrototype prototype)
|
||||
{
|
||||
var root = prototype.Slots[prototype.Root];
|
||||
var bodyId = Spawn(root.Part, body.Owner.ToCoordinates());
|
||||
var partComponent = Comp<BodyPartComponent>(bodyId);
|
||||
var slot = new BodyPartSlot(root.Part, body.Owner, partComponent.PartType);
|
||||
body.Root = slot;
|
||||
partComponent.Body = bodyId;
|
||||
|
||||
Containers.EnsureContainer<Container>(body.Owner, BodyContainerId);
|
||||
|
||||
AttachPart(bodyId, slot, partComponent);
|
||||
InitPart(partComponent, prototype, prototype.Root);
|
||||
}
|
||||
|
||||
private void InitPart(BodyPartComponent parent, BodyPrototype prototype, string slotId, HashSet<string>? initialized = null)
|
||||
{
|
||||
initialized ??= new HashSet<string>();
|
||||
|
||||
if (initialized.Contains(slotId))
|
||||
return;
|
||||
|
||||
initialized.Add(slotId);
|
||||
|
||||
var (_, connections, organs) = prototype.Slots[slotId];
|
||||
connections = new HashSet<string>(connections);
|
||||
connections.ExceptWith(initialized);
|
||||
|
||||
var coordinates = parent.Owner.ToCoordinates();
|
||||
var subConnections = new List<(BodyPartComponent child, string slotId)>();
|
||||
|
||||
Containers.EnsureContainer<Container>(parent.Owner, BodyContainerId);
|
||||
|
||||
foreach (var connection in connections)
|
||||
{
|
||||
var childSlot = prototype.Slots[connection];
|
||||
var childPart = Spawn(childSlot.Part, coordinates);
|
||||
var childPartComponent = Comp<BodyPartComponent>(childPart);
|
||||
var slot = CreatePartSlot(connection, parent.Owner, childPartComponent.PartType, parent);
|
||||
if (slot == null)
|
||||
{
|
||||
Logger.Error($"Could not create slot for connection {connection} in body {prototype.ID}");
|
||||
continue;
|
||||
}
|
||||
|
||||
AttachPart(childPart, slot, childPartComponent);
|
||||
subConnections.Add((childPartComponent, connection));
|
||||
}
|
||||
|
||||
foreach (var (organSlotId, organId) in organs)
|
||||
{
|
||||
var organ = Spawn(organId, coordinates);
|
||||
var organComponent = Comp<OrganComponent>(organ);
|
||||
|
||||
var slot = CreateOrganSlot(organSlotId, parent.Owner, parent);
|
||||
if (slot == null)
|
||||
{
|
||||
Logger.Error($"Could not create slot for connection {organSlotId} in body {prototype.ID}");
|
||||
continue;
|
||||
}
|
||||
|
||||
InsertOrgan(organ, slot, organComponent);
|
||||
}
|
||||
|
||||
foreach (var connection in subConnections)
|
||||
{
|
||||
InitPart(connection.child, prototype, connection.slotId, initialized);
|
||||
}
|
||||
}
|
||||
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyChildren(EntityUid? id, BodyComponent? body = null)
|
||||
{
|
||||
if (id == null ||
|
||||
!Resolve(id.Value, ref body, false) ||
|
||||
!TryComp(body.Root.Child, out BodyPartComponent? part))
|
||||
yield break;
|
||||
|
||||
yield return (body.Root.Child.Value, part);
|
||||
|
||||
foreach (var child in GetPartChildren(body.Root.Child))
|
||||
{
|
||||
yield return child;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<(EntityUid Id, OrganComponent Component)> GetBodyOrgans(EntityUid? bodyId, BodyComponent? body = null)
|
||||
{
|
||||
if (bodyId == null || !Resolve(bodyId.Value, ref body, false))
|
||||
yield break;
|
||||
|
||||
foreach (var part in GetBodyChildren(bodyId, body))
|
||||
{
|
||||
foreach (var organ in GetPartOrgans(part.Id, part.Component))
|
||||
{
|
||||
yield return organ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<BodyPartSlot> GetBodyAllSlots(EntityUid? bodyId, BodyComponent? body = null)
|
||||
{
|
||||
if (bodyId == null || !Resolve(bodyId.Value, ref body, false))
|
||||
yield break;
|
||||
|
||||
foreach (var slot in GetPartAllSlots(body.Root.Child))
|
||||
{
|
||||
yield return slot;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual HashSet<EntityUid> GibBody(EntityUid? partId, bool gibOrgans = false,
|
||||
BodyComponent? body = null)
|
||||
{
|
||||
if (partId == null || !Resolve(partId.Value, ref body, false))
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
var parts = GetBodyChildren(partId, body).ToArray();
|
||||
var gibs = new HashSet<EntityUid>(parts.Length);
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
DropPart(part.Id, part.Component);
|
||||
gibs.Add(part.Id);
|
||||
|
||||
if (!gibOrgans)
|
||||
continue;
|
||||
|
||||
foreach (var organ in GetPartOrgans(part.Id, part.Component))
|
||||
{
|
||||
DropOrgan(organ.Id, organ.Component);
|
||||
gibs.Add(organ.Id);
|
||||
}
|
||||
}
|
||||
|
||||
return gibs;
|
||||
}
|
||||
}
|
||||
230
Content.Shared/Body/Systems/SharedBodySystem.Organs.cs
Normal file
230
Content.Shared/Body/Systems/SharedBodySystem.Organs.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Events;
|
||||
using Content.Shared.Body.Organ;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Body.Systems;
|
||||
|
||||
public partial class SharedBodySystem
|
||||
{
|
||||
private void InitializeOrgans()
|
||||
{
|
||||
SubscribeLocalEvent<OrganComponent, ComponentGetState>(OnOrganGetState);
|
||||
SubscribeLocalEvent<OrganComponent, ComponentHandleState>(OnOrganHandleState);
|
||||
}
|
||||
|
||||
private OrganSlot? CreateOrganSlot(string slotId, EntityUid parent, BodyPartComponent? part = null)
|
||||
{
|
||||
if (!Resolve(parent, ref part, false))
|
||||
return null;
|
||||
|
||||
var slot = new OrganSlot(slotId, parent);
|
||||
part.Organs.Add(slotId, slot);
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
private bool CanInsertOrgan(EntityUid? organId, OrganSlot slot, OrganComponent? organ = null)
|
||||
{
|
||||
return organId != null &&
|
||||
slot.Child == null &&
|
||||
Resolve(organId.Value, ref organ, false) &&
|
||||
Containers.TryGetContainer(slot.Parent, BodyContainerId, out var container) &&
|
||||
container.CanInsert(organId.Value);
|
||||
}
|
||||
|
||||
private void OnOrganGetState(EntityUid uid, OrganComponent organ, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new OrganComponentState(organ.Body, organ.ParentSlot);
|
||||
}
|
||||
|
||||
private void OnOrganHandleState(EntityUid uid, OrganComponent organ, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not OrganComponentState state)
|
||||
return;
|
||||
|
||||
organ.Body = state.Body;
|
||||
organ.ParentSlot = state.Parent;
|
||||
}
|
||||
|
||||
public bool InsertOrgan(EntityUid? organId, OrganSlot slot, OrganComponent? organ = null)
|
||||
{
|
||||
if (organId == null ||
|
||||
!Resolve(organId.Value, ref organ, false) ||
|
||||
!CanInsertOrgan(organId, slot, organ))
|
||||
return false;
|
||||
|
||||
DropOrgan(slot.Child);
|
||||
DropOrgan(organId, organ);
|
||||
|
||||
var container = Containers.EnsureContainer<Container>(slot.Parent, BodyContainerId);
|
||||
if (!container.Insert(organId.Value))
|
||||
return false;
|
||||
|
||||
slot.Child = organId;
|
||||
organ.ParentSlot = slot;
|
||||
organ.Body = CompOrNull<BodyPartComponent>(slot.Parent)?.Body;
|
||||
|
||||
Dirty(slot.Parent);
|
||||
Dirty(organId.Value);
|
||||
|
||||
if (organ.Body == null)
|
||||
{
|
||||
RaiseLocalEvent(organId.Value, new AddedToPartEvent(slot.Parent));
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseLocalEvent(organId.Value, new AddedToPartInBodyEvent(organ.Body.Value, slot.Parent));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AddOrganToFirstValidSlot(
|
||||
EntityUid? childId,
|
||||
EntityUid? parentId,
|
||||
OrganComponent? child = null,
|
||||
BodyPartComponent? parent = null)
|
||||
{
|
||||
if (childId == null ||
|
||||
!Resolve(childId.Value, ref child, false) ||
|
||||
parentId == null ||
|
||||
!Resolve(parentId.Value, ref parent, false))
|
||||
return false;
|
||||
|
||||
foreach (var slot in parent.Organs.Values)
|
||||
{
|
||||
if (slot.Child == null)
|
||||
continue;
|
||||
|
||||
InsertOrgan(childId, slot, child);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DropOrgan(EntityUid? organId, OrganComponent? organ = null)
|
||||
{
|
||||
if (organId == null ||
|
||||
!Resolve(organId.Value, ref organ, false) ||
|
||||
organ.ParentSlot is not { } slot)
|
||||
return false;
|
||||
|
||||
var oldParent = CompOrNull<BodyPartComponent>(organ.ParentSlot.Parent);
|
||||
|
||||
slot.Child = null;
|
||||
organ.ParentSlot = null;
|
||||
organ.Body = null;
|
||||
|
||||
if (Containers.TryGetContainer(slot.Parent, BodyContainerId, out var container))
|
||||
container.Remove(organId.Value);
|
||||
|
||||
if (TryComp(organId, out TransformComponent? transform))
|
||||
transform.AttachToGridOrMap();
|
||||
|
||||
organ.Owner.RandomOffset(0.25f);
|
||||
|
||||
if (oldParent == null)
|
||||
return true;
|
||||
|
||||
if (oldParent.Body != null)
|
||||
{
|
||||
RaiseLocalEvent(organId.Value, new RemovedFromPartInBodyEvent(oldParent.Body.Value, oldParent.Owner));
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseLocalEvent(organId.Value, new RemovedFromPartEvent(oldParent.Owner));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DropOrganAt(EntityUid? organId, EntityCoordinates dropAt, OrganComponent? organ = null)
|
||||
{
|
||||
if (organId == null || !DropOrgan(organId, organ))
|
||||
return false;
|
||||
|
||||
if (TryComp(organId.Value, out TransformComponent? transform))
|
||||
transform.Coordinates = dropAt;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DeleteOrgan(EntityUid? id, OrganComponent? part = null)
|
||||
{
|
||||
if (id == null || !Resolve(id.Value, ref part, false))
|
||||
return false;
|
||||
|
||||
DropOrgan(id, part);
|
||||
|
||||
if (Deleted(id.Value))
|
||||
return false;
|
||||
|
||||
Del(id.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of ValueTuples of <see cref="T"/> and OrganComponent on each organ
|
||||
/// in the given body.
|
||||
/// </summary>
|
||||
/// <param name="uid">The entity to check for the component on.</param>
|
||||
/// <param name="body">The body to check for organs on.</param>
|
||||
/// <typeparam name="T">The component to check for.</typeparam>
|
||||
public List<(T Comp, OrganComponent Organ)> GetBodyOrganComponents<T>(
|
||||
EntityUid uid,
|
||||
BodyComponent? body = null)
|
||||
where T : Component
|
||||
{
|
||||
if (!Resolve(uid, ref body))
|
||||
return new List<(T Comp, OrganComponent Organ)>();
|
||||
|
||||
var query = EntityManager.GetEntityQuery<T>();
|
||||
var list = new List<(T Comp, OrganComponent Organ)>(3);
|
||||
foreach (var organ in GetBodyOrgans(uid, body))
|
||||
{
|
||||
if (query.TryGetComponent(organ.Id, out var comp))
|
||||
list.Add((comp, organ.Component));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a list of ValueTuples of <see cref="T"/> and OrganComponent on each organs
|
||||
/// in the given body.
|
||||
/// </summary>
|
||||
/// <param name="uid">The entity to check for the component on.</param>
|
||||
/// <param name="comps">The list of components.</param>
|
||||
/// <param name="body">The body to check for organs on.</param>
|
||||
/// <typeparam name="T">The component to check for.</typeparam>
|
||||
/// <returns>Whether any were found.</returns>
|
||||
public bool TryGetBodyOrganComponents<T>(
|
||||
EntityUid uid,
|
||||
[NotNullWhen(true)] out List<(T Comp, OrganComponent Organ)>? comps,
|
||||
BodyComponent? body = null)
|
||||
where T : Component
|
||||
{
|
||||
if (!Resolve(uid, ref body))
|
||||
{
|
||||
comps = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
comps = GetBodyOrganComponents<T>(uid, body);
|
||||
|
||||
if (comps.Count == 0)
|
||||
{
|
||||
comps = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
359
Content.Shared/Body/Systems/SharedBodySystem.Parts.cs
Normal file
359
Content.Shared/Body/Systems/SharedBodySystem.Parts.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Events;
|
||||
using Content.Shared.Body.Organ;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Damage.Prototypes;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Body.Systems;
|
||||
|
||||
public partial class SharedBodySystem
|
||||
{
|
||||
private void InitializeParts()
|
||||
{
|
||||
SubscribeLocalEvent<BodyPartComponent, ComponentRemove>(OnPartRemoved);
|
||||
SubscribeLocalEvent<BodyPartComponent, ComponentGetState>(OnPartGetState);
|
||||
SubscribeLocalEvent<BodyPartComponent, ComponentHandleState>(OnPartHandleState);
|
||||
}
|
||||
|
||||
private void OnPartGetState(EntityUid uid, BodyPartComponent part, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new BodyPartComponentState(
|
||||
part.Body,
|
||||
part.ParentSlot,
|
||||
part.Children,
|
||||
part.Organs,
|
||||
part.PartType,
|
||||
part.IsVital,
|
||||
part.Symmetry
|
||||
);
|
||||
}
|
||||
|
||||
private void OnPartHandleState(EntityUid uid, BodyPartComponent part, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not BodyPartComponentState state)
|
||||
return;
|
||||
|
||||
part.Body = state.Body;
|
||||
part.ParentSlot = state.ParentSlot;
|
||||
part.Children = state.Children;
|
||||
part.Organs = state.Organs;
|
||||
part.PartType = state.PartType;
|
||||
part.IsVital = state.IsVital;
|
||||
part.Symmetry = state.Symmetry;
|
||||
}
|
||||
|
||||
private void OnPartRemoved(EntityUid uid, BodyPartComponent part, ComponentRemove args)
|
||||
{
|
||||
if (part.ParentSlot is { } slot)
|
||||
{
|
||||
slot.Child = null;
|
||||
Dirty(slot.Parent);
|
||||
}
|
||||
|
||||
foreach (var childSlot in part.Children.Values.ToArray())
|
||||
{
|
||||
DropPart(childSlot.Child);
|
||||
}
|
||||
}
|
||||
|
||||
private BodyPartSlot? CreatePartSlot(
|
||||
string slotId,
|
||||
EntityUid parent,
|
||||
BodyPartType partType,
|
||||
BodyPartComponent? part = null)
|
||||
{
|
||||
if (!Resolve(parent, ref part, false))
|
||||
return null;
|
||||
|
||||
var slot = new BodyPartSlot(slotId, parent, partType);
|
||||
part.Children.Add(slotId, slot);
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
public bool TryCreatePartSlot(
|
||||
EntityUid? parentId,
|
||||
string id,
|
||||
[NotNullWhen(true)] out BodyPartSlot? slot,
|
||||
BodyPartComponent? parent = null)
|
||||
{
|
||||
slot = null;
|
||||
|
||||
if (parentId == null ||
|
||||
!Resolve(parentId.Value, ref parent, false))
|
||||
return false;
|
||||
|
||||
slot = new BodyPartSlot(id, parentId.Value, null);
|
||||
if (!parent.Children.TryAdd(id, slot))
|
||||
{
|
||||
slot = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryCreatePartSlotAndAttach(
|
||||
EntityUid? parentId,
|
||||
string id,
|
||||
EntityUid? childId,
|
||||
BodyPartComponent? parent = null,
|
||||
BodyPartComponent? child = null)
|
||||
{
|
||||
return TryCreatePartSlot(parentId, id, out var slot, parent) && AttachPart(childId, slot, child);
|
||||
}
|
||||
|
||||
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetPartChildren(EntityUid? id, BodyPartComponent? part = null)
|
||||
{
|
||||
if (id == null || !Resolve(id.Value, ref part, false))
|
||||
yield break;
|
||||
|
||||
foreach (var slot in part.Children.Values)
|
||||
{
|
||||
if (!TryComp(slot.Child, out BodyPartComponent? childPart))
|
||||
continue;
|
||||
|
||||
yield return (slot.Child.Value, childPart);
|
||||
|
||||
foreach (var subChild in GetPartChildren(slot.Child, childPart))
|
||||
{
|
||||
yield return subChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<(EntityUid Id, OrganComponent Component)> GetPartOrgans(EntityUid? partId, BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null || !Resolve(partId.Value, ref part, false))
|
||||
yield break;
|
||||
|
||||
foreach (var slot in part.Organs.Values)
|
||||
{
|
||||
if (!TryComp(slot.Child, out OrganComponent? organ))
|
||||
continue;
|
||||
|
||||
yield return (slot.Child.Value, organ);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<BodyPartSlot> GetPartAllSlots(EntityUid? partId, BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null ||
|
||||
!Resolve(partId.Value, ref part, false))
|
||||
yield break;
|
||||
|
||||
foreach (var slot in part.Children.Values)
|
||||
{
|
||||
yield return slot;
|
||||
|
||||
if (!TryComp(slot.Child, out BodyComponent? childPart))
|
||||
continue;
|
||||
|
||||
foreach (var subChild in GetBodyAllSlots(slot.Child, childPart))
|
||||
{
|
||||
yield return subChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanAttachPart([NotNullWhen(true)] EntityUid? partId, BodyPartSlot slot, BodyPartComponent? part = null)
|
||||
{
|
||||
return partId != null &&
|
||||
slot.Child == null &&
|
||||
Resolve(partId.Value, ref part, false) &&
|
||||
(slot.Type == null || slot.Type == part.PartType) &&
|
||||
Containers.TryGetContainer(slot.Parent, BodyContainerId, out var container) &&
|
||||
container.CanInsert(partId.Value);
|
||||
}
|
||||
|
||||
public virtual bool AttachPart(
|
||||
EntityUid? partId,
|
||||
BodyPartSlot slot,
|
||||
[NotNullWhen(true)] BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null ||
|
||||
!Resolve(partId.Value, ref part, false) ||
|
||||
!CanAttachPart(partId, slot, part))
|
||||
return false;
|
||||
|
||||
DropPart(slot.Child);
|
||||
DropPart(partId, part);
|
||||
|
||||
var container = Containers.EnsureContainer<Container>(slot.Parent, BodyContainerId);
|
||||
if (!container.Insert(partId.Value))
|
||||
return false;
|
||||
|
||||
slot.Child = partId;
|
||||
part.ParentSlot = slot;
|
||||
|
||||
if (TryComp(slot.Parent, out BodyPartComponent? parentPart))
|
||||
{
|
||||
part.Body = parentPart.Body;
|
||||
}
|
||||
else if (TryComp(slot.Parent, out BodyComponent? parentBody))
|
||||
{
|
||||
part.Body = parentBody.Owner;
|
||||
}
|
||||
else
|
||||
{
|
||||
part.Body = null;
|
||||
}
|
||||
|
||||
Dirty(slot.Parent);
|
||||
Dirty(partId.Value);
|
||||
|
||||
if (part.Body is { } newBody)
|
||||
{
|
||||
var argsAdded = new BodyPartAddedEventArgs(slot.Id, part);
|
||||
|
||||
// TODO: Body refactor. Somebody is doing it
|
||||
// EntitySystem.Get<SharedHumanoidAppearanceSystem>().BodyPartAdded(Owner, argsAdded);
|
||||
foreach (var component in AllComps<IBodyPartAdded>(newBody).ToArray())
|
||||
{
|
||||
component.BodyPartAdded(argsAdded);
|
||||
}
|
||||
|
||||
foreach (var organ in GetPartOrgans(partId, part))
|
||||
{
|
||||
RaiseLocalEvent(organ.Id, new AddedToBodyEvent(newBody), true);
|
||||
}
|
||||
|
||||
Dirty(newBody);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool DropPart(EntityUid? partId, [NotNullWhen(true)] BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null ||
|
||||
!Resolve(partId.Value, ref part, false) ||
|
||||
part.ParentSlot is not { } slot)
|
||||
return false;
|
||||
|
||||
var oldBody = part.Body;
|
||||
|
||||
slot.Child = null;
|
||||
part.ParentSlot = null;
|
||||
part.Body = null;
|
||||
|
||||
if (Containers.TryGetContainer(slot.Parent, BodyContainerId, out var container))
|
||||
container.Remove(partId.Value);
|
||||
|
||||
if (TryComp(partId, out TransformComponent? transform))
|
||||
transform.AttachToGridOrMap();
|
||||
|
||||
part.Owner.RandomOffset(0.25f);
|
||||
|
||||
if (oldBody != null)
|
||||
{
|
||||
var args = new BodyPartRemovedEventArgs(slot.Id, part);
|
||||
foreach (var component in AllComps<IBodyPartRemoved>(oldBody.Value))
|
||||
{
|
||||
component.BodyPartRemoved(args);
|
||||
}
|
||||
|
||||
if (part.PartType == BodyPartType.Leg &&
|
||||
!GetBodyChildrenOfType(oldBody, BodyPartType.Leg).Any())
|
||||
{
|
||||
Standing.Down(oldBody.Value);
|
||||
}
|
||||
|
||||
if (part.IsVital && !GetBodyChildrenOfType(oldBody, part.PartType).Any())
|
||||
{
|
||||
// TODO BODY SYSTEM KILL : Find a more elegant way of killing em than just dumping bloodloss damage.
|
||||
var damage = new DamageSpecifier(Prototypes.Index<DamageTypePrototype>("Bloodloss"), 300);
|
||||
Damageable.TryChangeDamage(part.Owner, damage);
|
||||
}
|
||||
|
||||
foreach (var organSlot in part.Organs.Values)
|
||||
{
|
||||
if (organSlot.Child is not { } child)
|
||||
continue;
|
||||
|
||||
RaiseLocalEvent(child, new RemovedFromBodyEvent(oldBody.Value), true);
|
||||
}
|
||||
}
|
||||
|
||||
Dirty(slot.Parent);
|
||||
Dirty(partId.Value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DropPartAt(EntityUid? partId, EntityCoordinates dropAt, BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null || !DropPart(partId, part))
|
||||
return false;
|
||||
|
||||
if (TryComp(partId.Value, out TransformComponent? transform))
|
||||
transform.Coordinates = dropAt;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OrphanPart(EntityUid? partId, BodyPartComponent? part = null)
|
||||
{
|
||||
if (partId == null || !Resolve(partId.Value, ref part, false))
|
||||
return false;
|
||||
|
||||
DropPart(partId, part);
|
||||
|
||||
foreach (var slot in part.Children.Values)
|
||||
{
|
||||
DropPart(slot.Child);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DeletePart(EntityUid? id, BodyPartComponent? part = null)
|
||||
{
|
||||
if (id == null || !Resolve(id.Value, ref part, false))
|
||||
return false;
|
||||
|
||||
DropPart(id, part);
|
||||
|
||||
if (Deleted(id.Value))
|
||||
return false;
|
||||
|
||||
Del(id.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyChildrenOfType(EntityUid? bodyId, BodyPartType type, BodyComponent? body = null)
|
||||
{
|
||||
foreach (var part in GetBodyChildren(bodyId, body))
|
||||
{
|
||||
if (part.Component.PartType == type)
|
||||
yield return part;
|
||||
}
|
||||
}
|
||||
|
||||
public bool BodyHasChildOfType(EntityUid? bodyId, BodyPartType type, BodyComponent? body = null)
|
||||
{
|
||||
return GetBodyChildrenOfType(bodyId, type, body).Any();
|
||||
}
|
||||
|
||||
public bool BodyHasChild(
|
||||
EntityUid? parentId,
|
||||
EntityUid? childId,
|
||||
BodyComponent? parent = null,
|
||||
BodyPartComponent? child = null)
|
||||
{
|
||||
if (parentId == null ||
|
||||
!Resolve(parentId.Value, ref parent, false) ||
|
||||
childId == null ||
|
||||
!Resolve(childId.Value, ref child, false))
|
||||
return false;
|
||||
|
||||
return child.ParentSlot?.Child == parentId;
|
||||
}
|
||||
}
|
||||
26
Content.Shared/Body/Systems/SharedBodySystem.cs
Normal file
26
Content.Shared/Body/Systems/SharedBodySystem.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Body.Systems;
|
||||
|
||||
public abstract partial class SharedBodySystem : EntitySystem
|
||||
{
|
||||
private const string BodyContainerId = "BodyContainer";
|
||||
|
||||
[Dependency] protected readonly IPrototypeManager Prototypes = default!;
|
||||
|
||||
[Dependency] protected readonly SharedContainerSystem Containers = default!;
|
||||
[Dependency] protected readonly DamageableSystem Damageable = default!;
|
||||
[Dependency] protected readonly StandingStateSystem Standing = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
InitializeBody();
|
||||
InitializeParts();
|
||||
InitializeOrgans();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user