Refactor body component to use slots instead of an army of dictionaries (#3749)

* Refactor body component to use slots instead of an army of dictionaries

* Update vox

* Replace static method call with extension

* Add setpart method, replace dispose with shutdown

* Fix tests, fix not listening to slot events when setting a part
This commit is contained in:
DrSmugleaf
2021-04-05 14:54:51 +02:00
committed by GitHub
parent 5387f87608
commit 677706b117
30 changed files with 602 additions and 466 deletions

View File

@@ -1,6 +1,5 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.Utility;
using Content.Shared.GameObjects.Components.Body;
@@ -61,7 +60,7 @@ namespace Content.Server.GameObjects.Components.Body.Part
{
base.Initialize();
_mechanismContainer = ContainerHelpers.EnsureContainer<Container>(Owner, $"{Name}-{nameof(BodyPartComponent)}");
_mechanismContainer = Owner.EnsureContainer<Container>($"{Name}-{nameof(BodyPartComponent)}");
// This is ran in Startup as entities spawned in Initialize
// are not synced to the client since they are assumed to be
@@ -123,25 +122,23 @@ namespace Content.Server.GameObjects.Components.Body.Part
// Here we are trying to grab a list of all empty BodySlots adjacent to an existing BodyPart that can be
// attached to. i.e. an empty left hand slot, connected to an occupied left arm slot would be valid.
var unoccupiedSlots = body.Slots.Keys.ToList().Except(body.Parts.Keys.ToList()).ToList();
foreach (var slot in unoccupiedSlots)
foreach (var slot in body.EmptySlots)
{
if (!body.TryGetSlotType(slot, out var typeResult) ||
typeResult != PartType ||
!body.TryGetPartConnections(slot, out var parts))
if (slot.PartType != PartType)
{
continue;
}
foreach (var connectedPart in parts)
foreach (var connection in slot.Connections)
{
if (!connectedPart.CanAttachPart(this))
if (connection.Part == null ||
!connection.Part.CanAttachPart(this))
{
continue;
}
_optionsCache.Add(_idHash, slot);
toSend.Add(slot, _idHash++);
toSend.Add(slot.Id, _idHash++);
}
}
@@ -269,7 +266,7 @@ namespace Content.Server.GameObjects.Components.Body.Part
return;
}
body.TryAddPart($"{nameof(AttachBodyPartVerb)}-{component.Owner.Uid}", component, true);
body.SetPart($"{nameof(AttachBodyPartVerb)}-{component.Owner.Uid}", component);
}
}
}