Пример #1
0
        public void ContextSets()
        {
            var context   = new Context();
            var entity    = context.CreateEntity("original");
            var component = new AComponent("a1");
            // set doesn't match until component is attached
            var setA = context.CreateSet(e => e.Contains <AComponent>());
            {
                Assert.AreEqual(0, setA.Count);
                entity.Add(component);
                // set should automatically match after component is added
                foreach (var e in setA)
                {
                    Assert.AreEqual(component, e.Get <AComponent>());
                }
                Assert.AreEqual(1, setA.Count);
            }

            // B doesn't match 'entity' because it doesn't have a BComponent.
            {
                Assert.AreEqual(1, setA.Count);
                var setB = context.CreateSet(e => e.Contains <BComponent>());
                Assert.AreEqual(0, setB.Count);
            }

            // A doesn't match because it must be A AND B
            {
                Assert.AreEqual(1, setA.Count);
                Assert.IsFalse(entity.Contains <BComponent>());
                var setAandB = context.CreateSet(e => e.Contains <AComponent>() && e.Contains <BComponent>());
                Assert.AreNotSame(setA, setAandB);
                Assert.AreEqual(0, setAandB.Count, "IEntity should not match, has no BComponent");
                var entityAandB = context.CreateEntity("ab");
                entityAandB.Add(new AComponent("ab test"));
                entityAandB.Add(new BComponent());
                Assert.AreEqual(1, setAandB.Count, "IEntity should match, has A and B Components");
                Assert.AreEqual(2, setA.Count, "Set A should now match both");
                context.ReleaseEntity(entityAandB);
                Assert.AreEqual(0, setAandB.Count, "IEntity should not match, was removed.");
            }

            // another A set should also match
            {
                var setA2 = context.CreateSet(e => e.Contains <AComponent>());
                foreach (var e in setA2)
                {
                    Assert.AreEqual(component, e.Get <AComponent>());
                }
                Assert.AreEqual(1, setA2.Count);
            }

            // set should not increase the entities matched if another component is added
            {
                Assert.AreEqual(1, setA.Count);
                entity.Add(new AComponent("inner"));
                Assert.AreEqual(1, setA.Count);
            }
        }
Пример #2
0
        public void CreateRemoveEntity()
        {
            var entities         = new List <IEntity>();
            var setManager       = new StubSetManager();
            var componentManager = new StubComponentManager();
            var entityManager    = new EntityManager(entities, componentManager);
            var context          = new Context(entityManager, setManager);

            var e1 = context.CreateEntity("e1");

            Assert.NotNull(e1);
            var e2 = context.CreateEntity("e2");

            Assert.NotNull(e2);

            Assert.AreEqual(2, context.Count());

            context.ReleaseEntity(e1);
            Assert.AreEqual(1, context.Count());

            context.ReleaseEntity(e2);
            Assert.AreEqual(0, context.Count());
        }
Пример #3
0
        public void ContextSetRemoval()
        {
            var context   = new Context();
            var set       = context.CreateSet(e => e.Contains <AComponent>());
            var entity    = context.CreateEntity("set");
            var component = new AComponent("a1");

            entity.Add(component);

            // sanity check
            Assert.AreEqual(1, set.Count);
            Assert.AreSame(entity, set.First());
            Assert.AreSame(component, set.First().First());

            // remove entity, should remove components
            context.ReleaseEntity(entity);
            Assert.AreEqual(0, set.Count);
            Assert.IsNull(set.FirstOrDefault());
        }