コード例 #1
0
        public void should_correctly_add_component()
        {
            var expectedSize       = 10;
            var fakeEntityId       = 1;
            var fakeComponent      = new TestComponentOne();
            var fakeComponentTypes = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };

            var mockComponentLookup = Substitute.For <IComponentTypeLookup>();

            mockComponentLookup.GetAllComponentTypes().Returns(fakeComponentTypes);

            var database = new ComponentDatabase(mockComponentLookup, expectedSize);

            database.Add(0, fakeEntityId, fakeComponent);

            Assert.Equal(database.EntityComponents[0][1], fakeComponent);
            var nullCount = database.EntityComponents.Sum(x => x.Count(y => y == null));

            Assert.Equal(29, nullCount);
        }
コード例 #2
0
        public void should_only_add_entity_when_components_match_group()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new GroupAccessorToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "default");

            var existingEntityOne = new Entity(Guid.NewGuid(), mockEventSystem);
            var componentToAdd    = new TestComponentOne();

            existingEntityOne.AddComponent <TestComponentTwo>();

            var existingEntityTwo     = new Entity(Guid.NewGuid(), mockEventSystem);
            var unapplicableComponent = new TestComponentThree();

            existingEntityTwo.AddComponent <TestComponentOne>();

            var dummyEventToSeedMock = new ComponentAddedEvent(new Entity(Guid.NewGuid(), mockEventSystem), new TestComponentOne());
            var underlyingEvent      = new ReactiveProperty <ComponentAddedEvent>(dummyEventToSeedMock);

            mockEventSystem.Receive <ComponentAddedEvent>().Returns(underlyingEvent);

            var cacheableGroupAccessor = new CacheableGroupAccessor(accessorToken, new IEntity[] {}, mockEventSystem);

            cacheableGroupAccessor.MonitorEntityChanges();

            existingEntityOne.AddComponent(componentToAdd);
            underlyingEvent.SetValueAndForceNotify(new ComponentAddedEvent(existingEntityOne, componentToAdd));

            existingEntityTwo.AddComponent(unapplicableComponent);
            underlyingEvent.SetValueAndForceNotify(new ComponentAddedEvent(existingEntityTwo, unapplicableComponent));

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(1));
            Assert.That(cacheableGroupAccessor.CachedEntities[existingEntityOne.Id], Is.EqualTo(existingEntityOne));
        }
コード例 #3
0
        public void should_get_all_components_for_entity()
        {
            var expectedSize       = 10;
            var fakeEntityId       = 1;
            var otherEntityId      = 2;
            var fakeComponent1     = new TestComponentOne();
            var fakeComponent2     = new TestComponentThree();
            var fakeComponentTypes = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };

            var mockComponentLookup = Substitute.For <IComponentTypeLookup>();

            mockComponentLookup.GetAllComponentTypes().Returns(fakeComponentTypes);

            var database = new ComponentDatabase(mockComponentLookup, expectedSize);

            database.Add(0, fakeEntityId, fakeComponent1);
            database.Add(2, fakeEntityId, fakeComponent2);
            database.Add(0, otherEntityId, new TestComponentOne());
            database.Add(1, otherEntityId, new TestComponentTwo());
            database.Add(2, otherEntityId, new TestComponentThree());

            var allComponents = database.GetAll(fakeEntityId).ToArray();

            Assert.True(allComponents.Contains(fakeComponent1));
            Assert.True(allComponents.Contains(fakeComponent2));
            Assert.Equal(allComponents.Length, 2);
        }
コード例 #4
0
        public void should_correctly_remove_component_for_entity()
        {
            var expectedSize       = 10;
            var fakeEntityId       = 1;
            var otherEntityId      = 2;
            var fakeComponent1     = new TestComponentOne();
            var fakeComponentTypes = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };

            var mockComponentLookup = Substitute.For <IComponentTypeLookup>();

            mockComponentLookup.GetAllComponentTypes().Returns(fakeComponentTypes);

            var database = new ComponentDatabase(mockComponentLookup, expectedSize);

            database.Add(0, fakeEntityId, fakeComponent1);
            database.Add(0, otherEntityId, new TestComponentOne());
            database.Add(1, otherEntityId, new TestComponentOne());

            database.Remove(0, fakeEntityId);
            Assert.False(database.Has(0, fakeEntityId));
        }
コード例 #5
0
        public void should_get_specific_components_for_entity()
        {
            var expectedSize       = 10;
            var fakeEntityId       = 1;
            var otherEntityId      = 2;
            var fakeComponent1     = new TestComponentOne();
            var fakeComponentTypes = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };

            var mockComponentLookup = Substitute.For <IComponentTypeLookup>();

            mockComponentLookup.GetAllComponentTypes().Returns(fakeComponentTypes);

            var database = new ComponentDatabase(mockComponentLookup, expectedSize);

            database.Add(0, fakeEntityId, fakeComponent1);
            database.Add(0, otherEntityId, new TestComponentOne());

            var component = database.Get(0, fakeEntityId);

            Assert.Equal(fakeComponent1, component);
        }
コード例 #6
0
        public void should_only_add_entity_when_components_match_group()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new ObservableGroupToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "default");

            var existingEntityOne = new Entity(Guid.NewGuid(), mockEventSystem);
            var componentToAdd    = new TestComponentOne();

            existingEntityOne.AddComponent <TestComponentTwo>();

            var existingEntityTwo     = new Entity(Guid.NewGuid(), mockEventSystem);
            var unapplicableComponent = new TestComponentThree();

            existingEntityTwo.AddComponent <TestComponentOne>();

            var dummyEventToSeedMock = new ComponentsAddedEvent(new Entity(Guid.NewGuid(), mockEventSystem), new[] { new TestComponentOne() });
            var underlyingEvent      = new ReactiveProperty <ComponentsAddedEvent>(dummyEventToSeedMock);

            mockEventSystem.Receive <ComponentsAddedEvent>().Returns(underlyingEvent);
            mockEventSystem.Receive <ComponentsRemovedEvent>().Returns(Observable.Empty <ComponentsRemovedEvent>());
            mockEventSystem.Receive <EntityAddedEvent>().Returns(Observable.Empty <EntityAddedEvent>());
            mockEventSystem.Receive <EntityRemovedEvent>().Returns(Observable.Empty <EntityRemovedEvent>());

            var cacheableGroupAccessor = new ObservableGroup(mockEventSystem, accessorToken, new IEntity[] {});

            existingEntityOne.AddComponent(componentToAdd);
            underlyingEvent.SetValueAndForceNotify(new ComponentsAddedEvent(existingEntityOne, new[] { componentToAdd }));

            existingEntityTwo.AddComponent(unapplicableComponent);
            underlyingEvent.SetValueAndForceNotify(new ComponentsAddedEvent(existingEntityTwo, new[] { unapplicableComponent }));

            Assert.Equal(1, cacheableGroupAccessor.CachedEntities.Count);
            Assert.Equal <IEntity>(existingEntityOne, cacheableGroupAccessor.CachedEntities[existingEntityOne.Id]);
        }
コード例 #7
0
        public void should_not_throw_error_when_removing_non_existent_component_with_instance()
        {
            var mockMessageBroker = Substitute.For <IMessageBroker>();
            var entity            = new Entity(1, mockMessageBroker);
            var dummyComponent    = new TestComponentOne();

            entity.RemoveComponent(dummyComponent);
        }
コード例 #8
0
ファイル: EntityTests.cs プロジェクト: yy1985710/ecsrx
        public void should_not_throw_error_when_removing_non_existent_component_with_instance()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var entity          = new Entity(Guid.NewGuid(), mockEventSystem);
            var dummyComponent  = new TestComponentOne();

            entity.RemoveComponent(dummyComponent);
        }
コード例 #9
0
        public void should_return_true_when_entity_has_component()
        {
            var mockMessageBroker = Substitute.For <IMessageBroker>();
            var entity            = new Entity(1, mockMessageBroker);
            var dummyComponent    = new TestComponentOne();

            entity.AddComponent(dummyComponent);

            Assert.That(entity.HasComponent <TestComponentOne>());
        }
コード例 #10
0
ファイル: EntityTests.cs プロジェクト: hongliyu2002/ecsrx
        public void should_return_true_when_entity_has_component()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var entity          = new Entity(1, mockEventSystem);
            var dummyComponent  = new TestComponentOne();

            entity.AddComponent(dummyComponent);

            Assert.That(entity.HasComponent <TestComponentOne>());
        }
コード例 #11
0
        public void should_create_batch_with_correct_values()
        {
            var fakeOne1 = new TestComponentOne {
                Data = "hello"
            };
            var fakeOne2 = new TestComponentOne {
                Data = "hi"
            };
            var fakeOnes = new[] { fakeOne1, fakeOne2 };

            var fakeTwo1 = new TestComponentTwo {
                Data = "goodbye"
            };
            var fakeTwo2 = new TestComponentTwo {
                Data = "bye"
            };
            var fakeTwos = new[] { fakeTwo1, fakeTwo2 };

            var mockComponentDatabase = Substitute.For <IComponentDatabase>();

            mockComponentDatabase.GetComponents <TestComponentOne>(Arg.Any <int>()).Returns(fakeOnes);
            mockComponentDatabase.GetComponents <TestComponentTwo>(Arg.Any <int>()).Returns(fakeTwos);

            var mockTypeLookup = Substitute.For <IComponentTypeLookup>();

            mockTypeLookup.GetComponentType(typeof(TestComponentOne)).Returns(0);
            mockTypeLookup.GetComponentType(typeof(TestComponentTwo)).Returns(1);

            var fakeEntity1 = Substitute.For <IEntity>();

            fakeEntity1.Id.Returns(1);
            fakeEntity1.ComponentAllocations.Returns(new[] { 0, 0 });

            var fakeEntity2 = Substitute.For <IEntity>();

            fakeEntity2.Id.Returns(2);
            fakeEntity2.ComponentAllocations.Returns(new[] { 1, 1 });

            var fakeEntities = new [] { fakeEntity1, fakeEntity2 };

            var batchBuilder = new ReferenceBatchBuilder <TestComponentOne, TestComponentTwo>(mockComponentDatabase, mockTypeLookup);

            var batches = batchBuilder.Build(fakeEntities);

            Assert.Equal(fakeEntities.Length, batches.Length);
            Assert.Equal(fakeEntities[0].Id, batches[0].EntityId);
            Assert.Equal(fakeOne1.Data, batches[0].Component1.Data);
            Assert.Equal(fakeTwo1.Data, batches[0].Component2.Data);
            Assert.Equal(fakeEntities[1].Id, batches[1].EntityId);
            Assert.Equal(fakeOne2.Data, batches[1].Component1.Data);
            Assert.Equal(fakeTwo2.Data, batches[1].Component2.Data);
        }
コード例 #12
0
        public void should_only_remove_entity_when_components_no_longer_match_group()
        {
            var mockEventSystem     = Substitute.For <IEventSystem>();
            var mockEntityIndexPool = Substitute.For <IEntityIndexPool>();
            var mockReactor         = Substitute.For <ISystemReactor>();
            var accessorToken       = new GroupAccessorToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "default");
            var mockPool            = Substitute.For <IPool>();

            var componentToRemove = new TestComponentOne();
            var existingEntityOne = new Entity(mockEntityIndexPool.GetId(), new List <IComponent>
            {
                componentToRemove,
                new TestComponentTwo()
            }, mockPool, mockReactor);

            var unapplicableComponent = new TestComponentThree();
            var existingEntityTwo     = new Entity(mockEntityIndexPool.GetId(), new List <IComponent>
            {
                new TestComponentOne(),
                new TestComponentTwo(),
                unapplicableComponent
            }, mockPool, mockReactor);

            /*
             * var dummyEventToSeedMock = new ComponentRemovedEvent(new Entity(mockEntityIndexPool.GetId(), mockPool, mockEventSystem), new TestComponentOne());
             * var underlyingEvent = new ReactiveProperty<ComponentRemovedEvent>(dummyEventToSeedMock);
             * mockEventSystem.Receive<ComponentRemovedEvent>().Returns(underlyingEvent);
             *
             * var cacheableGroupAccessor = new CacheableGroupAccessor(accessorToken, new IEntity[] { existingEntityOne, existingEntityTwo }, mockEventSystem);
             * cacheableGroupAccessor.MonitorEntityChanges();
             *
             * existingEntityOne.RemoveComponent(componentToRemove);
             * underlyingEvent.SetValueAndForceNotify(new ComponentRemovedEvent(existingEntityOne, componentToRemove));
             *
             * existingEntityTwo.RemoveComponent(unapplicableComponent);
             * underlyingEvent.SetValueAndForceNotify(new ComponentRemovedEvent(existingEntityTwo, unapplicableComponent));
             *
             * Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(1));
             * Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(existingEntityTwo));*/
        }