Add lookup reparenting tests (#3421)

This commit is contained in:
metalgearsloth
2022-11-21 23:14:08 +11:00
committed by GitHub
parent 1c3905a8f4
commit 9dd13245ef

View File

@@ -4,7 +4,9 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Systems;
using Robust.UnitTesting.Server;
@@ -13,6 +15,85 @@ namespace Robust.UnitTesting.Shared.Physics;
[TestFixture]
public sealed class Broadphase_Test
{
/// <summary>
/// If we reparent a sundries entity to another broadphase does it correctly update.
/// </summary>
[Test]
public void ReparentSundries()
{
var sim = RobustServerSimulation.NewSimulation().InitializeInstance();
var entManager = sim.Resolve<IEntityManager>();
var mapManager = sim.Resolve<IMapManager>();
var mapId = mapManager.CreateMap();
var mapEnt = mapManager.GetMapEntityId(mapId);
var grid = mapManager.CreateGrid(mapId);
grid.SetTile(Vector2i.Zero, new Tile(1));
Assert.That(entManager.HasComponent<BroadphaseComponent>(grid.GridEntityId));
var broadphase = entManager.GetComponent<BroadphaseComponent>(grid.GridEntityId);
var ent = entManager.SpawnEntity(null, new EntityCoordinates(grid.GridEntityId, new Vector2(0.5f, 0.5f)));
var xform = entManager.GetComponent<TransformComponent>(ent);
Assert.That(broadphase.SundriesTree, Does.Contain(ent));
var broadphaseData = xform.Broadphase;
Assert.That(broadphaseData!.Value.Uid, Is.EqualTo(grid.GridEntityId));
xform.Coordinates = new EntityCoordinates(mapEnt, Vector2.One);
Assert.That(broadphase.SundriesTree, Does.Not.Contain(ent));
Assert.That(entManager.GetComponent<BroadphaseComponent>(mapEnt).SundriesTree, Does.Contain(ent));
broadphaseData = xform.Broadphase;
Assert.That(broadphaseData!.Value.Uid, Is.EqualTo(mapEnt));
}
/// <summary>
/// If we reparent a colliding physics entity to another broadphase does it correctly update.
/// </summary>
[Test]
public void ReparentBroadphase()
{
var sim = RobustServerSimulation.NewSimulation().InitializeInstance();
var entManager = sim.Resolve<IEntityManager>();
var mapManager = sim.Resolve<IMapManager>();
var fixturesSystem = entManager.EntitySysManager.GetEntitySystem<FixtureSystem>();
var physicsSystem = entManager.EntitySysManager.GetEntitySystem<SharedPhysicsSystem>();
var mapId = mapManager.CreateMap();
var mapEnt = mapManager.GetMapEntityId(mapId);
var grid = mapManager.CreateGrid(mapId);
grid.SetTile(Vector2i.Zero, new Tile(1));
Assert.That(entManager.HasComponent<BroadphaseComponent>(grid.GridEntityId));
var broadphase = entManager.GetComponent<BroadphaseComponent>(grid.GridEntityId);
var ent = entManager.SpawnEntity(null, new EntityCoordinates(grid.GridEntityId, new Vector2(0.5f, 0.5f)));
var physics = entManager.AddComponent<PhysicsComponent>(ent);
var xform = entManager.GetComponent<TransformComponent>(ent);
// If we're not collidable we're still on the sundries tree.
Assert.That(broadphase.StaticSundriesTree, Does.Contain(ent));
Assert.That(xform.Broadphase!.Value.Uid, Is.EqualTo(grid.GridEntityId));
var shape = new PolygonShape();
shape.SetAsBox(0.5f, 0.5f);
var fixture = new Fixture(physics, shape);
fixturesSystem.CreateFixture(physics, fixture);
physicsSystem.SetCanCollide(physics, true);
// Now that we're collidable should be correctly on the grid's tree.
Assert.That(fixture.ProxyCount, Is.EqualTo(1));
Assert.That(broadphase.StaticSundriesTree, Does.Not.Contain(ent));
Assert.That(broadphase.StaticTree.GetProxy(fixture.Proxies[0].ProxyId)!.Equals(fixture.Proxies[0]));
// Now check we go to the map's tree correctly.
xform.Coordinates = new EntityCoordinates(mapEnt, Vector2.One);
Assert.That(entManager.GetComponent<BroadphaseComponent>(mapEnt).StaticTree.GetProxy(fixture.Proxies[0].ProxyId)!.Equals(fixture.Proxies[0]));
Assert.That(xform.Broadphase!.Value.Uid.Equals(mapEnt));
}
/// <summary>
/// If we change a grid's map does it still remain not on the general broadphase.
/// </summary>