Make body parts properly use containers for mechanisms (#2290)
This commit is contained in:
@@ -17,32 +17,33 @@ namespace Content.Server.GameObjects.Components.Body
|
||||
[ComponentReference(typeof(IBody))]
|
||||
public class BodyComponent : SharedBodyComponent, IRelayMoveInput
|
||||
{
|
||||
private Container _container = default!;
|
||||
private Container _partContainer = default!;
|
||||
|
||||
protected override bool CanAddPart(string slot, IBodyPart part)
|
||||
{
|
||||
return base.CanAddPart(slot, part) && _container.CanInsert(part.Owner);
|
||||
return base.CanAddPart(slot, part) &&
|
||||
_partContainer.CanInsert(part.Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddPart(string slot, IBodyPart part)
|
||||
{
|
||||
base.OnAddPart(slot, part);
|
||||
|
||||
_container.Insert(part.Owner);
|
||||
_partContainer.Insert(part.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemovePart(string slot, IBodyPart part)
|
||||
{
|
||||
base.OnRemovePart(slot, part);
|
||||
|
||||
_container.ForceRemove(part.Owner);
|
||||
_partContainer.ForceRemove(part.Owner);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_container = ContainerManagerComponent.Ensure<Container>($"{Name}-{nameof(BodyComponent)}", Owner);
|
||||
_partContainer = ContainerManagerComponent.Ensure<Container>($"{Name}-{nameof(BodyComponent)}", Owner);
|
||||
|
||||
foreach (var (slot, partId) in PartIds)
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@ using Content.Shared.GameObjects.Components.Body.Surgery;
|
||||
using Content.Shared.Interfaces;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.Components.Container;
|
||||
using Robust.Server.GameObjects.Components.UserInterface;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
@@ -26,39 +27,39 @@ namespace Content.Server.GameObjects.Components.Body.Part
|
||||
public class BodyPartComponent : SharedBodyPartComponent, IAfterInteract
|
||||
{
|
||||
private readonly Dictionary<int, object> _optionsCache = new Dictionary<int, object>();
|
||||
|
||||
private IBody? _owningBodyCache;
|
||||
|
||||
private int _idHash;
|
||||
|
||||
private IEntity? _surgeonCache;
|
||||
private Container _mechanismContainer = default!;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);
|
||||
|
||||
public override bool CanAddMechanism(IMechanism mechanism)
|
||||
{
|
||||
return base.CanAddMechanism(mechanism) &&
|
||||
_mechanismContainer.CanInsert(mechanism.Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddMechanism(IMechanism mechanism)
|
||||
{
|
||||
base.OnAddMechanism(mechanism);
|
||||
|
||||
if (mechanism.Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
sprite.Visible = false;
|
||||
}
|
||||
_mechanismContainer.Insert(mechanism.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemoveMechanism(IMechanism mechanism)
|
||||
{
|
||||
base.OnRemoveMechanism(mechanism);
|
||||
|
||||
if (mechanism.Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
sprite.Visible = true;
|
||||
}
|
||||
_mechanismContainer.Remove(mechanism.Owner);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_mechanismContainer = ContainerManagerComponent.Ensure<Container>($"{Name}-{nameof(BodyPartComponent)}", Owner);
|
||||
|
||||
// This is ran in Startup as entities spawned in Initialize
|
||||
// are not synced to the client since they are assumed to be
|
||||
// identical on it
|
||||
|
||||
@@ -17,8 +17,6 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
/// </summary>
|
||||
IMechanism? Mechanism { get; }
|
||||
|
||||
void Update(float frameTime);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the containing <see cref="IBodyPart"/> is attached to a
|
||||
/// <see cref="IBody"/>.
|
||||
|
||||
@@ -210,7 +210,7 @@ namespace Content.Shared.GameObjects.Components.Body.Part
|
||||
return SurgeryDataComponent?.CanAttachBodyPart(part) ?? false;
|
||||
}
|
||||
|
||||
public bool CanAddMechanism(IMechanism mechanism)
|
||||
public virtual bool CanAddMechanism(IMechanism mechanism)
|
||||
{
|
||||
DebugTools.AssertNotNull(mechanism);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user