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