Exemplo n.º 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));
        }
Exemplo n.º 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]));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 7
0
        public CacheableGroupAccessor(GroupAccessorToken accessorToken, IEnumerable <IEntity> initialEntities, IEventSystem eventSystem)
        {
            AccessorToken = accessorToken;
            EventSystem   = eventSystem;

            CachedEntities = new List <IEntity>(initialEntities);
            Subscriptions  = new List <IDisposable>();
        }
Exemplo n.º 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]));
        }
Exemplo n.º 9
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));*/
        }
 public GroupAccessorAddedEvent(GroupAccessorToken groupAccessorToken, IGroupAccessor groupAccessor)
 {
     GroupAccessor      = groupAccessor;
     GroupAccessorToken = groupAccessorToken;
 }
Exemplo n.º 11
0
 public GroupAccessor(GroupAccessorToken accessorToken, IEnumerable <IEntity> entities)
 {
     AccessorToken = accessorToken;
     Entities      = entities;
 }
Exemplo n.º 12
0
 public CacheableGroupAccessor(GroupAccessorToken accessorToken, IEnumerable <IEntity> initialEntities)
 {
     AccessorToken  = accessorToken;
     CachedEntities = new HashSet <IEntity>(initialEntities);
 }