// BuildEntity(entity, entityWorld) public static void LoadTiledMap(EntityWorld entityWorld, string mapFile, string tilesetLocation) { // Actual map entity. Contains map data. Rendering system draws map from this entity Entity tiledMapEntity = entityWorld.CreateEntity(); tiledMapEntity.Tag = "map"; tiledMapEntity.AddComponent(new TransformComponent()); tiledMapEntity.AddComponent(new TiledMapComponent(mapFile, tilesetLocation)); TmxMap tmxMap = tiledMapEntity.GetComponent<TiledMapComponent>().Map; List<Texture2D> textures = tiledMapEntity.GetComponent<TiledMapComponent>().Textures; // Farseer world associated with map Entity farseerWorldEntity = entityWorld.CreateEntity(); farseerWorldEntity.AddComponent(new FarseerWorldComponent(9.81f)); World world = farseerWorldEntity.GetComponent<FarseerWorldComponent>().world; foreach (var objLayer in tmxMap.ObjectGroups) { // Create entities for static objects in map // TODO: If there are performance issues, can optimize all static objects into single entity if (objLayer.Name == "collision") { foreach (var tmxObj in objLayer.Objects) { Entity fixtureEntity = entityWorld.CreateEntity(); fixtureEntity.AddComponent(new FarseerFixtureComponent(tmxObj, world)); } } // Create dynamic objects // In particular starting location of player is in here if (objLayer.Name == "dynamic") { foreach (var tmxObj in objLayer.Objects) { tmxObj.Properties.Add("BodyType", "Dynamic"); Entity dynamicEntity = entityWorld.CreateEntity(); if (tmxObj.Tile != null) { dynamicEntity.AddComponent(new TransformComponent()); Texture2D texture = GetTexture(tmxMap, textures, tmxObj.Tile.Gid); dynamicEntity.AddComponent(new TextureComponent(texture)); } dynamicEntity.AddComponent(new FarseerFixtureComponent(tmxObj, world)); if (tmxObj.Name == "player") { dynamicEntity.Tag = "player"; } } } } }
public static void HybridQueueSystemTeste() { EntityWorld world = new EntityWorld(); SystemManager systemManager = world.SystemManager; HybridQueueSystemTest HybridQueueSystemTest = new ArtemisTest.HybridQueueSystemTest(); EntitySystem hs = systemManager.SetSystem(HybridQueueSystemTest, ExecutionType.Update); systemManager.InitializeAll(); List<Entity> l = new List<Entity>(); for (int i = 0; i < 100; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; et.Refresh(); l.Add(et); } for (int i = 0; i < 100; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; HybridQueueSystemTest. AddToQueue(et); l.Add(et); } int j = 0; while (HybridQueueSystemTest.QueueCount > 0) { j++; DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateSynchronous(ExecutionType.Update); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } for (int i = 0; i < 100; i++) { Debug.Assert(l[i].GetComponent<Health>().HP == 100 - (10 * j)); } for (int i = 100; i < 200; i++) { Debug.Assert(l[i].GetComponent<Health>().HP == 90); } }
/// <summary>Creates the test health entity with ID.</summary> /// <param name="entityWorld">The entity world.</param> /// <param name="id">The id.</param> /// <returns>The Entity.</returns> public static Entity CreateTestHealthEntityWithId(EntityWorld entityWorld, long id) { Entity entity = entityWorld.CreateEntity(id); entity.AddComponent(new TestHealthComponent()); entity.GetComponent<TestHealthComponent>().Points = 100; return entity; }
public void AttributesTestsMethod() { EntityWorld world = new EntityWorld(); world.PoolCleanupDelay = 1; world.InitializeAll(); Debug.Assert(world.SystemManager.Systems.Size == 2); Entity et = world.CreateEntity(); var power = et.AddComponentFromPool<Power2>(); power.POWER = 100; et.Refresh(); Entity et1 = world.CreateEntityFromTemplate("teste"); Debug.Assert(et1 != null); { world.Update(0, ExecutionType.UpdateSynchronous); } et.RemoveComponent<Power2>(); et.Refresh(); { world.Update(0, ExecutionType.UpdateSynchronous); } et.AddComponentFromPool<Power2>(); et.GetComponent<Power2>().POWER = 100; et.Refresh(); world.Update(0, ExecutionType.UpdateSynchronous); }
public Entity CreateStandAloneEntity(EntityWorld world) { var entity = world.CreateEntity(); entity.AddComponentFromPool<Transform>(); entity.AddComponent(new SceneGraphComponent()); entity.AddComponent(new SceneGraphRenderRoot<StandardShipModelRenderer>()); CreateChildEntities(world, entity); return entity; }
public void TestAspectEmpty() { Aspect aspect = Aspect.Empty(); EntityWorld entityWorld = new EntityWorld(); Entity entity; // Aspect.Empty is a base Aspect for EntitySystem meaning "no entities to process" entity = entityWorld.CreateEntity(); Assert.IsFalse(aspect.Interests(entity), "Entity without components must NOT be subject to empty Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with any component must NOT be subject to empty Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestPowerComponent()); entity.AddComponent(new TestHealthComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with any components must NOT be subject to empty Aspect"); }
public override Entity CreateEntity(EntityWorld world, Entity ship, Entity parent, int? index = default(int?)) { var entity = world.CreateEntity(); parent.AddChild(entity); var xform = entity.AddComponentFromPool<Transform>(); xform.CopyValuesFrom(Transform); var component = new DummyPartComponent(this, ship); entity.AddComponent(component); entity.AddComponent<IShipPartComponent>(component); entity.AddComponent(new BoundingBoxComponent(new FloatRect(-10, -10, 20, 20))); return entity; }
public virtual Entity Install(EntityWorld world, Entity shipEntity, Entity hardpointEntity) { var slot = hardpointEntity.GetComponent<HardpointComponent>(); if (!hardpointEntity.IsChildOf(shipEntity)) throw new InvalidOperationException("Cannot install, ship entity does not own hardpoint entity."); if (slot == null) throw new InvalidOperationException("Cannot install on non-hardpoint entity."); var shipComponent = shipEntity.GetComponent<ShipComponent>(); var moduleEntity = world.CreateEntity(); var hardpointTransform = hardpointEntity.GetComponent<Transform>(); var moduleTransform = moduleEntity.AddComponentFromPool<Transform>(); moduleTransform.Position = Vector2.Zero; moduleTransform.Rotation = 0; moduleTransform.Scale = Vector2.One; moduleTransform.Origin = -hardpointTransform.Origin; var scale = hardpointTransform.Scale; var origin = -hardpointTransform.Origin; if (scale.X < 0) { scale.X = Math.Abs(scale.X); origin.X *= -1; } if (scale.Y < 0) { scale.Y *= Math.Abs(scale.Y); origin.Y *= -1; } moduleTransform.Scale = scale; moduleTransform.Origin = origin; hardpointEntity.AddChild(moduleEntity); var previousInstalled = slot.InstalledEntity; if (previousInstalled != null) { previousInstalled.GetComponent<ModuleComponent>().Module.Uninstall(shipEntity, previousInstalled); } if (!string.IsNullOrWhiteSpace(PartGroupId)) { ShipEntityFactory.GetShipModel(PartGroupId).CreateChildEntities(world, moduleEntity); } var moduleComponent = new ModuleComponent { HardpointEntity = hardpointEntity, Module = this }; slot.InstalledEntity = moduleEntity; moduleEntity.AddComponent(moduleComponent); return moduleEntity; }
public static Entity CreateBulletExplosion(EntityWorld world, float x, float y) { Entity e = world.CreateEntity(); GamePool pool = (GamePool)world.Pool; e.SetGroup("EFFECTS"); e.AddComponent(pool.TakeComponent<Transform>()); e.AddComponent(pool.TakeComponent<SpatialForm>()); e.AddComponent(pool.TakeComponent<Expires>()); e.GetComponent<SpatialForm>().SpatialFormFile = "BulletExplosion"; e.GetComponent<Expires>().LifeTime = 1000; e.GetComponent<Transform>().Coords = new Vector3(x, y, 0); return e; }
public static Entity CreateMissile(EntityWorld world) { Entity missile = world.CreateEntity(); GamePool pool = (GamePool)world.GetPool(); missile.SetGroup("BULLETS"); missile.AddComponent(pool.TakeComponent<Transform>()); missile.AddComponent(pool.TakeComponent<SpatialForm>()); missile.AddComponent(pool.TakeComponent<Velocity>()); missile.AddComponent(pool.TakeComponent<Expires>()); missile.GetComponent<SpatialForm>().SetSpatialFormFile("Missile"); missile.GetComponent<Expires>().SetLifeTime(2000); return missile; }
public static Entity CreateEnemyShip(EntityWorld world) { Entity e = world.CreateEntity(); e.SetGroup("SHIPS"); GamePool pool = (GamePool)world.GetPool(); e.AddComponent(pool.TakeComponent<Transform>()); e.AddComponent(pool.TakeComponent<SpatialForm>()); e.AddComponent(pool.TakeComponent<Health>()); e.AddComponent(pool.TakeComponent<Weapon>()); e.AddComponent(pool.TakeComponent<Enemy>()); e.AddComponent(pool.TakeComponent<Velocity>()); e.GetComponent<SpatialForm>().SetSpatialFormFile("EnemyShip"); e.GetComponent<Health>().SetHealth(10); return e; }
public static Entity CreateShipExplosion(EntityWorld world, float x, float y) { Entity e = world.CreateEntity(); GamePool pool = (GamePool)world.GetPool(); e.SetGroup("EFFECTS"); e.AddComponent(pool.TakeComponent<Transform>()); e.AddComponent(pool.TakeComponent<SpatialForm>()); e.AddComponent(pool.TakeComponent<Expires>()); e.GetComponent<SpatialForm>().SetSpatialFormFile("ShipExplosion"); e.GetComponent<Transform>().SetCoords(new Vector3(x, y, 0)); e.GetComponent<Expires>().SetLifeTime(1000); return e; }
public void DummyTests() { EntityWorld world = new EntityWorld(); SystemManager systemManager = world.SystemManager; DummyCommunicationSystem DummyCommunicationSystem = new DummyCommunicationSystem(); systemManager.SetSystem(DummyCommunicationSystem, ExecutionType.UpdateSynchronous); world.InitializeAll(false); for (int i = 0; i < 100; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; et.Group = "teste"; et.Refresh(); } { Entity et = world.CreateEntity(); et.Tag = "tag"; et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; et.Refresh(); } { DateTime dt = DateTime.Now; world.Update(0); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } Debug.Assert(world.TagManager.GetEntity("tag") != null); Debug.Assert(world.GroupManager.GetEntities("teste").Size == 100); Debug.Assert(world.EntityManager.ActiveEntitiesCount == 101); Debug.Assert(world.SystemManager.Systems.Size == 1); }
// Use this for initialization void Awake() { world = new EntityWorld(); for(int i = 0; i< 5; ++i){ Entity e = world.CreateEntity(); TestComp1 te = new TestComp1(); e.AddComponent(te); } EntitySystem.BlackBoard.SetEntry<TestProcessor>("test1", new TestProcessor()); //world.RefreshEntity(); TestProcessor processor = new TestProcessor(); TestProcessor2 processor2 = new TestProcessor2(); world.SystemManager.SetSystem<TestProcessor>(processor,Artemis.Manager.GameLoopType.Update); // world.SystemManager.SetSystem<TestProcessor2>(processor2,Artemis.Manager.GameLoopType.Update); }
static void multi() { healthBag.Add(new Health()); healthBag.Add(new Health()); componentPool.Add(typeof(Health), healthBag); Bag<Component> tempBag; EntityWorld world = new EntityWorld(); SystemManager systemManager = world.GetSystemManager(); world.GetEntityManager().RemovedComponentEvent += new RemovedComponentHandler(RemovedComponent); world.GetEntityManager().RemovedEntityEvent += new RemovedEntityHandler(RemovedEntity); EntitySystem hs = systemManager.SetSystem(new MultHealthBarRenderSystem(),ExecutionType.Update); //EntitySystem hs = systemManager.SetSystem(new SingleHEAVYHealthBarRenderSystem(),ExecutionType.Update); systemManager.InitializeAll(); List<Entity> l = new List<Entity>(); for (int i = 0; i < 1000; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().AddHealth(100); et.Refresh(); l.Add(et); } for (int i = 0; i < 100; i++) { DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateSynchronous(ExecutionType.Update); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } int df = 0; foreach (var item in l) { if (item.GetComponent<Health>().GetHealth() == 90) { df++; } } Console.ReadLine(); }
/// <summary>Creates the test health entity.</summary> /// <param name="entityWorld">The entity world.</param> /// <param name="group">The group.</param> /// <param name="tag">The tag.</param> /// <returns>The specified entity.</returns> public static Entity CreateTestHealthEntity(EntityWorld entityWorld, string group = "", string tag = "") { Entity entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.GetComponent<TestHealthComponent>().Points = 100; if (!string.IsNullOrEmpty(group)) { entity.Group = group; } if (!string.IsNullOrEmpty(tag)) { entity.Tag = tag; } return entity; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize () { spriteBatch = new SpriteBatch (GraphicsDevice); entityWorld = new EntityWorld(); EntitySystem.BlackBoard.SetEntry("SpriteBatch", this.spriteBatch); EntitySystem.BlackBoard.SetEntry("ContentManager", this.Content); this.entityWorld.InitializeAll(true); var e = entityWorld.CreateEntity(); e.AddComponentFromPool<Transform>(); e.AddComponentFromPool<Texture2DRenderer>(); e.GetComponent<Transform>().x = 128; e.GetComponent<Texture2DRenderer>().TextureName = "Area1Tileset"; // TODO: Add your initialization logic here base.Initialize (); }
public static void SystemComunicationTeste() { EntitySystem.BlackBoard.SetEntry<int>("Damage", 5); EntityWorld world = new EntityWorld(); SystemManager systemManager = world.SystemManager; DummyCommunicationSystem DummyCommunicationSystem = new DummyCommunicationSystem(); systemManager.SetSystem(DummyCommunicationSystem, ExecutionType.Update); systemManager.InitializeAll(); List<Entity> l = new List<Entity>(); for (int i = 0; i < 100; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; et.Refresh(); l.Add(et); } { DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateSynchronous(ExecutionType.Update); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } EntitySystem.BlackBoard.SetEntry<int>("Damage", 10); { DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateSynchronous(ExecutionType.Update); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } foreach (var item in l) { Debug.Assert(item.GetComponent<Health>().HP == 85); } }
public static void QueueSystemTeste() { EntityWorld world = new EntityWorld(); SystemManager systemManager = world.SystemManager; QueueSystemTest QueueSystemTest = new ArtemisTest.QueueSystemTest(); QueueSystemTest QueueSystemTest2 = new ArtemisTest.QueueSystemTest(); systemManager.SetSystem(QueueSystemTest, ExecutionType.Update); systemManager.SetSystem(QueueSystemTest2, ExecutionType.Update); systemManager.InitializeAll(); QueueSystemTest.SetQueueProcessingLimit(20, QueueSystemTest.Id); Debug.Assert(QueueSystemTest.GetQueueProcessingLimit(QueueSystemTest.Id) == QueueSystemTest.GetQueueProcessingLimit(QueueSystemTest2.Id)); QueueSystemTest2 QueueSystemTestteste = new ArtemisTest.QueueSystemTest2(); Debug.Assert(QueueSystemTest.GetQueueProcessingLimit(QueueSystemTestteste.Id) != QueueSystemTest.GetQueueProcessingLimit(QueueSystemTest2.Id)); QueueSystemTest.SetQueueProcessingLimit(1000, QueueSystemTest.Id); List<Entity> l = new List<Entity>(); for (int i = 0; i < 1000000; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP = 100; QueueSystemTest.AddToQueue(et, QueueSystemTest.Id); l.Add(et); } Console.WriteLine("Start"); while (QueueSystemTest.QueueCount(QueueSystemTest.Id) > 0 && QueueSystemTest.QueueCount(QueueSystemTest2.Id) > 0) { DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateAsynchronous(ExecutionType.Update); Console.WriteLine("Count: " + QueueSystemTest.QueueCount(QueueSystemTest.Id)); Console.WriteLine("Time: " + (DateTime.Now - dt).TotalMilliseconds); } Console.WriteLine("End"); foreach (var item in l) { Debug.Assert(item.GetComponent<Health>().HP == 90); } }
public static void multsystem() { healthBag.Clear(); componentPool.Clear(); healthBag.Add(new Health()); healthBag.Add(new Health()); componentPool.Add(typeof(Health), healthBag); Bag<Component> tempBag; EntityWorld world = new EntityWorld(); SystemManager systemManager = world.SystemManager; world.EntityManager.RemovedComponentEvent += new RemovedComponentHandler(RemovedComponent); world.EntityManager.RemovedEntityEvent += new RemovedEntityHandler(RemovedEntity); EntitySystem hs = systemManager.SetSystem(new SingleHealthBarRenderSystem(),ExecutionType.Update); hs = systemManager.SetSystem(new DummySystem(),ExecutionType.Update); hs = systemManager.SetSystem(new DummySystem2(),ExecutionType.Update); hs = systemManager.SetSystem(new DummySystem3(),ExecutionType.Update); systemManager.InitializeAll(); List<Entity> l = new List<Entity>(); for (int i = 0; i < 100000; i++) { Entity et = world.CreateEntity(); et.AddComponent(new Health()); et.GetComponent<Health>().HP += 100; et.Refresh(); l.Add(et); } for (int i = 0; i < 100; i++) { DateTime dt = DateTime.Now; world.LoopStart(); systemManager.UpdateAsynchronous(ExecutionType.Update); //systemManager.UpdateSynchronous(ExecutionType.Update); Console.WriteLine((DateTime.Now - dt).TotalMilliseconds); } //int df = 0; //foreach (var item in l) //{ // if (item.GetComponent<Health>().GetHealth() == 90) // { // df++; // } // else // { // Console.WriteLine("errro"); // } //} }
public Entity GenerateBackground(EntityWorld world, float starDensity, params NebulaParameters[] nebulae) { var entity = world.CreateEntity(); entity.AddComponentFromPool<Transform>(); var bgTex = GenerateBackgroundTexture(new Point(Device.Viewport.Width, Device.Viewport.Height), starDensity, nebulae); entity.AddComponent(new SpriteComponent { Texture = bgTex, TextureRect = new Rectangle(0, 0, bgTex.Width, bgTex.Height) }); entity.AddComponent(new BackgroundComponent { Parallax = 0, FillViewPort = false }); entity.Refresh(); return entity; }
public Entity GenerateBackground(EntityWorld world, int seed, Point size) { var entity = world.CreateEntity(); entity.AddComponentFromPool<Transform>(); var bgTex = GenerateBackgroundTexture(seed, size); entity.AddComponent(new SpriteComponent { Texture = bgTex, TextureRect = new Rectangle(0, 0, bgTex.Width, bgTex.Height) }); entity.AddComponent(new BackgroundComponent { Parallax = 0, FillViewPort = false }); entity.Refresh(); return entity; }
public void TestDerivedComponents() { EntityWorld entityWorld = new EntityWorld(); TestDerivedComponent derived = new TestDerivedComponent(); Entity entity = entityWorld.CreateEntity(); entity.AddComponent(derived); Assert.IsNull(entity.GetComponent<TestDerivedComponent>(), "Should be null because the component should be added as if it was a base component"); Assert.IsNotNull(entity.GetComponent<TestBaseComponent>()); Assert.IsTrue(entity.GetComponent<TestBaseComponent>().IsDerived()); #pragma warning disable 612,618 ComponentMapper<TestBaseComponent> baseMapper = new ComponentMapper<TestBaseComponent>(entityWorld); ComponentMapper<TestDerivedComponent> derivedMapper = new ComponentMapper<TestDerivedComponent>(entityWorld); #pragma warning restore 612,618 Assert.IsNull(derivedMapper.Get(entity)); Assert.IsNotNull(baseMapper.Get(entity)); Assert.AreEqual(baseMapper.Get(entity), entity.GetComponent<TestBaseComponent>()); }
protected override void Initialize() { spriteBatch = new SpriteBatch(GraphicsDevice); _netAgent = new NetworkAgent(AgentRole.Client, "Umbra"); EntitySystem.BlackBoard.SetEntry("Game", this); EntitySystem.BlackBoard.SetEntry("ContentManager", Content); EntitySystem.BlackBoard.SetEntry("SpriteBatch", spriteBatch); EntitySystem.BlackBoard.SetEntry("GraphicsDevice", GraphicsDevice); EntitySystem.BlackBoard.SetEntry("ContentManager", Content); EntitySystem.BlackBoard.SetEntry("NetworkAgent", _netAgent); _entityWorld = new EntityWorld(); // create camera Vector3 camPosition = new Vector3(0, 10, 5); Matrix camRotation = Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(-65.0f)); float aspectRatio = (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height; CameraComponent cameraComponent = new CameraComponent(camPosition, camRotation, aspectRatio); Entity cameraEntity = _entityWorld.CreateEntity(); cameraEntity.AddComponent(cameraComponent); EntitySystem.BlackBoard.SetEntry("Camera", cameraComponent); //// TEMP //// Map map = new Map(100, 100); Entity mapEntity = _entityWorld.CreateEntity(); mapEntity.AddComponent(new TileMapComponent(map)); EntitySystem.BlackBoard.SetEntry("Map", map); //// TEMP //// _entityWorld.InitializeAll(new[] { GetType().Assembly }); CrawEntityManager.Instance.Initialize(_entityWorld, new ClientEntityFactory(_entityWorld)); _netAgent.Connect("127.0.0.1"); base.Initialize(); }
public void TestRemoveComponent() { EntityWorld entityWorld = new EntityWorld(); Entity entity = entityWorld.CreateEntity(); // Remove absent component from a world with no components entity.RemoveComponent<TestHealthComponent>(); Debug.WriteLine("OK"); entity.AddComponent(new TestHealthComponent()); entity.RemoveComponent<TestHealthComponent>(); // Removing component is deferred until the update Assert.IsNotNull(entity.GetComponent<TestHealthComponent>()); // Update the world, component should be removed entityWorld.Update(); Assert.IsNull(entity.GetComponent<TestHealthComponent>()); // Remove absent component entity.RemoveComponent<TestHealthComponent>(); Debug.WriteLine("OK"); int defaultComponentsBagCapacity = 16; for (int i = 0; i < defaultComponentsBagCapacity - 1; i++) { entity = entityWorld.CreateEntity(); entity.RemoveComponent<TestHealthComponent>(); } Debug.WriteLine("OK"); entity = entityWorld.CreateEntity(); Assert.AreEqual(defaultComponentsBagCapacity, entity.Id, "Entity id has unexpected value."); // Remove absent component now from Entity{16} (16 is the magic number = defaultComponentsBagCapacity) entity.RemoveComponent<TestHealthComponent>(); Debug.WriteLine("OK"); }
public void TestPoolableComponents() { var entityWorld = new EntityWorld(isSortedEntities: false, processAttributes: true, initializeAll: true) { PoolCleanupDelay = 0 }; var pool = (ComponentPool<ComponentPoolable>)entityWorld.GetPool(typeof(TestPowerComponentPoolable)); Debug.WriteLine("ComponentPool<TestPowerComponentPoolable> is not Null:"); Assert.IsNotNull(pool); Debug.WriteLine("OK"); var poolAttribute = (ArtemisComponentPool) typeof(TestPowerComponentPoolable).GetCustomAttributes(typeof(ArtemisComponentPool), false).Single(); Assert.AreEqual(poolAttribute.InitialSize, pool.InvalidCount, "Initially component pool should contain only invalid items"); int expectedPower = default(int); var addedComponentEventHandler = new AddedComponentHandler((e, c) => { Debug.WriteLine("TestPowerComponentPoolable added: "); Assert.AreEqual(typeof (TestPowerComponentPoolable), c.GetType()); Debug.WriteLine("OK"); Debug.WriteLine("TestPowerComponentPoolable.Power == {0}:", expectedPower); Assert.AreEqual(expectedPower, ((TestPowerComponentPoolable) c).Power); Debug.WriteLine("OK"); }); entityWorld.EntityManager.AddedComponentEvent += addedComponentEventHandler; Entity entity = entityWorld.CreateEntity(); Debug.WriteLine("Adding FRESH uninitialized TestPowerComponentPoolable from pool (expected power = {0})", default(int)); TestPowerComponentPoolable testPowerComponent = entity.AddComponentFromPool<TestPowerComponentPoolable>(); Assert.AreEqual(expectedPower, testPowerComponent.Power); Assert.AreEqual(expectedPower, entity.GetComponent<TestPowerComponentPoolable>().Power); entity.RemoveComponent<TestPowerComponentPoolable>(); entityWorld.Update(); Assert.IsFalse(entity.HasComponent<TestPowerComponentPoolable>()); expectedPower = 100; Debug.WriteLine("Adding initialized TestPowerComponentPoolable from pool (expected power = {0})", expectedPower); entity.AddComponentFromPool<TestPowerComponentPoolable>(c => c.Power = expectedPower); Assert.AreEqual(expectedPower, entity.GetComponent<TestPowerComponentPoolable>().Power); entity.RemoveComponent<TestPowerComponentPoolable>(); entityWorld.Update(); Assert.IsFalse(entity.HasComponent<TestPowerComponentPoolable>()); entityWorld.EntityManager.AddedComponentEvent -= addedComponentEventHandler; Debug.WriteLine("Causing ComponentPool<TestPowerComponentPoolable> to fill up to maximum capacity..."); var entities = new List<Entity>(); while (pool.InvalidCount > 0) { var ent = entityWorld.CreateEntity(); ent.AddComponentFromPool<TestPowerComponentPoolable>(c => c.Power = expectedPower); entities.Add(ent); } foreach (var ent in entities) { ent.RemoveComponent<TestPowerComponentPoolable>(); } Debug.WriteLine("Causing ComponentPool<TestPowerComponentPoolable> cleanup..."); entityWorld.Update(); Assert.AreEqual(poolAttribute.InitialSize, pool.InvalidCount, "Cleaned up component pool should contain only invalid items"); Debug.WriteLine("OK"); entityWorld.EntityManager.AddedComponentEvent += addedComponentEventHandler; Debug.WriteLine("Adding USED uninitialized TestPowerComponentPoolable from pool (expected power = {0})", expectedPower); testPowerComponent = entity.AddComponentFromPool<TestPowerComponentPoolable>(); Assert.AreEqual(expectedPower, testPowerComponent.Power); Assert.AreEqual(expectedPower, entity.GetComponent<TestPowerComponentPoolable>().Power); }
public override Entity CreateEntity(EntityWorld world, Entity ship, Entity parent, int? index=null) { var entity = world.CreateEntity(); parent.AddChild(entity, index); var thruster = new ThrusterComponent(this, ship); entity.AddComponent<IShipPartComponent>(thruster); entity.AddComponent(thruster); entity.AddSceneGraphRendererComponent<StandardShipModelRenderer>(); entity.AddComponentFromPool<Transform>(t=>t.CopyValuesFrom(Transform)); var spriteManager = ServiceLocator.Instance.GetService<ISpriteManagerService>(); var spriteComponent = spriteManager.CreateSpriteComponent(SpriteId); entity.AddComponent(spriteComponent); entity.AddComponent(new BoundingBoxComponent(new FloatRect(0, 0, spriteComponent.TextureRect.Width, spriteComponent.TextureRect.Height))); ship.GetComponent<ShipComponent>()?.Thrusters.Add(entity); return entity; }
public void TestAspectAllOneExclude() { Aspect aspect = Aspect.Empty() .GetAll(typeof(TestHealthComponent)) .GetOne(typeof(TestBaseComponent), typeof(TestDerivedComponent)) .GetExclude(typeof(TestPowerComponent), typeof(TestPowerComponentPoolable)); EntityWorld entityWorld = new EntityWorld(); Entity entity; entity = entityWorld.CreateEntity(); Assert.IsFalse(aspect.Interests(entity), "Entity without components must NOT be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent} must NOT be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestBaseComponent()); Assert.IsTrue(aspect.Interests(entity), "Entity with {TestHealthComponent, TestBaseComponent} must be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestBaseComponent()); entity.AddComponent(new TestDerivedComponent()); Assert.IsTrue(aspect.Interests(entity), "Entity with {TestHealthComponent, TestBaseComponent, TestDerivedComponent} must be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestBaseComponent()); entity.AddComponent(new TestDerivedComponent()); entity.AddComponent(new TestPowerComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent, TestBaseComponent, TestDerivedComponent, TestPowerComponent} must NOT be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestDerivedComponent()); entity.AddComponent(new TestPowerComponentPoolable()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent, TestDerivedComponent, TestPowerComponentPoolable} must NOT be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestBaseComponent()); entity.AddComponent(new TestDerivedComponent()); entity.AddComponent(new TestPowerComponent()); entity.AddComponent(new TestPowerComponentPoolable()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent, TestBaseComponent, TestDerivedComponent, TestPowerComponent, TestPowerComponentPoolable} must NOT be subject to \"(Healthy) && (Base || Derived) && !(Power || PowerPoolable)\" Aspect"); }
public void TestAspectExcludeMultiple() { Aspect aspect = Aspect.Exclude(typeof(TestPowerComponent), typeof(TestPowerComponentPoolable)); EntityWorld entityWorld = new EntityWorld(); Entity entity; entity = entityWorld.CreateEntity(); Assert.IsTrue(aspect.Interests(entity), "Entity without components must be subject to \"Not Powered by any means\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); Assert.IsTrue(aspect.Interests(entity), "Entity with {TestHealthComponent} must be subject to \"Not Powered by any means\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestPowerComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent, TestPowerComponent} must NOT be subject to \"Not Powered by any means\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); entity.AddComponent(new TestPowerComponent()); entity.AddComponent(new TestPowerComponentPoolable()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent, TestPowerComponent, TestPowerComponentPoolable} must NOT be subject to \"Not Powered by any means\" Aspect"); }
public void TestAspectOneSingle() { Aspect aspect = Aspect.One(typeof(TestPowerComponent)); EntityWorld entityWorld = new EntityWorld(); Entity entity; entity = entityWorld.CreateEntity(); Assert.IsFalse(aspect.Interests(entity), "Entity without components must NOT be subject to \"Powered\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestHealthComponent()); Assert.IsFalse(aspect.Interests(entity), "Entity with {TestHealthComponent} must NOT be subject to \"Powered\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestPowerComponent()); Assert.IsTrue(aspect.Interests(entity), "Entity with {TestPowerComponent} must be subject to \"Powered\" Aspect"); entity = entityWorld.CreateEntity(); entity.AddComponent(new TestPowerComponent()); entity.AddComponent(new TestHealthComponent()); Assert.IsTrue(aspect.Interests(entity), "Entity with {TestPowerComponent, TestHealthComponent} must be subject to \"Powered\" Aspect"); }