using System.Numerics;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Reflection;
using Robust.UnitTesting.Server;
using Is = Robust.UnitTesting.Is;
namespace Robust.Server.IntegrationTests.GameObjects;
[TestFixture]
internal sealed partial class ComponentMapInitTest
{
///
/// Asserts whether a component added after an entity has fully initialized has MapInit called.
///
[Test]
public void ComponentMapInit()
{
var simFactory = RobustServerSimulation.NewSimulation();
simFactory.RegisterComponents(fac =>
{
fac.RegisterClass();
}).RegisterEntitySystems(fac =>
{
fac.LoadExtraSystemType();
});
var sim = simFactory.InitializeInstance();
var entManager = sim.Resolve();
var mapSystem = entManager.System();
mapSystem.CreateMap(out var mapId);
var ent = entManager.SpawnEntity(null, new MapCoordinates(Vector2.Zero, mapId));
Assert.That(entManager.GetComponent(ent).EntityLifeStage, Is.EqualTo(EntityLifeStage.MapInitialized));
var comp = entManager.AddComponent(ent);
Assert.That(comp.Count, Is.EqualTo(1));
mapSystem.DeleteMap(mapId);
}
[Reflect(false)]
private sealed class MapInitTestSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnMapInitTestMapInit);
}
private void OnMapInitTestMapInit(EntityUid uid, MapInitTestComponent component, MapInitEvent args)
{
component.Count += 1;
}
}
[Reflect(false)]
private sealed partial class MapInitTestComponent : Component
{
public int Count = 0;
}
}