Beispiel #1
0
        public void should_only_remove_applicable_entity_when_entity_removed()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new GroupAccessorToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "default");
            var mockPool        = Substitute.For <IPool>();

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

            existingEntityOne.AddComponent <TestComponentOne>();
            existingEntityOne.AddComponent <TestComponentTwo>();

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

            existingEntityTwo.AddComponent <TestComponentOne>();
            existingEntityTwo.AddComponent <TestComponentTwo>();

            var unapplicableEntity = new Entity(Guid.NewGuid(), mockEventSystem);

            unapplicableEntity.AddComponent <TestComponentOne>();

            var underlyingEvent = new ReactiveProperty <EntityRemovedEvent>(new EntityRemovedEvent(unapplicableEntity, mockPool));

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

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

            cacheableGroupAccessor.MonitorEntityChanges();

            underlyingEvent.SetValueAndForceNotify(new EntityRemovedEvent(existingEntityOne, mockPool));

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(1));
            Assert.That(cacheableGroupAccessor.CachedEntities[existingEntityTwo.Id], Is.EqualTo(existingEntityTwo));
        }
Beispiel #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));
        }
        public void should_include_entity_snapshot_on_creation()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new GroupAccessorToken(new Type[] { }, "default");

            var mockPool = Substitute.For <IPool>();

            var dummyEntitySnapshot = new List <IEntity>
            {
                mockPool.CreateEntity(),
                mockPool.CreateEntity(),
                mockPool.CreateEntity()
            };

            /*
             * var dummyEntitySnapshot = new List<IEntity>
             * {
             *  new Entity(Guid.NewGuid(), pool, mockEventSystem),
             *  new Entity(Guid.NewGuid(), pool, mockEventSystem),
             *  new Entity(Guid.NewGuid(), pool, mockEventSystem)
             * };*/

            var cacheableGroupAccessor = new CacheableGroupAccessor(accessorToken, dummyEntitySnapshot, mockEventSystem);

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(3));
            Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(dummyEntitySnapshot[0]));
            Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(dummyEntitySnapshot[1]));
            Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(dummyEntitySnapshot[2]));
        }
        public void should_not_cache_applicable_entity_when_added_to_different_pool()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var poolName        = "defaut";
            var accessorToken   = new GroupAccessorToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "some-other-pool-name");
            var mockPool        = Substitute.For <IPool>();

            mockPool.Name.Returns(poolName);

            var applicableEntity = new Entity(Guid.NewGuid(), mockEventSystem);

            applicableEntity.AddComponent <TestComponentOne>();
            applicableEntity.AddComponent <TestComponentTwo>();

            var unapplicableEntity = new Entity(Guid.NewGuid(), mockEventSystem);

            unapplicableEntity.AddComponent <TestComponentOne>();

            var underlyingEvent = new ReactiveProperty <EntityAddedEvent>(new EntityAddedEvent(applicableEntity, mockPool));

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

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

            cacheableGroupAccessor.MonitorEntityChanges();

            underlyingEvent.SetValueAndForceNotify(new EntityAddedEvent(unapplicableEntity, mockPool));

            Assert.That(cacheableGroupAccessor.CachedEntities, Is.Empty);
        }
Beispiel #5
0
        public void should_only_cache_applicable_entity_when_applicable_entity_added()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new GroupAccessorToken(new[] { typeof(TestComponentOne), typeof(TestComponentTwo) }, "default");
            var mockPool        = Substitute.For <IPool>();

            var applicableEntity = new Entity(1, mockEventSystem);

            applicableEntity.AddComponent <TestComponentOne>();
            applicableEntity.AddComponent <TestComponentTwo>();

            var unapplicableEntity = new Entity(2, mockEventSystem);

            unapplicableEntity.AddComponent <TestComponentOne>();

            var underlyingEvent = new ReactiveProperty <EntityAddedEvent>(new EntityAddedEvent(applicableEntity, mockPool));

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

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

            cacheableGroupAccessor.MonitorEntityChanges();

            underlyingEvent.SetValueAndForceNotify(new EntityAddedEvent(unapplicableEntity, mockPool));

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(1));
            Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(applicableEntity));
        }
Beispiel #6
0
        public void should_only_remove_applicable_entity_when_entity_removed()
        {
            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 existingEntityOne = new Entity(mockEntityIndexPool.GetId(), new List <IComponent> {
                new TestComponentOne(), new TestComponentTwo()
            }, mockPool, mockReactor);

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

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

            var underlyingEvent = new ReactiveProperty <EntityRemovedEvent>(new EntityRemovedEvent(unapplicableEntity, mockPool));

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

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

            underlyingEvent.SetValueAndForceNotify(new EntityRemovedEvent(existingEntityOne, mockPool));

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(1));
            Assert.That(cacheableGroupAccessor.CachedEntities, Contains.Item(existingEntityTwo));
        }
Beispiel #7
0
        public IGroupAccessor CreateGroupAccessor(IGroup group, string poolName = null)
        {
            var groupAccessorToken = new GroupAccessorToken(group.TargettedComponents.ToArray(), poolName);

            if (_groupAccessors.ContainsKey(groupAccessorToken))
            {
                return(_groupAccessors[groupAccessorToken]);
            }

            var entityMatches = GetEntitiesFor(@group, poolName);
            var groupAccessor = new CacheableGroupAccessor(groupAccessorToken, entityMatches, EventSystem);

            groupAccessor.MonitorEntityChanges();
            _groupAccessors.Add(groupAccessorToken, groupAccessor);

            return(_groupAccessors[groupAccessorToken]);
        }
Beispiel #8
0
        public void should_include_entity_snapshot_on_creation()
        {
            var mockEventSystem = Substitute.For <IEventSystem>();
            var accessorToken   = new GroupAccessorToken(new Type[] { }, "default");

            var dummyEntitySnapshot = new List <IEntity>
            {
                new Entity(Guid.NewGuid(), mockEventSystem),
                new Entity(Guid.NewGuid(), mockEventSystem),
                new Entity(Guid.NewGuid(), mockEventSystem)
            };

            var cacheableGroupAccessor = new CacheableGroupAccessor(accessorToken, dummyEntitySnapshot, mockEventSystem);

            Assert.That(cacheableGroupAccessor.CachedEntities, Has.Count.EqualTo(3));
            Assert.That(cacheableGroupAccessor.CachedEntities[dummyEntitySnapshot[0].Id], Is.EqualTo(dummyEntitySnapshot[0]));
            Assert.That(cacheableGroupAccessor.CachedEntities[dummyEntitySnapshot[1].Id], Is.EqualTo(dummyEntitySnapshot[1]));
            Assert.That(cacheableGroupAccessor.CachedEntities[dummyEntitySnapshot[2].Id], Is.EqualTo(dummyEntitySnapshot[2]));
        }