public void TestSlices() { EntityRegistry registry = new EntityRegistry(5000); TagPosition sliceTagPosition = new TagPosition(); registry.SetupSlice(ref sliceTagPosition); ParentedPosition sliceParentedPosition = new ParentedPosition(); registry.SetupSlice(ref sliceParentedPosition); UnparentedPosition sliceUnparentedPosition = new UnparentedPosition(); registry.SetupSlice(ref sliceUnparentedPosition); List <Entity> entities = new List <Entity>(registry.Capacity); for (int i = 0; i < registry.Capacity; ++i) { Entity entity = registry.Create(); entities.Add(entity); registry.AddComponent(entity, new PositionComponent(i, i)); } Assert.AreEqual(0, sliceTagPosition.Positions.Count); Assert.AreEqual(0, sliceParentedPosition.Positions.Count); Assert.AreEqual((int)registry.Capacity, sliceUnparentedPosition.Positions.Count); for (int i = 0; i < registry.Capacity; i += 2) { Entity entity = entities[i]; registry.AddComponent(entity, new ParentedComponent(entities[i + 1])); } Assert.AreEqual(0, sliceTagPosition.Positions.Count); Assert.AreEqual(2500, sliceParentedPosition.Positions.Count); Assert.AreEqual(2500, sliceUnparentedPosition.Positions.Count); foreach (Entity entity in entities) { registry.Destroy(entity); } entities.Clear(); Assert.AreEqual(0, sliceTagPosition.Positions.Count); Assert.AreEqual(0, sliceParentedPosition.Positions.Count); Assert.AreEqual(0, sliceUnparentedPosition.Positions.Count); }
public void TestSliceInjection() { Random randomizer = new Random(); const int kMaxEntities = 2000; EntityRegistry registry = new EntityRegistry(kMaxEntities); ParentedPosition parentedPositionSlice = new ParentedPosition(); registry.SetupSlice(ref parentedPositionSlice); List <Entity> activeEntities = new List <Entity>(kMaxEntities); //Create and destroy various waves of entities to ensure things are shuffled enough int shufles = 10; while (--shufles >= 0) { int randomCreate = randomizer.Next(kMaxEntities - activeEntities.Count); Entity previousEntity = Entity.NullEntity; for (int i = 0; i < randomCreate; ++i) { Entity entity = registry.Create(); if (i % 2 == 0) { registry.AddComponent(entity, new PositionComponent(i, i)); } if (i % 3 == 0) { registry.AddComponent(entity, new ParentedComponent(previousEntity)); } activeEntities.Add(entity); previousEntity = entity; } int randomDestroy = randomizer.Next(activeEntities.Count); for (int i = 0; i < randomDestroy; ++i) { int randomIndex = randomizer.Next(activeEntities.Count); registry.Destroy(activeEntities[randomIndex]); activeEntities.RemoveAt(randomIndex); } for (int i = 0; i < parentedPositionSlice.Entities.Count; ++i) { Entity entity = parentedPositionSlice.Entities[i]; Assert.IsTrue(registry.IsValid(entity)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity)); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity), parentedPositionSlice.Positions[i]); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity)); Assert.AreEqual(registry.GetComponent <ParentedComponent>(entity), parentedPositionSlice.Parents[i]); } for (int i = 0; i < parentedPositionSlice.Entities.Count; ++i) { Entity entity = parentedPositionSlice.Entities[i]; PositionComponent position = parentedPositionSlice.Positions[i]; PositionComponent newPosition = new PositionComponent(position.X + 1, position.Y + 1); registry.SetComponent(entity, newPosition); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity)); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity), newPosition); Assert.AreEqual(newPosition, parentedPositionSlice.Positions[i]); } } }