Refactoring body system to use containers and general body cleanup (#20202)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -17,6 +17,7 @@ using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
@@ -32,80 +33,16 @@ public sealed class BodySystem : SharedBodySystem
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<BodyPartComponent, ComponentStartup>(OnPartStartup);
|
||||
SubscribeLocalEvent<BodyComponent, ComponentStartup>(OnBodyStartup);
|
||||
SubscribeLocalEvent<BodyComponent, MoveInputEvent>(OnRelayMoveInput);
|
||||
SubscribeLocalEvent<BodyComponent, ApplyMetabolicMultiplierEvent>(OnApplyMetabolicMultiplier);
|
||||
SubscribeLocalEvent<BodyComponent, BeingMicrowavedEvent>(OnBeingMicrowaved);
|
||||
}
|
||||
|
||||
private void OnPartStartup(EntityUid uid, BodyPartComponent component, ComponentStartup args)
|
||||
{
|
||||
// This inter-entity relationship makes be deeply uncomfortable because its probably going to re-encounter
|
||||
// all of the networking & startup ordering issues that containers and joints have.
|
||||
// TODO just use containers. Please.
|
||||
|
||||
foreach (var slot in component.Children.Values)
|
||||
{
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
continue;
|
||||
|
||||
if (TryComp(slot.Child, out BodyPartComponent? child))
|
||||
{
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.Error($"Body part encountered missing limbs: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
}
|
||||
|
||||
foreach (var slot in component.Organs.Values)
|
||||
{
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
continue;
|
||||
|
||||
if (TryComp(slot.Child, out OrganComponent? child))
|
||||
{
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.Error($"Body part encountered missing organ: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBodyStartup(EntityUid uid, BodyComponent component, ComponentStartup args)
|
||||
{
|
||||
if (component.Root is not { } slot)
|
||||
return;
|
||||
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
return;
|
||||
|
||||
if (!TryComp(slot.Child, out BodyPartComponent? child))
|
||||
{
|
||||
Log.Error($"Body part encountered missing limbs: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
return;
|
||||
}
|
||||
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
}
|
||||
|
||||
private void OnRelayMoveInput(EntityUid uid, BodyComponent component, ref MoveInputEvent args)
|
||||
{
|
||||
if (_mobState.IsDead(uid) && _mindSystem.TryGetMind(uid, out var mindId, out var mind))
|
||||
@@ -130,98 +67,95 @@ public sealed class BodySystem : SharedBodySystem
|
||||
return;
|
||||
|
||||
// Don't microwave animals, kids
|
||||
_transform.AttachToGridOrMap(uid);
|
||||
SharedTransform.AttachToGridOrMap(uid);
|
||||
_appearance.SetData(args.Microwave, MicrowaveVisualState.Bloody, true);
|
||||
GibBody(uid, false, component);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
public override bool AttachPart(
|
||||
EntityUid? partId,
|
||||
BodyPartSlot slot,
|
||||
[NotNullWhen(true)] BodyPartComponent? part = null)
|
||||
protected override void AddPart(
|
||||
EntityUid bodyUid,
|
||||
EntityUid partUid,
|
||||
string slotId,
|
||||
BodyPartComponent component,
|
||||
BodyComponent? bodyComp = null)
|
||||
{
|
||||
if (!base.AttachPart(partId, slot, part))
|
||||
return false;
|
||||
// TODO: Predict this probably.
|
||||
base.AddPart(bodyUid, partUid, slotId, component, bodyComp);
|
||||
|
||||
if (part.Body is { } body &&
|
||||
TryComp<HumanoidAppearanceComponent>(body, out var humanoid))
|
||||
if (TryComp<HumanoidAppearanceComponent>(bodyUid, out var humanoid))
|
||||
{
|
||||
var layer = part.ToHumanoidLayers();
|
||||
var layer = component.ToHumanoidLayers();
|
||||
if (layer != null)
|
||||
{
|
||||
var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
|
||||
_humanoidSystem.SetLayersVisibility(body, layers, true, true, humanoid);
|
||||
_humanoidSystem.SetLayersVisibility(bodyUid, layers, true, true, humanoid);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool DropPart(EntityUid? partId, BodyPartComponent? part = null)
|
||||
protected override void RemovePart(
|
||||
EntityUid bodyUid,
|
||||
EntityUid partUid,
|
||||
string slotId,
|
||||
BodyPartComponent component,
|
||||
BodyComponent? bodyComp = null)
|
||||
{
|
||||
if (partId == null || !Resolve(partId.Value, ref part))
|
||||
return false;
|
||||
base.RemovePart(bodyUid, partUid, slotId, component, bodyComp);
|
||||
|
||||
if (!base.DropPart(partId, part))
|
||||
return false;
|
||||
if (!TryComp<HumanoidAppearanceComponent>(bodyUid, out var humanoid))
|
||||
return;
|
||||
|
||||
var oldBody = part.Body;
|
||||
if (oldBody == null || !TryComp<HumanoidAppearanceComponent>(oldBody, out var humanoid))
|
||||
return true;
|
||||
var layer = component.ToHumanoidLayers();
|
||||
|
||||
var layer = part.ToHumanoidLayers();
|
||||
if (layer == null)
|
||||
return true;
|
||||
return;
|
||||
|
||||
var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
|
||||
_humanoidSystem.SetLayersVisibility(oldBody.Value, layers, false, true, humanoid);
|
||||
return true;
|
||||
_humanoidSystem.SetLayersVisibility(bodyUid, layers, false, true, humanoid);
|
||||
}
|
||||
|
||||
public override HashSet<EntityUid> GibBody(EntityUid? bodyId, bool gibOrgans = false, BodyComponent? body = null, bool deleteItems = false)
|
||||
public override HashSet<EntityUid> GibBody(EntityUid bodyId, bool gibOrgans = false, BodyComponent? body = null, bool deleteItems = false)
|
||||
{
|
||||
if (bodyId == null || !Resolve(bodyId.Value, ref body, false))
|
||||
if (!Resolve(bodyId, ref body, false))
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
if (LifeStage(bodyId.Value) >= EntityLifeStage.Terminating || EntityManager.IsQueuedForDeletion(bodyId.Value))
|
||||
if (LifeStage(bodyId) >= EntityLifeStage.Terminating || EntityManager.IsQueuedForDeletion(bodyId))
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
var xform = Transform(bodyId.Value);
|
||||
var xform = Transform(bodyId);
|
||||
if (xform.MapUid == null)
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
var gibs = base.GibBody(bodyId, gibOrgans, body, deleteItems);
|
||||
|
||||
var coordinates = xform.Coordinates;
|
||||
var filter = Filter.Pvs(bodyId.Value, entityManager: EntityManager);
|
||||
var filter = Filter.Pvs(bodyId, entityManager: EntityManager);
|
||||
var audio = AudioParams.Default.WithVariation(0.025f);
|
||||
|
||||
_audio.Play(body.GibSound, filter, coordinates, true, audio);
|
||||
|
||||
if (TryComp(bodyId, out ContainerManagerComponent? container))
|
||||
var containers = GetBodyContainers(bodyId, body: body).ToList();
|
||||
|
||||
foreach (var container in containers)
|
||||
{
|
||||
foreach (var cont in container.GetAllContainers().ToArray())
|
||||
foreach (var entity in container.ContainedEntities)
|
||||
{
|
||||
foreach (var ent in cont.ContainedEntities.ToArray())
|
||||
if (deleteItems)
|
||||
{
|
||||
if (deleteItems)
|
||||
{
|
||||
QueueDel(ent);
|
||||
}
|
||||
else
|
||||
{
|
||||
cont.Remove(ent, EntityManager, force: true);
|
||||
Transform(ent).Coordinates = coordinates;
|
||||
ent.RandomOffset(0.25f);
|
||||
}
|
||||
QueueDel(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
container.Remove(entity, EntityManager, force: true);
|
||||
SharedTransform.SetCoordinates(entity,coordinates);
|
||||
entity.RandomOffset(0.25f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RaiseLocalEvent(bodyId.Value, new BeingGibbedEvent(gibs));
|
||||
QueueDel(bodyId.Value);
|
||||
RaiseLocalEvent(bodyId, new BeingGibbedEvent(gibs));
|
||||
QueueDel(bodyId);
|
||||
|
||||
return gibs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user