diff --git a/Content.Client/GameObjects/Components/Body/Mechanism/MechanismComponent.cs b/Content.Client/GameObjects/Components/Body/Mechanism/MechanismComponent.cs index fed6fb82e0..191ee99c96 100644 --- a/Content.Client/GameObjects/Components/Body/Mechanism/MechanismComponent.cs +++ b/Content.Client/GameObjects/Components/Body/Mechanism/MechanismComponent.cs @@ -1,7 +1,5 @@ #nullable enable using Content.Shared.GameObjects.Components.Body.Mechanism; -using Content.Shared.GameObjects.Components.Body.Part; -using Robust.Client.Interfaces.GameObjects.Components; using Robust.Shared.GameObjects; namespace Content.Client.GameObjects.Components.Body.Mechanism @@ -9,26 +7,5 @@ namespace Content.Client.GameObjects.Components.Body.Mechanism [RegisterComponent] [ComponentReference(typeof(SharedMechanismComponent))] [ComponentReference(typeof(IMechanism))] - public class MechanismComponent : SharedMechanismComponent - { - protected override void OnAddedToPart() - { - base.OnAddedToPart(); - - if (Owner.TryGetComponent(out ISpriteComponent? sprite)) - { - sprite.Visible = false; - } - } - - protected override void OnRemovedFromPart(IBodyPart old) - { - base.OnRemovedFromPart(old); - - if (Owner.TryGetComponent(out ISpriteComponent? sprite)) - { - sprite.Visible = true; - } - } - } + public class MechanismComponent : SharedMechanismComponent { } } diff --git a/Content.IntegrationTests/Tests/Body/MechanismBehaviorEventsTest.cs b/Content.IntegrationTests/Tests/Body/MechanismBehaviorEventsTest.cs index 9c28ff7666..e3c571496e 100644 --- a/Content.IntegrationTests/Tests/Body/MechanismBehaviorEventsTest.cs +++ b/Content.IntegrationTests/Tests/Body/MechanismBehaviorEventsTest.cs @@ -35,16 +35,6 @@ namespace Content.IntegrationTests.Tests.Body public override void Update(float frameTime) { } - public bool AllAdded() - { - return WasAddedToBody && WasAddedToPart && WasAddedToPartInBody; - } - - public bool AllRemoved() - { - return WasRemovedFromBody && WasRemovedFromPart && WasRemovedFromPartInBody; - } - public bool NoAdded() { return !WasAddedToBody && !WasAddedToPart && !WasAddedToPartInBody; @@ -75,23 +65,23 @@ namespace Content.IntegrationTests.Tests.Body ResetRemoved(); } - protected override void OnAddedToBody() + protected override void OnAddedToBody(IBody body) { - base.OnAddedToBody(); + base.OnAddedToBody(body); WasAddedToBody = true; } - protected override void OnAddedToPart() + protected override void OnAddedToPart(IBodyPart part) { - base.OnAddedToPart(); + base.OnAddedToPart(part); WasAddedToPart = true; } - protected override void OnAddedToPartInBody() + protected override void OnAddedToPartInBody(IBody body, IBodyPart part) { - base.OnAddedToPartInBody(); + base.OnAddedToPartInBody(body, part); WasAddedToPartInBody = true; } @@ -110,7 +100,7 @@ namespace Content.IntegrationTests.Tests.Body WasRemovedFromPart = true; } - protected override void OnRemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart) + protected override void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { base.OnRemovedFromPartInBody(oldBody, oldPart); diff --git a/Content.Server/GameObjects/Components/Body/Behavior/BrainBehaviorComponent.cs b/Content.Server/GameObjects/Components/Body/Behavior/BrainBehaviorComponent.cs index 337cb148ce..0dff9e5a98 100644 --- a/Content.Server/GameObjects/Components/Body/Behavior/BrainBehaviorComponent.cs +++ b/Content.Server/GameObjects/Components/Body/Behavior/BrainBehaviorComponent.cs @@ -17,25 +17,25 @@ namespace Content.Server.GameObjects.Components.Body.Behavior { public override string Name => "Brain"; - protected override void OnAddedToBody() + protected override void OnAddedToBody(IBody body) { - base.OnAddedToBody(); + base.OnAddedToBody(body); - HandleMind(Body!.Owner, Owner); + HandleMind(body.Owner, Owner); } - protected override void OnAddedToPart() + protected override void OnAddedToPart(IBodyPart part) { - base.OnAddedToPart(); + base.OnAddedToPart(part); - HandleMind(Part!.Owner, Owner); + HandleMind(part.Owner, Owner); } - protected override void OnAddedToPartInBody() + protected override void OnAddedToPartInBody(IBody body, IBodyPart part) { - base.OnAddedToPartInBody(); + base.OnAddedToPartInBody(body, part); - HandleMind(Body!.Owner, Owner); + HandleMind(body.Owner, Owner); } protected override void OnRemovedFromBody(IBody old) @@ -52,16 +52,16 @@ namespace Content.Server.GameObjects.Components.Body.Behavior HandleMind(Owner, old.Owner); } - protected override void OnRemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart) + protected override void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { base.OnRemovedFromPartInBody(oldBody, oldPart); - HandleMind(oldBody!.Owner, Owner); + HandleMind(oldBody.Owner, Owner); } private void HandleMind(IEntity newEntity, IEntity oldEntity) { - var newMind = newEntity.EnsureComponent(); + newEntity.EnsureComponent(); var oldMind = oldEntity.EnsureComponent(); oldMind.Mind?.TransferTo(newEntity); diff --git a/Content.Server/GameObjects/Components/Body/MechanismComponent.cs b/Content.Server/GameObjects/Components/Body/MechanismComponent.cs index 6ac52501c5..9a419c2e86 100644 --- a/Content.Server/GameObjects/Components/Body/MechanismComponent.cs +++ b/Content.Server/GameObjects/Components/Body/MechanismComponent.cs @@ -157,25 +157,5 @@ namespace Content.Server.GameObjects.Components.Body break; } } - - protected override void OnAddedToPart() - { - base.OnAddedToPart(); - - if (Owner.TryGetComponent(out SpriteComponent? sprite)) - { - sprite.Visible = false; - } - } - - protected override void OnRemovedFromPart(IBodyPart old) - { - base.OnRemovedFromPart(old); - - if (Owner.TryGetComponent(out SpriteComponent? sprite)) - { - sprite.Visible = true; - } - } } } diff --git a/Content.Shared/GameObjects/Components/Body/Behavior/IMechanismBehavior.cs b/Content.Shared/GameObjects/Components/Body/Behavior/IMechanismBehavior.cs index edbf8e5d42..2e38200f58 100644 --- a/Content.Shared/GameObjects/Components/Body/Behavior/IMechanismBehavior.cs +++ b/Content.Shared/GameObjects/Components/Body/Behavior/IMechanismBehavior.cs @@ -23,7 +23,10 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, attaching a head with a brain inside to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToBody(); + /// + /// The body that the containing was added to. + /// + void AddedToBody(IBody body); /// /// Called when the parent is @@ -32,7 +35,10 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, adding a brain to a dismembered head. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToPart(); + /// + /// The part that the containing was added to. + /// + void AddedToPart(IBodyPart part); /// /// Called when the parent is added to a @@ -40,7 +46,13 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, adding a brain to a head that is attached to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToPartInBody(); + /// + /// The body that the containing was added to. + /// + /// + /// The part that the containing was added to. + /// + void AddedToPartInBody(IBody body, IBodyPart part); /// /// Called when the parent is removed from a @@ -48,6 +60,9 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, removing a head with a brain inside from a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// + /// + /// The body that the containing was removed from. + /// void RemovedFromBody(IBody old); /// @@ -57,6 +72,9 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, removing a brain from a dismembered head. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// + /// + /// The part that the containing was removed from. + /// void RemovedFromPart(IBodyPart old); /// @@ -65,6 +83,12 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior /// For instance, removing a brain from a head that is attached to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart); + /// + /// The body that the containing was removed from. + /// + /// + /// The part that the containing was removed from. + /// + void RemovedFromPartInBody(IBody oldBody, IBodyPart oldPart); } } diff --git a/Content.Shared/GameObjects/Components/Body/Behavior/MechanismBehaviorComponent.cs b/Content.Shared/GameObjects/Components/Body/Behavior/MechanismBehaviorComponent.cs index 27b31b0d9d..f4a0683eca 100644 --- a/Content.Shared/GameObjects/Components/Body/Behavior/MechanismBehaviorComponent.cs +++ b/Content.Shared/GameObjects/Components/Body/Behavior/MechanismBehaviorComponent.cs @@ -2,6 +2,7 @@ using Content.Shared.GameObjects.Components.Body.Mechanism; using Content.Shared.GameObjects.Components.Body.Part; using Robust.Shared.GameObjects; +using Robust.Shared.Utility; namespace Content.Shared.GameObjects.Components.Body.Behavior { @@ -24,56 +25,78 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior if (Body == null) { - AddedToPart(); + AddedToPart(Part); } else { - AddedToPartInBody(); + AddedToPartInBody(Body, Part); } } public abstract void Update(float frameTime); - public void AddedToBody() + public void AddedToBody(IBody body) { - OnAddedToBody(); + DebugTools.AssertNotNull(Body); + DebugTools.AssertNotNull(body); + + OnAddedToBody(body); } - public void AddedToPart() + public void AddedToPart(IBodyPart part) { - OnAddedToPart(); + DebugTools.AssertNotNull(Part); + DebugTools.AssertNotNull(part); + + OnAddedToPart(part); + } + + public void AddedToPartInBody(IBody body, IBodyPart part) + { + DebugTools.AssertNotNull(Body); + DebugTools.AssertNotNull(body); + DebugTools.AssertNotNull(Part); + DebugTools.AssertNotNull(part); + + OnAddedToPartInBody(body, part); } public void RemovedFromBody(IBody old) { + DebugTools.AssertNull(Body); + DebugTools.AssertNotNull(old); + OnRemovedFromBody(old); } public void RemovedFromPart(IBodyPart old) { + DebugTools.AssertNull(Part); + DebugTools.AssertNotNull(old); + OnRemovedFromPart(old); } - public void AddedToPartInBody() + public void RemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { - OnAddedToPartInBody(); - } + DebugTools.AssertNull(Body); + DebugTools.AssertNull(Part); + DebugTools.AssertNotNull(oldBody); + DebugTools.AssertNotNull(oldPart); - public void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart) - { OnRemovedFromPartInBody(oldBody, oldPart); } - protected virtual void OnAddedToBody() { } + protected virtual void OnAddedToBody(IBody body) { } - protected virtual void OnAddedToPart() { } + protected virtual void OnAddedToPart(IBodyPart part) { } + + protected virtual void OnAddedToPartInBody(IBody body, IBodyPart part) { } protected virtual void OnRemovedFromBody(IBody old) { } protected virtual void OnRemovedFromPart(IBodyPart old) { } - protected virtual void OnAddedToPartInBody() { } - - protected virtual void OnRemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart) { } + protected virtual void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { } } } diff --git a/Content.Shared/GameObjects/Components/Body/Mechanism/IMechanism.cs b/Content.Shared/GameObjects/Components/Body/Mechanism/IMechanism.cs index 871f534d00..092b838819 100644 --- a/Content.Shared/GameObjects/Components/Body/Mechanism/IMechanism.cs +++ b/Content.Shared/GameObjects/Components/Body/Mechanism/IMechanism.cs @@ -62,7 +62,10 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, attaching a head with a brain inside to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToBody(); + /// + /// The body that this was added to. + /// + void AddedToBody(IBody body); /// /// Called when the parent is @@ -71,7 +74,10 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, adding a brain to a dismembered head. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToPart(); + /// + /// The part that this was added to. + /// + void AddedToPart(IBodyPart part); /// /// Called when the parent is added to a @@ -79,7 +85,13 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, adding a brain to a head that is attached to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void AddedToPartInBody(); + /// + /// The body that this was added to. + /// + /// + /// The part that this was added to. + /// + void AddedToPartInBody(IBody body, IBodyPart part); /// /// Called when the parent is removed from a @@ -87,6 +99,9 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, removing a head with a brain inside from a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// + /// + /// The body that this was removed from. + /// void RemovedFromBody(IBody old); /// @@ -96,6 +111,9 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, removing a brain from a dismembered head. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// + /// + /// The part that this was removed from. + /// void RemovedFromPart(IBodyPart old); /// @@ -104,6 +122,12 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism /// For instance, removing a brain from a head that is attached to a body. /// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE! /// - void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart); + /// + /// The body that this was removed from. + /// + /// + /// The part that this was removed from. + /// + void RemovedFromPartInBody(IBody oldBody, IBodyPart oldPart); } } diff --git a/Content.Shared/GameObjects/Components/Body/Mechanism/SharedMechanismComponent.cs b/Content.Shared/GameObjects/Components/Body/Mechanism/SharedMechanismComponent.cs index ce5aef57c8..cfd6d8e2f9 100644 --- a/Content.Shared/GameObjects/Components/Body/Mechanism/SharedMechanismComponent.cs +++ b/Content.Shared/GameObjects/Components/Body/Mechanism/SharedMechanismComponent.cs @@ -15,13 +15,9 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism public override string Name => "Mechanism"; private IBodyPart? _part; - protected readonly Dictionary OptionsCache = new Dictionary(); - protected IBody? BodyCache; - protected int IdHash; - protected IEntity? PerformerCache; public IBody? Body => Part?.Body; @@ -55,11 +51,11 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism { if (value.Body == null) { - AddedToPart(); + AddedToPart(value); } else { - AddedToPartInBody(); + AddedToPartInBody(value.Body, value); } } } @@ -104,20 +100,54 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism serializer.DataField(this, m => m.Compatibility, "compatibility", BodyPartCompatibility.Universal); } - public void AddedToBody() + public void AddedToBody(IBody body) { DebugTools.AssertNotNull(Body); + DebugTools.AssertNotNull(body); - OnAddedToBody(); + OnAddedToBody(body); foreach (var behavior in Owner.GetAllComponents()) { - behavior.AddedToBody(); + behavior.AddedToBody(body); + } + } + + public void AddedToPart(IBodyPart part) + { + DebugTools.AssertNotNull(Part); + DebugTools.AssertNotNull(part); + + Owner.Transform.AttachParent(part.Owner); + OnAddedToPart(part); + + foreach (var behavior in Owner.GetAllComponents().ToArray()) + { + behavior.AddedToPart(part); + } + } + + public void AddedToPartInBody(IBody body, IBodyPart part) + { + DebugTools.AssertNotNull(Body); + DebugTools.AssertNotNull(body); + DebugTools.AssertNotNull(Part); + DebugTools.AssertNotNull(part); + + Owner.Transform.AttachParent(part.Owner); + OnAddedToPartInBody(body, part); + + foreach (var behavior in Owner.GetAllComponents()) + { + behavior.AddedToPartInBody(body, part); } } public void RemovedFromBody(IBody old) { + DebugTools.AssertNull(Body); + DebugTools.AssertNotNull(old); + OnRemovedFromBody(old); foreach (var behavior in Owner.GetAllComponents()) @@ -126,35 +156,11 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism } } - public void AddedToPart() - { - DebugTools.AssertNotNull(Part); - - Owner.Transform.AttachParent(Part!.Owner); - OnAddedToPart(); - - foreach (var behavior in Owner.GetAllComponents().ToArray()) - { - behavior.AddedToPart(); - } - } - - public void AddedToPartInBody() - { - DebugTools.AssertNotNull(Body); - DebugTools.AssertNotNull(Part); - - Owner.Transform.AttachParent(Part!.Owner); - OnAddedToPartInBody(); - - foreach (var behavior in Owner.GetAllComponents()) - { - behavior.AddedToPartInBody(); - } - } - public void RemovedFromPart(IBodyPart old) { + DebugTools.AssertNull(Part); + DebugTools.AssertNotNull(old); + Owner.Transform.AttachToGridOrMap(); OnRemovedFromPart(old); @@ -164,10 +170,15 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism } } - public void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart) + public void RemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { + DebugTools.AssertNull(Body); + DebugTools.AssertNotNull(oldBody); + DebugTools.AssertNull(Part); + DebugTools.AssertNotNull(oldPart); + Owner.Transform.AttachToGridOrMap(); - OnRemovedFromPartInBody(); + OnRemovedFromPartInBody(oldBody, oldPart); foreach (var behavior in Owner.GetAllComponents()) { @@ -175,16 +186,16 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism } } - protected virtual void OnAddedToBody() { } + protected virtual void OnAddedToBody(IBody body) { } + + protected virtual void OnAddedToPart(IBodyPart part) { } + + protected virtual void OnAddedToPartInBody(IBody body, IBodyPart part) { } protected virtual void OnRemovedFromBody(IBody old) { } - protected virtual void OnAddedToPart() { } - protected virtual void OnRemovedFromPart(IBodyPart old) { } - protected virtual void OnAddedToPartInBody() { } - - protected virtual void OnRemovedFromPartInBody() { } + protected virtual void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { } } } diff --git a/Content.Shared/GameObjects/Components/Body/Part/SharedBodyPartComponent.cs b/Content.Shared/GameObjects/Components/Body/Part/SharedBodyPartComponent.cs index 66dc88e4cd..b915e4112b 100644 --- a/Content.Shared/GameObjects/Components/Body/Part/SharedBodyPartComponent.cs +++ b/Content.Shared/GameObjects/Components/Body/Part/SharedBodyPartComponent.cs @@ -50,7 +50,7 @@ namespace Content.Shared.GameObjects.Components.Body.Part if (value != null) { - AddedToBody(); + AddedToBody(value); } } } @@ -284,14 +284,14 @@ namespace Content.Shared.GameObjects.Components.Body.Part return true; } - private void AddedToBody() + private void AddedToBody(IBody body) { Owner.Transform.AttachParent(Body!.Owner); - OnAddedToBody(); + OnAddedToBody(body); foreach (var mechanism in _mechanisms) { - mechanism.AddedToBody(); + mechanism.AddedToBody(body); } } @@ -310,7 +310,7 @@ namespace Content.Shared.GameObjects.Components.Body.Part } } - protected virtual void OnAddedToBody() { } + protected virtual void OnAddedToBody(IBody body) { } protected virtual void OnRemovedFromBody(IBody old) { } }