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)); }
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_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)); }
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); }
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]); }