/// <summary>Creates the test power2 entity with pooled component.</summary>
        /// <param name="entityWorld">The entity world.</param>
        /// <returns>The specified entity.</returns>
        public static Entity CreateTestPowerEntityWithPooledComponent(EntityWorld entityWorld)
        {
            Entity entity = entityWorld.CreateEntity();

            TestPowerComponentPoolable testPower = entity.AddComponentFromPool <TestPowerComponentPoolable>();

            testPower.Power = 100;

            return(entity);
        }
        /// <summary>The process.</summary>
        /// <param name="entity">The entity.</param>
        public override void Process(Entity entity)
        {
            TestHealthComponent testHealth = entity.GetComponent <TestHealthComponent>();

            if (testHealth != null)
            {
                testHealth.AddDamage(10);
            }

            TestPowerComponentPoolable testPower = entity.GetComponent <TestPowerComponentPoolable>();

            if (testPower != null)
            {
                testPower.Power -= 10;
            }
        }
        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);
        }