public void TestComponents()
        {
            EntityRegistry registry = new EntityRegistry(100);

            Entity entity1 = registry.Create();
            Entity entity2 = registry.Create();

            PositionComponent pos1 = new PositionComponent(1, 0);
            PositionComponent pos2 = new PositionComponent(0, 1);

            registry.AddComponent(entity1, new TagComponent());

            registry.AddComponent(entity1, pos1);
            registry.AddComponent(entity2, pos2);

            registry.AddComponent(entity2, new ParentedComponent(entity1));

            Assert.IsTrue(registry.HasComponent <TagComponent>(entity1));
            Assert.IsFalse(registry.HasComponent <TagComponent>(entity2));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity1));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2));
            Assert.IsFalse(registry.HasComponent <ParentedComponent>(entity1));
            Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2));

            Assert.AreEqual(registry.GetComponent <PositionComponent>(entity1), pos1);
            Assert.AreEqual(registry.GetComponent <PositionComponent>(entity2), pos2);

            registry.Destroy(entity1);

            Assert.AreEqual(registry.GetComponent <PositionComponent>(entity2)
                            , pos2);

            Assert.IsFalse(registry.IsValid(registry.GetComponent <ParentedComponent>(entity2).Parent));

            Assert.IsFalse(registry.HasComponent <TagComponent>(entity2));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2));
            Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2));

            Entity entity3 = registry.Create();
            Entity entity4 = registry.Create();

            registry.AddComponent(entity4, pos1);
            registry.AddComponent(entity3, pos1);
            registry.AddComponent(entity3, new ParentedComponent());
            registry.AddComponent(entity4, new ParentedComponent());
            Assert.IsFalse(registry.HasComponent <TagComponent>(entity3));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity3));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity4));
            Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity3));
            Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity4));

            Assert.IsFalse(registry.HasComponent <TagComponent>(entity2));
            Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2));
            Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2));
        }
        public void TestEntityCreationAndDestruction()
        {
            const int      kEntitiesToTest = 1000;
            int            tests           = 100;
            EntityRegistry registry        = new EntityRegistry(kEntitiesToTest);

            Assert.IsFalse(registry.IsValid(Entity.NullEntity));

            List <Entity> validEntities   = new List <Entity>(kEntitiesToTest * 100);
            List <Entity> invalidEntities = new List <Entity>(kEntitiesToTest * 100);

            for (int i = 0; i < kEntitiesToTest; ++i)
            {
                validEntities.Add(registry.Create());
            }

            Random randomizer = new Random(457);

            while (tests > 0)
            {
                --tests;
                for (int i = validEntities.Count - 1; i >= 0; --i)
                {
                    Entity entity = validEntities[i];
                    if (randomizer.NextDouble() < 0.01)
                    {
                        Assert.IsTrue(registry.IsValid(entity));
                        registry.Destroy(entity);
                        validEntities.RemoveAt(i);
                        invalidEntities.Add(entity);
                    }
                }
                for (int i = validEntities.Count; i < kEntitiesToTest; ++i)
                {
                    if (randomizer.NextDouble() < 0.01)
                    {
                        Entity replacement = registry.Create();
                        validEntities.Add(replacement);
                    }
                }
            }

            foreach (Entity entity in validEntities)
            {
                Assert.IsTrue(registry.IsValid(entity));
            }

            foreach (Entity entity in invalidEntities)
            {
                Assert.IsFalse(registry.IsValid(entity));
            }
        }
        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);
        }
예제 #4
0
        public void Update(EntityRegistry registry, EventSystem eventSystem)
        {
            int toungueCount = mToungueSlice.Rect.Count;

            for (int toungueIndex = 0; toungueIndex < toungueCount; ++toungueIndex)
            {
                Rect toungueRect = mToungueSlice.Rect[toungueIndex];
                int  insectCount = mInsectSlice.Entity.Count;
                for (int insectIndex = insectCount - 1; insectIndex >= 0; --insectIndex)
                {
                    Rect insectRect = mInsectSlice.Rect[insectIndex];
                    if (toungueRect.Intersects(insectRect))
                    {
                        registry.Destroy(mInsectSlice.Entity[insectIndex]);
                        eventSystem.PostMessage <AteInsect>(new AteInsect(mToungueSlice.ToungueParent[toungueIndex].Parent));
                    }
                }
            }
        }
예제 #5
0
        public void Update(EntityRegistry registry, EventSystem eventSystem)
        {
            const float kBuffer  = 200;
            int         flyCount = mFliesSlice.Entity.Count;

            for (int i = flyCount - 1; i >= 0; --i)
            {
                Rect flyRect = mFliesSlice.FlyRect[i];
                if ((flyRect.MaxX < -kBuffer) || (flyRect.MinX > mPond.Width + kBuffer))
                {
                    registry.Destroy(mFliesSlice.Entity[i]);
                    --flyCount;
                }
            }
            if (flyCount < mFlyData.MaxActiveFlies)
            {
                mTimeBeforeSpawn -= mTime.DeltaTime;
                if (mTimeBeforeSpawn <= 0f)
                {
                    SpawnFly(registry);
                    mTimeBeforeSpawn = mRandom.NextPoissonTime(mFlyData.FliesPerSecond);
                }
            }
        }
예제 #6
0
        private void UpdateToungueState(EntityRegistry registry)
        {
            int count = mAllToungues.Entity.Count;

            for (int i = 0; i < count; ++i)
            {
                ToungueState state   = mAllToungues.State[i];
                float        newTime = state.TimeLeft - mTime.DeltaTime;
                if (newTime > 0)
                {
                    state.TimeLeft = newTime;
                    registry.SetComponent(mAllToungues.Entity[i], state);
                }
                else
                {
                    int stateIndex = (int)state.Stage;
                    if (stateIndex > 0)
                    {
                        ToungueStages nextStage = (ToungueStages)(stateIndex - 1);
                        float         nextTime  = mData.GetStateTime(nextStage);
                        registry.SetComponent(mAllToungues.Entity[i], new ToungueState(nextStage, nextTime));
                    }
                    else
                    {
                        mTransitioning.Add(mAllToungues.Entity[i]);
                    }
                }
            }
            foreach (Entity toungue in mTransitioning)
            {
                Entity frog = registry.GetComponent <ParentEntity>(toungue).Parent;
                registry.RemoveComponent <ControlledEntity>(frog);
                registry.Destroy(toungue);
            }
            mTransitioning.Clear();
        }
        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]);
                }
            }
        }