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 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;
		}
Beispiel #4
0
        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);
        }