Fix mechanism events not being called properly, add test (#2279)

* Add mechanism events when added/removed to/from body/parts

* Change old usages

* Add TODO

* Remove BodyExtensions and IHasBody

* Remove unnecessary extensions and fix wrong event call in mechanism behavior component

* Complete test and fix event calls
This commit is contained in:
DrSmugleaf
2020-10-17 12:26:39 +02:00
committed by GitHub
parent 05a78f117d
commit 101fa9e466
25 changed files with 365 additions and 154 deletions

View File

@@ -1,5 +1,6 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Body.Behavior;
using Content.Shared.GameObjects.Components.Body.Part;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
@@ -39,12 +40,26 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
if (old != null)
{
OnRemovedFromPart(old);
if (old.Body == null)
{
RemovedFromPart(old);
}
else
{
RemovedFromPartInBody(old.Body, old);
}
}
if (value != null)
{
OnAddedToPart();
if (value.Body == null)
{
AddedToPart();
}
else
{
AddedToPartInBody();
}
}
}
}
@@ -94,7 +109,7 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
OnAddedToBody();
foreach (var behavior in Owner.GetMechanismBehaviors())
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.AddedToBody();
}
@@ -104,7 +119,7 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
{
OnRemovedFromBody(old);
foreach (var behavior in Owner.GetMechanismBehaviors())
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.RemovedFromBody(old);
}
@@ -117,23 +132,12 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
Owner.Transform.AttachParent(Part!.Owner);
OnAddedToPart();
foreach (var behavior in Owner.GetMechanismBehaviors())
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.AddedToPart();
}
}
public void RemovedFromPart(IBodyPart old)
{
Owner.Transform.AttachToGridOrMap();
OnRemovedFromPart(old);
foreach (var behavior in Owner.GetMechanismBehaviors())
{
behavior.RemovedFromPart(old);
}
}
public void AddedToPartInBody()
{
DebugTools.AssertNotNull(Body);
@@ -142,18 +146,29 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
Owner.Transform.AttachParent(Part!.Owner);
OnAddedToPartInBody();
foreach (var behavior in Owner.GetMechanismBehaviors())
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.AddedToPartInBody();
}
}
public void RemovedFromPart(IBodyPart old)
{
Owner.Transform.AttachToGridOrMap();
OnRemovedFromPart(old);
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.RemovedFromPart(old);
}
}
public void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart)
{
Owner.Transform.AttachToGridOrMap();
OnRemovedFromPartInBody();
foreach (var behavior in Owner.GetMechanismBehaviors())
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
{
behavior.RemovedFromPartInBody(oldBody, oldPart);
}