Make body parts properly use containers for mechanisms (#2290)

This commit is contained in:
DrSmugleaf
2020-10-18 11:20:10 +02:00
committed by GitHub
parent 34e0330187
commit 32138958ce
4 changed files with 19 additions and 19 deletions

View File

@@ -17,32 +17,33 @@ namespace Content.Server.GameObjects.Components.Body
[ComponentReference(typeof(IBody))] [ComponentReference(typeof(IBody))]
public class BodyComponent : SharedBodyComponent, IRelayMoveInput public class BodyComponent : SharedBodyComponent, IRelayMoveInput
{ {
private Container _container = default!; private Container _partContainer = default!;
protected override bool CanAddPart(string slot, IBodyPart part) 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) protected override void OnAddPart(string slot, IBodyPart part)
{ {
base.OnAddPart(slot, part); base.OnAddPart(slot, part);
_container.Insert(part.Owner); _partContainer.Insert(part.Owner);
} }
protected override void OnRemovePart(string slot, IBodyPart part) protected override void OnRemovePart(string slot, IBodyPart part)
{ {
base.OnRemovePart(slot, part); base.OnRemovePart(slot, part);
_container.ForceRemove(part.Owner); _partContainer.ForceRemove(part.Owner);
} }
public override void Initialize() public override void Initialize()
{ {
base.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) foreach (var (slot, partId) in PartIds)
{ {

View File

@@ -9,6 +9,7 @@ using Content.Shared.GameObjects.Components.Body.Surgery;
using Content.Shared.Interfaces; using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.Components.UserInterface; using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects; using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player; using Robust.Server.Interfaces.Player;
@@ -26,39 +27,39 @@ namespace Content.Server.GameObjects.Components.Body.Part
public class BodyPartComponent : SharedBodyPartComponent, IAfterInteract public class BodyPartComponent : SharedBodyPartComponent, IAfterInteract
{ {
private readonly Dictionary<int, object> _optionsCache = new Dictionary<int, object>(); private readonly Dictionary<int, object> _optionsCache = new Dictionary<int, object>();
private IBody? _owningBodyCache; private IBody? _owningBodyCache;
private int _idHash; private int _idHash;
private IEntity? _surgeonCache; private IEntity? _surgeonCache;
private Container _mechanismContainer = default!;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key); [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) protected override void OnAddMechanism(IMechanism mechanism)
{ {
base.OnAddMechanism(mechanism); base.OnAddMechanism(mechanism);
if (mechanism.Owner.TryGetComponent(out SpriteComponent? sprite)) _mechanismContainer.Insert(mechanism.Owner);
{
sprite.Visible = false;
}
} }
protected override void OnRemoveMechanism(IMechanism mechanism) protected override void OnRemoveMechanism(IMechanism mechanism)
{ {
base.OnRemoveMechanism(mechanism); base.OnRemoveMechanism(mechanism);
if (mechanism.Owner.TryGetComponent(out SpriteComponent? sprite)) _mechanismContainer.Remove(mechanism.Owner);
{
sprite.Visible = true;
}
} }
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_mechanismContainer = ContainerManagerComponent.Ensure<Container>($"{Name}-{nameof(BodyPartComponent)}", Owner);
// This is ran in Startup as entities spawned in Initialize // This is ran in Startup as entities spawned in Initialize
// are not synced to the client since they are assumed to be // are not synced to the client since they are assumed to be
// identical on it // identical on it

View File

@@ -17,8 +17,6 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
/// </summary> /// </summary>
IMechanism? Mechanism { get; } IMechanism? Mechanism { get; }
void Update(float frameTime);
/// <summary> /// <summary>
/// Called when the containing <see cref="IBodyPart"/> is attached to a /// Called when the containing <see cref="IBodyPart"/> is attached to a
/// <see cref="IBody"/>. /// <see cref="IBody"/>.

View File

@@ -210,7 +210,7 @@ namespace Content.Shared.GameObjects.Components.Body.Part
return SurgeryDataComponent?.CanAttachBodyPart(part) ?? false; return SurgeryDataComponent?.CanAttachBodyPart(part) ?? false;
} }
public bool CanAddMechanism(IMechanism mechanism) public virtual bool CanAddMechanism(IMechanism mechanism)
{ {
DebugTools.AssertNotNull(mechanism); DebugTools.AssertNotNull(mechanism);