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

@@ -3,10 +3,8 @@ using Content.Server.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Part;
using Content.Shared.GameObjects.Components.Rotation;
using Content.Shared.GameObjects.EntitySystems;
using NUnit.Framework;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
@@ -36,7 +34,7 @@ namespace Content.IntegrationTests.Tests.Body
var entityManager = IoCManager.Resolve<IEntityManager>();
var human = entityManager.SpawnEntity("HumanMob_Content", MapCoordinates.Nullspace);
Assert.That(human.TryGetBody(out var body));
Assert.That(human.TryGetComponent(out IBody body));
Assert.That(human.TryGetComponent(out appearance));
Assert.That(!appearance.TryGetData(RotationVisuals.RotationState, out RotationState _));

View File

@@ -6,6 +6,7 @@ using Content.Server.GameObjects.Components.Body.Behavior;
using Content.Server.GameObjects.Components.Body.Circulatory;
using Content.Server.GameObjects.Components.Metabolism;
using Content.Shared.Atmos;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using NUnit.Framework;
using Robust.Server.Interfaces.Maps;
@@ -36,9 +37,10 @@ namespace Content.IntegrationTests.Tests.Body
var human = entityManager.SpawnEntity("HumanMob_Content", MapCoordinates.Nullspace);
Assert.True(human.TryGetMechanismBehaviors(out List<LungBehaviorComponent> lungs));
Assert.That(human.TryGetComponent(out IBody body));
Assert.That(body.TryGetMechanismBehaviors(out List<LungBehaviorComponent> lungs));
Assert.That(lungs.Count, Is.EqualTo(1));
Assert.True(human.TryGetComponent(out BloodstreamComponent bloodstream));
Assert.That(human.TryGetComponent(out BloodstreamComponent bloodstream));
var gas = new GasMixture(1);
@@ -137,7 +139,8 @@ namespace Content.IntegrationTests.Tests.Body
var coordinates = new EntityCoordinates(grid.GridEntityId, center);
human = entityManager.SpawnEntity("HumanMob_Content", coordinates);
Assert.True(human.HasMechanismBehavior<LungBehaviorComponent>());
Assert.True(human.TryGetComponent(out IBody body));
Assert.True(body.HasMechanismBehavior<LungBehaviorComponent>());
Assert.True(human.TryGetComponent(out metabolism));
Assert.False(metabolism.Suffocating);
});

View File

@@ -0,0 +1,219 @@
#nullable enable
using System.Linq;
using System.Threading.Tasks;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Behavior;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using Content.Shared.GameObjects.Components.Body.Part;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.IntegrationTests.Tests.Body
{
[TestFixture]
[TestOf(typeof(SharedBodyComponent))]
[TestOf(typeof(SharedBodyPartComponent))]
[TestOf(typeof(SharedMechanismComponent))]
[TestOf(typeof(MechanismBehaviorComponent))]
public class MechanismBehaviorEventsTest : ContentIntegrationTest
{
[RegisterComponent]
private class TestBehaviorComponent : MechanismBehaviorComponent
{
public override string Name => nameof(MechanismBehaviorEventsTest) + "TestBehavior";
public bool WasAddedToBody;
public bool WasAddedToPart;
public bool WasAddedToPartInBody;
public bool WasRemovedFromBody;
public bool WasRemovedFromPart;
public bool WasRemovedFromPartInBody;
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;
}
public bool NoRemoved()
{
return !WasRemovedFromBody && !WasRemovedFromPart && !WasRemovedFromPartInBody;
}
public void ResetAdded()
{
WasAddedToBody = false;
WasAddedToPart = false;
WasAddedToPartInBody = false;
}
public void ResetRemoved()
{
WasRemovedFromBody = false;
WasRemovedFromPart = false;
WasRemovedFromPartInBody = false;
}
public void ResetAll()
{
ResetAdded();
ResetRemoved();
}
protected override void OnAddedToBody()
{
base.OnAddedToBody();
WasAddedToBody = true;
}
protected override void OnAddedToPart()
{
base.OnAddedToPart();
WasAddedToPart = true;
}
protected override void OnAddedToPartInBody()
{
base.OnAddedToPartInBody();
WasAddedToPartInBody = true;
}
protected override void OnRemovedFromBody(IBody old)
{
base.OnRemovedFromBody(old);
WasRemovedFromBody = true;
}
protected override void OnRemovedFromPart(IBodyPart old)
{
base.OnRemovedFromPart(old);
WasRemovedFromPart = true;
}
protected override void OnRemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart)
{
base.OnRemovedFromPartInBody(oldBody, oldPart);
WasRemovedFromPartInBody = true;
}
}
[Test]
public async Task EventsTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IComponentFactory>().Register<TestBehaviorComponent>();
}
});
await server.WaitAssertion(() =>
{
var mapManager = IoCManager.Resolve<IMapManager>();
var mapId = new MapId(0);
mapManager.CreateNewMapEntity(mapId);
var entityManager = IoCManager.Resolve<IEntityManager>();
var human = entityManager.SpawnEntity("HumanMob_Content", MapCoordinates.Nullspace);
Assert.That(human.TryGetComponent(out IBody? body));
Assert.NotNull(body);
var centerPart = body!.CenterPart();
Assert.NotNull(centerPart);
Assert.That(body.TryGetSlot(centerPart!, out var centerSlot));
Assert.NotNull(centerSlot);
var mechanism = centerPart!.Mechanisms.First();
Assert.NotNull(mechanism);
var component = mechanism.Owner.AddComponent<TestBehaviorComponent>();
Assert.False(component.WasAddedToBody);
Assert.False(component.WasAddedToPart);
Assert.That(component.WasAddedToPartInBody);
Assert.That(component.NoRemoved);
component.ResetAll();
Assert.That(component.NoAdded);
Assert.That(component.NoRemoved);
centerPart.RemoveMechanism(mechanism);
Assert.That(component.NoAdded);
Assert.False(component.WasRemovedFromBody);
Assert.False(component.WasRemovedFromPart);
Assert.That(component.WasRemovedFromPartInBody);
component.ResetAll();
centerPart.TryAddMechanism(mechanism, true);
Assert.False(component.WasAddedToBody);
Assert.False(component.WasAddedToPart);
Assert.That(component.WasAddedToPartInBody);
Assert.That(component.NoRemoved());
component.ResetAll();
body.RemovePart(centerPart, true);
Assert.That(component.NoAdded);
Assert.That(component.WasRemovedFromBody);
Assert.False(component.WasRemovedFromPart);
Assert.False(component.WasRemovedFromPartInBody);
component.ResetAll();
centerPart.RemoveMechanism(mechanism);
Assert.That(component.NoAdded);
Assert.False(component.WasRemovedFromBody);
Assert.That(component.WasRemovedFromPart);
Assert.False(component.WasRemovedFromPartInBody);
component.ResetAll();
centerPart.TryAddMechanism(mechanism, true);
Assert.False(component.WasAddedToBody);
Assert.That(component.WasAddedToPart);
Assert.False(component.WasAddedToPartInBody);
Assert.That(component.NoRemoved);
component.ResetAll();
body.TryAddPart(centerSlot!, centerPart, true);
Assert.That(component.WasAddedToBody);
Assert.False(component.WasAddedToPart);
Assert.False(component.WasAddedToPartInBody);
Assert.That(component.NoRemoved);
});
}
}
}