public void TestCombine() { ComponentA a = new ComponentA(); var ti = a.MethodC(); Assert.IsNotNull(ti); }
public void GroupInvokesOnEntityUpdatedWhenReplacingAComponent() { SetupGroupEntities(); var updated = 0; var prevComp = eA.GetComponent(MyTestComponentsLookup.ComponentA); var newComp = new ComponentA(); var g = _defaultContext.GetGroup(Matcher <MyTestEntity> .AllOf(MyTestComponentsLookup.ComponentA)); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; Assert.AreEqual(g, group); Assert.AreEqual(eA, entity); Assert.AreEqual(MyTestComponentsLookup.ComponentA, index); Assert.AreEqual(prevComp, previousComponent); Assert.AreEqual(newComp, newComponent); }; eA.ReplaceComponent(MyTestComponentsLookup.ComponentA, newComp); Assert.AreEqual(1, updated); }
public void TestSimpleAuto() { ComponentA a = new ComponentA(); var ti = a.MethodA(); Assert.IsNotNull(ti); }
public void Before() { var context = Helper.CreateContext(); _e = context.CreateEntity(); _componentA = new ComponentA(); }
public ComponentB( IRequireComponent <ComponentA> componentA, IInstantiateComponent <ComponentD> componentD) { _componentA = componentA.Component; RegisterPublicComponent(componentD.Component); }
public void CreateEntityBeforeUpdate() { EcsService service = EcsServiceFactory.CreateECSManager(); Entity entity = service.CreateEntityFromTemplate <TestEntityTemplate>(); Assert.That(entity.Transform, Is.Not.Null); ComponentA component = entity.GetComponent <ComponentA>(); Assert.That(component.Transform, Is.Not.Null); Assert.That(!component.IsStarted); Assert.That(!component.IsUpdated); Assert.That(!component.IsDrew); Assert.That(!component.IsDestroyed); service.Update(new GameTime()); service.Draw(new GameTime()); Assert.That(component.IsStarted); Assert.That(component.IsUpdated); Assert.That(component.IsDrew); Assert.That(!component.IsDestroyed); }
public void Before() { var pool = Helper.CreatePool(); _e = pool.CreateEntity(); _componentA = new ComponentA(); }
public void GroupInvokesOnEntityRemovedAndOnEntityAddedWhenReplacingComponents() { SetupGroupEntities(); var g = _defaultContext.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { Assert.AreEqual(g, group); Assert.AreEqual(eAB1, entity); Assert.AreEqual(MyTestComponentsLookup.ComponentA, index); Assert.AreEqual(Component.A, component); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { Assert.AreEqual(g, group); Assert.AreEqual(eAB1, entity); Assert.AreEqual(MyTestComponentsLookup.ComponentA, index); Assert.AreEqual(componentA, component); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); Assert.AreEqual(1, didDispatchRemoved); Assert.AreEqual(1, didDispatchAdded); }
public void Before() { _context = Helper.CreateContext(); _context.GetGroup(Matcher <Entity> .AllOf(MyTestComponentsLookup.ComponentA)); _context.GetGroup(Matcher <Entity> .AllOf(MyTestComponentsLookup.ComponentB)); _context.GetGroup(Matcher <Entity> .AllOf(MyTestComponentsLookup.ComponentC)); _context.GetGroup( Matcher <Entity> .AllOf( MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB)); _context.GetGroup( Matcher <Entity> .AllOf( MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentC)); _context.GetGroup( Matcher <Entity> .AllOf( MyTestComponentsLookup.ComponentB, MyTestComponentsLookup.ComponentC)); _context.GetGroup( Matcher <Entity> .AllOf( MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB, MyTestComponentsLookup.ComponentC)); _e = _context.CreateEntity(); _componentA = new ComponentA(); _e.AddComponent(MyTestComponentsLookup.ComponentA, _componentA); }
public void TestNonTx() { ComponentA a = new ComponentA(); var ti = a.MethodB(); Assert.IsNull(ti); }
public ComponentB( IRequireComponent<ComponentA> componentA, IInstantiateComponent<ComponentD> componentD) { _componentA = componentA.Component; RegisterPublicComponent(componentD.Component); }
public void Before() { _pool = Helper.CreatePool(); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentB })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentC })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentC })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentB, CP.ComponentC })); _pool.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB, CP.ComponentC })); _e = _pool.CreateEntity(); _componentA = new ComponentA(); _e.AddComponent(CP.ComponentA, _componentA); }
public void Before() { _context = Helper.CreateContext(); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentA })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentB })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentC })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentC })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentB, CP.ComponentC })); _context.GetGroup(Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB, CP.ComponentC })); _e = _context.CreateEntity(); _componentA = new ComponentA(); _e.AddComponent(CP.ComponentA, _componentA); }
public void DrawDisabledHierarchicalEntities() { EcsService service = EcsServiceFactory.CreateECSManager(); Entity entity = service.CreateEntityFromTemplate <TestEntityTemplate>(); Entity entityChild = service.CreateEntityFromTemplate <TestEntityTemplate>(); entityChild.Transform.Parent = entity.Transform; ComponentA component = entity.GetComponent <ComponentA>(); ComponentA componentChild = entityChild.GetComponent <ComponentA>(); entity.Enabled = false; service.Update(new GameTime()); service.Draw(new GameTime()); Assert.That(entity.EnabledInHierarchy, Is.False); Assert.That(component.IsStarted, Is.True); Assert.That(component.IsDrew, Is.False); Assert.That(entityChild.EnabledInHierarchy, Is.False); Assert.That(componentChild.IsStarted, Is.True); Assert.That(componentChild.IsDrew, Is.False); }
public void Before() { _context = Helper.CreateContext(); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentA })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentB })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentC })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentA, CP.ComponentB })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentA, CP.ComponentC })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentB, CP.ComponentC })); _context.GetGroup(Matcher <Entity> .AllOf(new [] { CP.ComponentA, CP.ComponentB, CP.ComponentC })); _e = _context.CreateEntity(); _componentA = new ComponentA(); _e.AddComponent(CP.ComponentA, _componentA); }
public void ExistingComponentCanBeReplaced() { var component = new ComponentA(); _defaultEntity.ReplaceComponentA(component); AssertHasComponentA(_defaultEntity, component); }
public void GetComponentFromPawn() { Pawn pawn = new Pawn(); ComponentA a = pawn.AddComponent <ComponentA>(new ComponentA()); ComponentB b = pawn.AddComponent <ComponentB>(new ComponentB()); Assert.NotNull(pawn.GetComponent <ComponentA>()); Assert.AreNotEqual(a, b); }
public void Can_resolve_component_with_provided_instance() { IComponentA compA1 = new ComponentA(); container.Register(Component.For <IComponentA>().Instance(compA1)); compA1.Should().Be.SameInstanceAs(container.Resolve <IComponentA>()); container.Register(Component.For <IComponentA>().Instance(compA1).Named("comp.a2")); compA1.Should().Be.SameInstanceAs(container.Resolve <IComponentA>("comp.a2")); }
public void Can_specify_concrete_instance_during_singleton_registration() { var a = new ComponentA(); container.Register(Component.For <IComponentA>().Instance(a)); var ci = container.Components.FindServiceType(typeof(IComponentA)); ci.Instance.Should().Be.SameInstanceAs(a); }
public void UsesComponentPoolWhenCopyingComponents() { _originalEntity.AddComponentA(); var component = new ComponentA(); _targetEntity.GetComponentPool(MyTestComponentsLookup.ComponentA).Push(component); _originalEntity.CopyTo(_targetEntity); Assert.AreEqual(component, _targetEntity.GetComponentA()); }
public void Should_use_parameter_override_when_resolving_component() { var specificCompA = new ComponentA(); container.Register( Component.For<IComponentB>().ImplementedBy<ComponentB>() .With(Parameter.ForKey("compA").Eq(specificCompA)) ); var compb = container.Resolve<IComponentB>(); compb.CompA.Should().Be.SameInstanceAs(specificCompA); }
public void AddComponent_ShouldAddComponentToEntity() { // Arrange var entity = GetNewEntity(); var componentA = new ComponentA(); // Act entity.AddComponent(componentA); // Assert Assert.That(entity.Components.Count, Is.EqualTo(1)); Assert.That(entity.Components.Single(), Is.EqualTo(componentA)); }
public void Should_use_parameter_override_when_resolving_component() { var specificCompA = new ComponentA(); container.Register( Component.For <IComponentB>().ImplementedBy <ComponentB>() .With(Parameter.ForKey("compA").Eq(specificCompA)) ); var compb = container.Resolve <IComponentB>(); compb.CompA.Should().Be.SameInstanceAs(specificCompA); }
public ComponentB(ComponentA componentA) { if (componentA == null) { throw new ArgumentException(nameof(componentA)); } else { _componentA = componentA; } _componentA = componentA ?? throw new ArgumentNullException(nameof(componentA)); }
public void WrongConnect() { int port = 11000; ComponentA a = new ComponentA(); bool b = a.Connect(IPAddress.Parse("127.0.0.1"), port); Thread t = new Thread(new ParameterizedThreadStart(CreateServer)); t.Start(port); synchronize.WaitOne(); b = a.Connect(IPAddress.Parse("127.0.0.1"), port + 1); Assert.AreEqual(false, b); t.Abort(); }
public void CreateEntityWithTwoComponents() { EcsService service = EcsServiceFactory.CreateECSManager(); Entity entity = service.CreateEntityFromTemplate <TwoComponentsEntityTemplate>(); ComponentA componentA = entity.GetComponent <ComponentA>(); ComponentB componentB = entity.GetComponent <ComponentB>(); service.Update(new GameTime()); Assert.That(componentB.ComponentAReference, Is.EqualTo(componentA)); }
public void GetComponent_ShouldReturnComponentByTypeFromEntity() { // Arrange var entity = GetNewEntity(); var componentA = new ComponentA(); entity.AddComponent(componentA); // Act var component = entity.GetComponent <ComponentA>(); // Assert Assert.That(component, Is.EqualTo(componentA)); }
public void RemoveComponent_ShouldRemoveComponentFromEntity() { // Arrange var entity = GetNewEntity(); var componentA = new ComponentA(); entity.AddComponent(componentA); // Act entity.RemoveComponent(componentA); // Assert Assert.That(entity.Components, Is.Empty); }
public void HasComponent_ShouldReturnTrue_WhenAskedFor_ComponentA_and_ThereIs_ComponentA_InEntity() { // Arrange var entity = GetNewEntity(); var componentA = new ComponentA(); entity.AddComponent(componentA); // Act var actual = entity.HasComponent <ComponentA>(); // Assert Assert.That(actual, Is.True); }
public void UpdateDisabledEntity() { EcsService service = EcsServiceFactory.CreateECSManager(); Entity entity = service.CreateEntityFromTemplate <TestEntityTemplate>(); ComponentA component = entity.GetComponent <ComponentA>(); entity.Enabled = false; service.Update(new GameTime()); Assert.That(entity.EnabledInHierarchy, Is.False); Assert.That(component.IsStarted, Is.True); Assert.That(component.IsUpdated, Is.False); }
public void GetComponent_ShouldReturnOnly_ComponentA_WhenThereAreManyComponentsTypes() { // Arrange var entity = GetNewEntity(); var componentA = new ComponentA(); var componentB = new ComponentB(); entity.AddComponent(componentA); entity.AddComponent(componentB); // Act var component = entity.GetComponent <ComponentA>(); // Assert Assert.That(component, Is.EqualTo(componentA)); }
public void UsesComponentPoolWhenCopyingComponents() { _entity.AddComponentA(); var component = new ComponentA(); _target.GetComponentPool(CID.ComponentA).Push(component); #pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable HAA0101 // Array allocation for params parameter _entity.CopyTo(_target); #pragma warning restore HAA0101 // Array allocation for params parameter #pragma warning restore CS0618 // Type or member is obsolete Assert.AreEqual(component, _target.GetComponentA()); }
public void Send() { int port = 11010; ComponentA a = new ComponentA(); string data = "data"; bool b = a.Send(Encoding.UTF8.GetBytes(data)); Assert.AreEqual(false, b); Thread t = new Thread(new ParameterizedThreadStart(CreateServer)); t.Start(port); synchronize.WaitOne(); b = a.Connect(IPAddress.Parse("127.0.0.1"), port); Assert.AreEqual(true, b); b = a.Send(Encoding.UTF8.GetBytes(data)); Assert.AreEqual(true, b); t.Abort(); }
public static Entity ReplaceComponentA(this Entity e, ComponentA component) { return e.ReplaceComponent(CID.ComponentA, component); }
void when_created() { Entity e = null; before = () => { e = this.CreateEntity(); }; context["initial state"] = () => { it["has default ContextInfo"] = () => { e.contextInfo.name.should_be("No Context"); e.contextInfo.componentNames.Length.should_be(CID.TotalComponents); e.contextInfo.componentTypes.should_be_null(); for(int i = 0; i < e.contextInfo.componentNames.Length; i++) { e.contextInfo.componentNames[i].should_be(i.ToString()); } }; it["has custom ContextInfo when set"] = () => { var contextInfo = new ContextInfo(null, null, null); e = new Entity(0, null, contextInfo); e.contextInfo.should_be_same(contextInfo); }; it["throws when attempting to get component at index which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.GetComponentA(); }); it["gets total components count when empty"] = () => { e.totalComponents.should_be(CID.TotalComponents); }; it["gets empty array of components when no components were added"] = () => { e.GetComponents().should_be_empty(); }; it["gets empty array of component indices when no components were added"] = () => { e.GetComponentIndices().should_be_empty(); }; it["doesn't have component at index when no component was added"] = () => { e.HasComponentA().should_be_false(); }; it["doesn't have components at indices when no components were added"] = () => { e.HasComponents(_indicesA).should_be_false(); }; it["doesn't have any components at indices when no components were added"] = () => { e.HasAnyComponent(_indicesA).should_be_false(); }; it["returns entity when adding a component"] = () => { e.AddComponent(0, null).should_be_same(e); }; it["adds a component"] = () => { e.AddComponentA(); assertHasComponentA(e); }; it["throws when attempting to remove a component at index which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.RemoveComponentA(); }); it["replacing a non existing component adds component"] = () => { e.ReplaceComponentA(Component.A); assertHasComponentA(e); }; }; context["when component added"] = () => { before = () => { e.AddComponentA(); }; it["throws when adding a component at the same index twice"] = expect<EntityAlreadyHasComponentException>(() => { e.AddComponentA(); }); it["returns entity when removing a component"] = () => { e.RemoveComponent(CID.ComponentA).should_be_same(e); }; it["removes a component at index"] = () => { e.RemoveComponentA(); assertHasNotComponentA(e); }; it["returns entity when replacing a component"] = () => { e.ReplaceComponent(CID.ComponentA, null).should_be_same(e); }; it["replaces existing component"] = () => { var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); assertHasComponentA(e, newComponentA); }; it["doesn't have components at indices when not all components were added"] = () => { e.HasComponents(_indicesAB).should_be_false(); }; it["has any components at indices when any component was added"] = () => { e.HasAnyComponent(_indicesAB).should_be_true(); }; context["when adding another component"] = () => { before = () => { e.AddComponentB(); }; it["gets all components"] = () => { var components = e.GetComponents(); components.Length.should_be(2); components.should_contain(Component.A); components.should_contain(Component.B); }; it["gets all component indices"] = () => { var componentIndices = e.GetComponentIndices(); componentIndices.Length.should_be(2); componentIndices.should_contain(CID.ComponentA); componentIndices.should_contain(CID.ComponentB); }; it["has other component"] = () => { e.HasComponentB().should_be_true(); }; it["has components at indices when all components were added"] = () => { e.HasComponents(_indicesAB).should_be_true(); }; it["removes all components"] = () => { e.RemoveAllComponents(); e.HasComponentA().should_be_false(); e.HasComponentB().should_be_false(); e.GetComponents().should_be_empty(); e.GetComponentIndices().should_be_empty(); }; it["can ToString and removes *Component suffix"] = () => { e.AddComponent(0, new SomeComponent()); e.Retain(this); e.ToString().should_be("Entity_0(*1)(Some, ComponentA, ComponentB)"); }; it["uses component.ToString()"] = () => { e.AddComponent(0, new NameAgeComponent { name = "Max", age = 42 }); e.ToString().should_be("Entity_0(*0)(NameAge(Max, 42), ComponentA, ComponentB)"); }; it["uses short component name without namespace if ToString is not implemented"] = () => { e.AddComponent(0, new NamespaceComponent()); e.ToString().should_be("Entity_0(*0)(Namespace, ComponentA, ComponentB)"); }; }; }; context["componentPool"] = () => { it["gets component context"] = () => { var componentPool = e.GetComponentPool(CID.ComponentA); componentPool.Count.should_be(0); }; it["gets same component context instance"] = () => { e.GetComponentPool(CID.ComponentA).should_be_same(e.GetComponentPool(CID.ComponentA)); }; it["pushes component to componentPool when removed"] = () => { e.AddComponentA(); var component = e.GetComponentA(); e.RemoveComponentA(); var componentPool = e.GetComponentPool(CID.ComponentA); componentPool.Count.should_be(1); componentPool.Pop().should_be_same(component); }; it["creates new component when componentPool is empty"] = () => { var type = typeof(NameAgeComponent); var component = e.CreateComponent(1, type); component.GetType().should_be(type); var nameAgeComponent = ((NameAgeComponent)component); nameAgeComponent.name.should_be_null(); nameAgeComponent.age.should_be(0); }; it["gets pooled component when componentPool is not empty"] = () => { var component = new NameAgeComponent(); e.AddComponent(1, component); e.RemoveComponent(1); var newComponent = (NameAgeComponent)e.CreateComponent(1, typeof(NameAgeComponent)); newComponent.should_be_same(component); }; }; context["events"] = () => { int didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnComponentAdded when adding a component"] = () => { e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.AddComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.RemoveComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved before pushing component to context"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { var newComponent = entity.CreateComponent(index, component.GetType()); component.should_not_be_same(newComponent); }; e.RemoveComponentA(); }; it["dispatches OnComponentReplaced when replacing a component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); previousComponent.should_be_same(Component.A); newComponent.should_be_same(newComponentA); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with different component"] = () => { var prevComp = new ComponentA(); var newComp = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; e.AddComponent(CID.ComponentA, prevComp); e.ReplaceComponent(CID.ComponentA, newComp); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with same component"] = () => { e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(Component.A); newComponent.should_be_same(Component.A); }; e.AddComponentA(); e.ReplaceComponentA(Component.A); didDispatch.should_be(1); }; it["doesn't dispatch anything when replacing a non existing component with null"] = () => { e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(null); }; it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => { var newComponentA = new ComponentA(); e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when replacing a component with null"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; component.should_be_same(Component.A); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.ReplaceComponentA(null); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing all components"] = () => { e.AddComponentA(); e.AddComponentB(); e.OnComponentRemoved += (entity, index, component) => didDispatch += 1; e.RemoveAllComponents(); didDispatch.should_be(2); }; }; context["reference counting"] = () => { it["retains entity"] = () => { e.retainCount.should_be(0); e.Retain(this); e.retainCount.should_be(1); }; it["releases entity"] = () => { e.Retain(this); e.Release(this); e.retainCount.should_be(0); }; it["throws when releasing more than it has been retained"] = expect<EntityIsNotRetainedByOwnerException>(() => { e.Retain(this); e.Release(this); e.Release(this); }); it["throws when retaining twice with same owner"] = expect<EntityIsAlreadyRetainedByOwnerException>(() => { var owner1 = new object(); e.Retain(owner1); e.Retain(owner1); }); it["throws when releasing with unknown owner"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner = new object(); var unknownOwner = new object(); e.Retain(owner); e.Release(unknownOwner); }); it["throws when releasing with owner which doesn't retain entity anymore"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner1 = new object(); var owner2 = new object(); e.Retain(owner1); e.Retain(owner2); e.Release(owner2); e.Release(owner2); }); context["events"] = () => { it["doesn't dispatch OnEntityReleased when retaining"] = () => { e.OnEntityReleased += delegate { this.Fail(); }; e.Retain(this); }; it["dispatches OnEntityReleased when retain and release"] = () => { var didDispatch = 0; e.OnEntityReleased += entity => { didDispatch += 1; entity.should_be_same(e); }; e.Retain(this); e.Release(this); didDispatch.should_be(1); }; }; }; context["internal caching"] = () => { context["components"] = () => { IComponent[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponents(); }; it["caches components"] = () => { e.GetComponents().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponents().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced with same component"] = () => { e.ReplaceComponentA(Component.A); e.GetComponents().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponents().should_not_be_same(cache); }; }; context["component indices"] = () => { int[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponentIndices(); }; it["caches component indices"] = () => { e.GetComponentIndices().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponentIndices().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponentIndices().should_be_same(cache); }; it["updates cache when adding a new component with ReplaceComponent"] = () => { e.ReplaceComponentC(Component.C); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponentIndices().should_not_be_same(cache); }; }; context["ToString"] = () => { context["when component was added"] = () => { string cache = null; before = () => { e.AddComponentA(); cache = e.ToString(); }; it["caches entity description"] = () => { e.ToString().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.ToString().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; it["updates cache when entity gets retained"] = () => { e.Retain(this); e.ToString().should_not_be_same(cache); }; it["updates cache when entity gets released"] = () => { e.Retain(this); cache = e.ToString(); e.Release(this); e.ToString().should_not_be_same(cache); }; it["released entity has updated cache"] = () => { e.Retain(this); cache = e.ToString(); e.OnEntityReleased += (entity) => { e.ToString().should_not_be_same(cache); }; e.Release(this); }; it["updates cache when RemoveAllComponents is called, even if entity has no components"] = () => { cache = e.ToString(); e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; }; }; }; }
void when_created() { Pool pool = null; before = () => { pool = new Pool(CID.NumComponents); }; it["increments creationIndex"] = () => { pool.CreateEntity().creationIndex.should_be(0); pool.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new Pool(CID.NumComponents, 42).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { pool.GetEntities().should_be_empty(); }; it["gets total entity count"] = () => { pool.Count.should_be(0); }; it["creates entity"] = () => { var e = pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; context["when entity created"] = () => { Entity e = null; before = () => { e = pool.CreateEntity(); e.AddComponentA(); }; it["gets total entity count"] = () => { pool.Count.should_be(1); }; it["has entities that were created with CreateEntity()"] = () => { pool.HasEntity(e).should_be_true(); }; it["doesn't have entities that were not created with CreateEntity()"] = () => { pool.HasEntity(this.CreateEntity()).should_be_false(); }; it["returns all created entities"] = () => { var e2 = pool.CreateEntity(); var entities = pool.GetEntities(); entities.Length.should_be(2); entities.should_contain(e); entities.should_contain(e2); }; it["destroys entity and removes it"] = () => { pool.DestroyEntity(e); pool.HasEntity(e).should_be_false(); pool.Count.should_be(0); pool.GetEntities().should_be_empty(); }; it["destroys an entity and removes all its components"] = () => { pool.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entities"] = () => { pool.CreateEntity(); pool.DestroyAllEntities(); pool.HasEntity(e).should_be_false(); pool.Count.should_be(0); pool.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; }; context["internal caching"] = () => { it["caches entities"] = () => { var entities = pool.GetEntities(); pool.GetEntities().should_be_same(entities); }; it["updates entities cache when creating an entity"] = () => { var entities = pool.GetEntities(); pool.CreateEntity(); pool.GetEntities().should_not_be_same(entities); }; it["updates entities cache when destroying an entity"] = () => { var e = pool.CreateEntity(); var entities = pool.GetEntities(); pool.DestroyEntity(e); pool.GetEntities().should_not_be_same(entities); }; }; context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityCreated when creating a new entity"] = () => { Entity eventEntity = null; pool.OnEntityCreated += (p, entity) => { didDispatch += 1; eventEntity = entity; p.should_be_same(p); }; var e = pool.CreateEntity(); didDispatch.should_be(1); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.OnEntityWillBeDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.HasComponentA().should_be_true(); entity.IsEnabled().should_be_true(); }; pool.DestroyEntity(e); didDispatch.should_be(1); }; it["dispatches OnEntityDestroyed when destroying an entity"] = () => { var e = pool.CreateEntity(); pool.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.HasComponentA().should_be_false(); entity.IsEnabled().should_be_false(); }; pool.DestroyEntity(e); didDispatch.should_be(1); }; it["Entity is released after OnEntityDestroyed"] = () => { var e = pool.CreateEntity(); pool.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.RefCount().should_be(1); var newEntity = pool.CreateEntity(); newEntity.should_not_be_null(); newEntity.should_not_be_same(entity); }; pool.DestroyEntity(e); var reusedEntity = pool.CreateEntity(); reusedEntity.should_be_same(e); didDispatch.should_be(1); }; it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => { var e = pool.CreateEntity(); e.Release(); }); it["dispatches OnGroupCreated when creating a new group"] = () => { Group eventGroup = null; pool.OnGroupCreated += (p, g) => { didDispatch += 1; p.should_be_same(pool); eventGroup = g; }; var group = pool.GetGroup(Matcher.AllOf(0)); didDispatch.should_be(1); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => { pool.GetGroup(Matcher.AllOf(0)); pool.OnGroupCreated += (p, g) => this.Fail(); pool.GetGroup(Matcher.AllOf(0)); }; it["removes all external delegates when destroying an entity"] = () => { var e = pool.CreateEntity(); e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); pool.DestroyEntity(e); var e2 = pool.CreateEntity(); e2.should_be_same(e); e2.AddComponentA(); e2.ReplaceComponentA(Component.A); e2.RemoveComponentA(); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); var entity = pool.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["only returns released entities"] = () => { var e1 = pool.CreateEntity(); e1.Retain(); pool.DestroyEntity(e1); var e2 = pool.CreateEntity(); e2.should_not_be_same(e1); e1.Release(); var e3 = pool.CreateEntity(); e3.should_be_same(e1); }; it["returns new entity"] = () => { var e1 = pool.CreateEntity(); e1.AddComponentA(); pool.DestroyEntity(e1); pool.CreateEntity(); var e2 = pool.CreateEntity(); e2.HasComponent(CID.ComponentA).should_be_false(); e2.should_not_be_same(e1); }; it["sets up entity from pool"] = () => { pool.DestroyEntity(pool.CreateEntity()); var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); var e = pool.CreateEntity(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when entity gets destroyed"] = () => { Entity e = null; before = () => { e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); }; it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["groups"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IMatcher matcherAB = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = pool.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = pool.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = pool.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = pool.GetGroup(matcherAB).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { pool.GetGroup(matcherAB).should_be_same(pool.GetGroup(matcherAB)); }; it["cached group contains newly created matching entity"] = () => { var g = pool.GetGroup(matcherAB); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = pool.GetGroup(matcherAB); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = pool.GetGroup(matcherAB); pool.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["throws when destroying an entity the pool doesn't contain"] = expect<PoolDoesNotContainEntityException>(() => { var e = pool.CreateEntity(); pool.DestroyEntity(e); pool.DestroyEntity(e); }); it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = pool.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; }; }; }
public void Can_resolve_component_with_provided_instance() { IComponentA compA1 = new ComponentA(); container.Register(Component.For<IComponentA>().Instance(compA1)); compA1.Should().Be.SameInstanceAs(container.Resolve<IComponentA>()); container.Register(Component.For<IComponentA>().Instance(compA1).Named("comp.a2")); compA1.Should().Be.SameInstanceAs(container.Resolve<IComponentA>("comp.a2")); }
public Service(ComponentA a) { A = a; }
void when_creating() { Pool pool = null; Entity entity = null; before = () => { pool = new Pool(CID.NumComponents); entity = pool.CreateEntity(); }; context["ComponentBlueprint"] = () => { it["creates a component blueprint from a component without members"] = () => { var component = new ComponentA(); const int index = 42; var componentBlueprint = new ComponentBlueprint(index, component); componentBlueprint.index.should_be(index); componentBlueprint.fullTypeName.should_be(component.GetType().FullName); componentBlueprint.members.Length.should_be(0); }; it["throws when unknown type"] = expect<ComponentBlueprintException>(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "UnknownType"; componentBlueprint.CreateComponent(null); }); it["throws when type doesn't implement IComponent"] = expect<ComponentBlueprintException>(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "string"; componentBlueprint.CreateComponent(null); }); it["creates a component blueprint from a component with members"] = () => { var component = new NameAgeComponent(); component.name = "Max"; component.age = 42; const int index = 24; var componentBlueprint = new ComponentBlueprint(index, component); componentBlueprint.index.should_be(index); componentBlueprint.fullTypeName.should_be(component.GetType().FullName); componentBlueprint.members.Length.should_be(2); componentBlueprint.members[0].name.should_be("name"); componentBlueprint.members[0].value.should_be(component.name); componentBlueprint.members[1].name.should_be("age"); componentBlueprint.members[1].value.should_be(component.age); }; it["creates a component with and sets members values"] = () => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = typeof(ComponentWithFieldsAndProperties).FullName; componentBlueprint.index = CID.ComponentB; componentBlueprint.members = new [] { new SerializableMember("publicField", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; var component = (ComponentWithFieldsAndProperties)componentBlueprint.CreateComponent(entity); component.publicField.should_be("publicFieldValue"); component.publicProperty.should_be("publicPropertyValue"); }; it["throws when invalid member name"] = expect<ComponentBlueprintException>(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.index = 0; componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName; componentBlueprint.members = new [] { new SerializableMember("xxx", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; componentBlueprint.CreateComponent(entity); }); }; context["Blueprint"] = () => { it["creates a blueprint from an entity"] = () => { entity.AddComponentA(); var component = new NameAgeComponent(); component.name = "Max"; component.age = 42; entity.AddComponent(CID.ComponentB, component); var blueprint = new Blueprint("My Pool", "Hero", entity); blueprint.poolIdentifier.should_be("My Pool"); blueprint.name.should_be("Hero"); blueprint.components.Length.should_be(2); blueprint.components[0].index.should_be(CID.ComponentA); blueprint.components[0].fullTypeName.should_be(Component.A.GetType().FullName); blueprint.components[1].index.should_be(CID.ComponentB); blueprint.components[1].fullTypeName.should_be(component.GetType().FullName); }; context["when applying blueprint"] = () => { Blueprint blueprint = null; before = () => { var component1 = new ComponentBlueprint(); component1.index = CID.ComponentA; component1.fullTypeName = typeof(ComponentA).FullName; component1.members = new SerializableMember[0]; var component2 = new ComponentBlueprint(); component2.index = CID.ComponentB; component2.fullTypeName = typeof(NameAgeComponent).FullName; component2.members = new [] { new SerializableMember("name", "Max"), new SerializableMember("age", 42) }; blueprint = new Blueprint(); blueprint.name = "Hero"; blueprint.components = new [] { component1, component2 }; }; it["applies blueprint to entity"] = () => { entity.ApplyBlueprint(blueprint).should_be(entity); entity.GetComponents().Length.should_be(2); entity.GetComponent(CID.ComponentA).GetType().should_be(typeof(ComponentA)); var nameAgeComponent = (NameAgeComponent)entity.GetComponent(CID.ComponentB); nameAgeComponent.GetType().should_be(typeof(NameAgeComponent)); nameAgeComponent.name.should_be("Max"); nameAgeComponent.age.should_be(42); }; it["throws when entity already has a component which should be added from blueprint"] = expect<EntityAlreadyHasComponentException>(() => { entity.AddComponentA(); entity.ApplyBlueprint(blueprint); }); it["can overwrite existing components"] = () => { var nameAgeComponent = new NameAgeComponent(); nameAgeComponent.name = "Jack"; nameAgeComponent.age = 24; entity.AddComponent(CID.ComponentB, nameAgeComponent); entity.ApplyBlueprint(blueprint, true); }; it["uses component from componentPool"] = () => { var component = new ComponentBlueprint(); component.index = CID.ComponentA; component.fullTypeName = typeof(ComponentA).FullName; component.members = new SerializableMember[0]; blueprint = new Blueprint(); blueprint.name = "Hero"; blueprint.components = new [] { component }; var componentA = entity.CreateComponent<ComponentA>(CID.ComponentA); entity.AddComponent(CID.ComponentA, componentA); entity.RemoveComponentA(); entity.ApplyBlueprint(blueprint); entity.GetComponentA().should_be_same(componentA); }; }; }; }
void when_created() { Context ctx = null; before = () => { ctx = new Context(CID.TotalComponents); }; it["increments creationIndex"] = () => { ctx.CreateEntity().creationIndex.should_be(0); ctx.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new Context(CID.TotalComponents, 42, null).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { ctx.GetEntities().should_be_empty(); }; it["gets total entity count"] = () => { ctx.count.should_be(0); }; it["creates entity"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["has default ContextInfo"] = () => { ctx.contextInfo.name.should_be("Unnamed Context"); ctx.contextInfo.componentNames.Length.should_be(CID.TotalComponents); for (int i = 0; i < ctx.contextInfo.componentNames.Length; i++) { ctx.contextInfo.componentNames[i].should_be("Index " + i); } }; it["creates component pools"] = () => { ctx.componentPools.should_not_be_null(); ctx.componentPools.Length.should_be(CID.TotalComponents); }; it["creates entity with component pools"] = () => { var e = ctx.CreateEntity(); e.componentPools.should_be_same(ctx.componentPools); }; it["throws when destroying an entity which the context doesn't contain"] = expect<ContextDoesNotContainEntityException>(() => { var e = ctx.CreateEntity(); ctx.DestroyEntity(e); ctx.DestroyEntity(e); }); it["can ToString"] = () => { ctx.ToString().should_be("Unnamed Context"); }; context["when ContextInfo set"] = () => { ContextInfo contextInfo = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) }; contextInfo = new ContextInfo("My Context", componentNames, componentTypes); ctx = new Context(componentNames.Length, 0, contextInfo); }; it["has custom ContextInfo"] = () => { ctx.contextInfo.should_be_same(contextInfo); }; it["creates entity with same ContextInfo"] = () => { ctx.CreateEntity().contextInfo.should_be_same(contextInfo); }; it["throws when componentNames is not same length as totalComponents"] = expect<ContextInfoException>(() => { new Context(contextInfo.componentNames.Length + 1, 0, contextInfo); }); }; context["when entity created"] = () => { Entity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); }; it["gets total entity count"] = () => { ctx.count.should_be(1); }; it["has entities that were created with CreateEntity()"] = () => { ctx.HasEntity(e).should_be_true(); }; it["doesn't have entities that were not created with CreateEntity()"] = () => { ctx.HasEntity(this.CreateEntity()).should_be_false(); }; it["returns all created entities"] = () => { var e2 = ctx.CreateEntity(); var entities = ctx.GetEntities(); entities.Length.should_be(2); entities.should_contain(e); entities.should_contain(e2); }; it["destroys entity and removes it"] = () => { ctx.DestroyEntity(e); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); }; it["destroys an entity and removes all its components"] = () => { ctx.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entities"] = () => { ctx.CreateEntity(); ctx.DestroyAllEntities(); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["ensures same deterministic order when getting entities after destroying all entities"] = () => { // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project const int numEntities = 10; for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order1 = new int[numEntities]; var entities1 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order1[i] = entities1[i].creationIndex; } ctx.DestroyAllEntities(); ctx.ResetCreationIndex(); for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order2 = new int[numEntities]; var entities2 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order2[i] = entities2[i].creationIndex; } for (int i = 0; i < numEntities; i++) { var index1 = order1[i]; var index2 = order2[i]; index1.should_be(index2); } }; it["throws when destroying all entities and there are still entities retained"] = expect<ContextStillHasRetainedEntitiesException>(() => { ctx.CreateEntity().Retain(new object()); ctx.DestroyAllEntities(); }); }; context["internal caching"] = () => { it["caches entities"] = () => { var entities = ctx.GetEntities(); ctx.GetEntities().should_be_same(entities); }; it["updates entities cache when creating an entity"] = () => { var entities = ctx.GetEntities(); ctx.CreateEntity(); ctx.GetEntities().should_not_be_same(entities); }; it["updates entities cache when destroying an entity"] = () => { var e = ctx.CreateEntity(); var entities = ctx.GetEntities(); ctx.DestroyEntity(e); ctx.GetEntities().should_not_be_same(entities); }; }; context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityCreated when creating a new entity"] = () => { Entity eventEntity = null; ctx.OnEntityCreated += (p, entity) => { didDispatch += 1; eventEntity = entity; p.should_be_same(p); }; var e = ctx.CreateEntity(); didDispatch.should_be(1); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.OnEntityWillBeDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_true(); entity.isEnabled.should_be_true(); p.GetEntities().Length.should_be(0); }; ctx.GetEntities(); ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["dispatches OnEntityDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_false(); entity.isEnabled.should_be_false(); }; ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["entity is released after OnEntityDestroyed"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; entity.retainCount.should_be(1); var newEntity = ctx.CreateEntity(); newEntity.should_not_be_null(); newEntity.should_not_be_same(entity); }; ctx.DestroyEntity(e); var reusedEntity = ctx.CreateEntity(); reusedEntity.should_be_same(e); didDispatch.should_be(1); }; it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => { var e = ctx.CreateEntity(); e.Release(ctx); }); it["dispatches OnGroupCreated when creating a new group"] = () => { Group eventGroup = null; ctx.OnGroupCreated += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; var group = ctx.GetGroup(Matcher.AllOf(0)); didDispatch.should_be(1); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => { ctx.GetGroup(Matcher.AllOf(0)); ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.GetGroup(Matcher.AllOf(0)); }; it["dispatches OnGroupCleared when clearing groups"] = () => { Group eventGroup = null; ctx.OnGroupCleared += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; ctx.GetGroup(Matcher.AllOf(0)); var group2 = ctx.GetGroup(Matcher.AllOf(1)); ctx.ClearGroups(); didDispatch.should_be(2); eventGroup.should_be_same(group2); }; it["removes all external delegates when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; ctx.DestroyEntity(e); var e2 = ctx.CreateEntity(); e2.should_be_same(e); e2.AddComponentA(); e2.ReplaceComponentA(Component.A); e2.RemoveComponentA(); }; it["will not remove external delegates for OnEntityReleased"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased when after being dispatched"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); e.Retain(this); e.Release(this); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased after being dispatched (when delayed release)"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; e.Retain(this); ctx.DestroyEntity(e); didRelease.should_be(0); e.Release(this); didRelease.should_be(1); e.Retain(this); e.Release(this); didRelease.should_be(1); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); var entity = ctx.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["only returns released entities"] = () => { var e1 = ctx.CreateEntity(); e1.Retain(this); ctx.DestroyEntity(e1); var e2 = ctx.CreateEntity(); e2.should_not_be_same(e1); e1.Release(this); var e3 = ctx.CreateEntity(); e3.should_be_same(e1); }; it["returns new entity"] = () => { var e1 = ctx.CreateEntity(); e1.AddComponentA(); ctx.DestroyEntity(e1); ctx.CreateEntity(); var e2 = ctx.CreateEntity(); e2.HasComponent(CID.ComponentA).should_be_false(); e2.should_not_be_same(e1); }; it["sets up entity from pool"] = () => { ctx.DestroyEntity(ctx.CreateEntity()); var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); var e = ctx.CreateEntity(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when entity gets destroyed"] = () => { Entity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); }; it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["groups"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IMatcher matcherAB = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = ctx.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = ctx.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = ctx.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = ctx.GetGroup(matcherAB).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { ctx.GetGroup(matcherAB).should_be_same(ctx.GetGroup(matcherAB)); }; it["cached group contains newly created matching entity"] = () => { var g = ctx.GetGroup(matcherAB); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = ctx.GetGroup(matcherAB); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = ctx.GetGroup(matcherAB); ctx.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = ctx.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; it["group with matcher NoneOf doesn't dispatch OnEntityAdded when destroying entity"] = () => { var e = ctx.CreateEntity() .AddComponentA() .AddComponentB(); var matcher = Matcher.AllOf(CID.ComponentB).NoneOf(CID.ComponentA); var g = ctx.GetGroup(matcher); g.OnEntityAdded += delegate { this.Fail(); }; ctx.DestroyEntity(e); }; context["event timing"] = () => { before = () => { ctx = new Context(CID.TotalComponents); }; it["dispatches group.OnEntityAdded events after all groups are updated"] = () => { var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB)); groupA.OnEntityAdded += delegate { groupB.count.should_be(1); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); }; it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => { ctx = new Context(CID.TotalComponents); var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB)); var groupAB = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); groupB.OnEntityRemoved += delegate { groupAB.count.should_be(0); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentB(); }; }; }; }; context["EntityIndex"] = () => { it["throws when EntityIndex for key doesn't exist"] = expect<ContextEntityIndexDoesNotExistException>(() => { ctx.GetEntityIndex("unknown"); }); it["adds and EntityIndex"] = () => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.GetEntityIndex(componentIndex.ToString()).should_be_same(entityIndex); }; it["throws when adding an EntityIndex with same name"] = expect<ContextEntityIndexDoesAlreadyExistException>(() => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); }); }; context["reset"] = () => { context["groups"] = () => { it["resets and removes groups from context"] = () => { var m = Matcher.AllOf(CID.ComponentA); var groupsCreated = 0; Group createdGroup = null; ctx.OnGroupCreated += (p, g) => { groupsCreated += 1; createdGroup = g; }; var initialGroup = ctx.GetGroup(m); ctx.ClearGroups(); ctx.GetGroup(m); ctx.CreateEntity().AddComponentA(); groupsCreated.should_be(2); createdGroup.should_not_be_same(initialGroup); initialGroup.count.should_be(0); createdGroup.count.should_be(1); }; it["removes all event handlers from groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); var group = ctx.GetGroup(m); group.OnEntityAdded += delegate { this.Fail(); }; ctx.ClearGroups(); var e = ctx.CreateEntity(); e.AddComponentA(); group.HandleEntity(e, CID.ComponentA, Component.A); }; it["releases entities in groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); ctx.GetGroup(m); var entity = ctx.CreateEntity(); entity.AddComponentA(); ctx.ClearGroups(); entity.retainCount.should_be(1); }; }; context["context"] = () => { it["resets creation index"] = () => { ctx.CreateEntity(); ctx.ResetCreationIndex(); ctx.CreateEntity().creationIndex.should_be(0); }; context["removes all event handlers"] = () => { it["removes OnEntityCreated"] = () => { ctx.OnEntityCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.CreateEntity(); }; it["removes OnEntityWillBeDestroyed"] = () => { ctx.OnEntityWillBeDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnEntityDestroyed"] = () => { ctx.OnEntityDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnGroupCreated"] = () => { ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher.AllOf(0)); }; it["removes OnGroupCleared"] = () => { ctx.OnGroupCleared += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher.AllOf(0)); ctx.ClearGroups(); }; }; }; context["component pools"] = () => { before = () => { var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentA(); entity.RemoveComponentB(); }; it["clears all component pools"] = () => { ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(1); ctx.ClearComponentPools(); ctx.componentPools[CID.ComponentA].Count.should_be(0); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["clears a specific component pool"] = () => { ctx.ClearComponentPool(CID.ComponentB); ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["only clears existing component pool"] = () => { ctx.ClearComponentPool(CID.ComponentC); }; }; context["EntityIndex"] = () => { PrimaryEntityIndex<string> entityIndex = null; before = () => { entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(CID.ComponentA)), (e, c) => ((NameAgeComponent)(c)).name); ctx.AddEntityIndex(CID.ComponentA.ToString(), entityIndex); }; it["deactivates EntityIndex"] = () => { var nameAgeComponent = new NameAgeComponent(); nameAgeComponent.name = "Max"; ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent); entityIndex.HasEntity("Max").should_be_true(); ctx.DeactivateAndRemoveEntityIndices(); entityIndex.HasEntity("Max").should_be_false(); }; it["removes EntityIndex"] = expect<ContextEntityIndexDoesNotExistException>(() => { ctx.DeactivateAndRemoveEntityIndices(); ctx.GetEntityIndex(CID.ComponentA.ToString()); }); }; }; context["EntitasCache"] = () => { it["pops new list from list pool"] = () => { var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); var groupAB = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB)); var groupABC = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB, CID.ComponentC)); var didExecute = 0; groupA.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; entity.RemoveComponentA(); }; groupAB.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; groupABC.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; ctx.CreateEntity().AddComponentA(); didExecute.should_be(3); }; }; }
void when_created() { Entity e = null; before = () => { e = this.CreateEntity(); }; it["has component of type when component of that type was added"] = () => { e.AddComponentA(); e.HasComponentA().should_be_true(); }; it["returns entity when adding a component"] = () => { e.AddComponent(0, null).should_be_same(e); }; it["doesn't have component of type when no component of that type was added"] = () => { e.HasComponentA().should_be_false(); }; it["doesn't have components of types when no components of these types were added"] = () => { e.HasComponents(new [] { CID.ComponentA }).should_be_false(); }; it["doesn't have components of types when not all components of these types were added"] = () => { e.AddComponentA(); e.HasComponents(new [] { CID.ComponentA, CID.ComponentB }).should_be_false(); }; it["has components of types when all components of these types were added"] = () => { e.AddComponentA(); e.AddComponentB(); e.HasComponents(new [] { CID.ComponentA, CID.ComponentB }).should_be_true(); }; it["doesn't have any components of types when no components of these types were added"] = () => { e.HasAnyComponent(new [] { CID.ComponentA }).should_be_false(); }; it["has any components of types when any component of these types was added"] = () => { e.AddComponentA(); e.HasAnyComponent(new [] { CID.ComponentA, CID.ComponentB }).should_be_true(); }; it["removes a component of type"] = () => { e.AddComponentA(); e.RemoveComponentA(); e.HasComponentA().should_be_false(); }; it["returns entity when removing a component"] = () => { e.AddComponentA(); e.RemoveComponent(1).should_be_same(e); }; it["gets a component of type"] = () => { e.AddComponentA(); e.GetComponentA().should_be_same(Component.A); }; it["replaces existing component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); e.GetComponentA().should_be_same(newComponentA); }; it["returns entity when replacing a component"] = () => { e.ReplaceComponent(1, null).should_be_same(e); }; it["replacing a non existing component adds component"] = () => { var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); e.GetComponentA().should_be_same(newComponentA); }; it["gets empty array of components when no components were added"] = () => { e.GetComponents().should_be_empty(); }; it["gets empty array of component indices when no components were added"] = () => { e.GetComponentIndices().should_be_empty(); }; it["gets all components"] = () => { e.AddComponentA(); e.AddComponentB(); var allComponents = e.GetComponents(); allComponents.should_contain(Component.A); allComponents.should_contain(Component.B); allComponents.Length.should_be(2); }; it["gets all component indices"] = () => { e.AddComponentA(); e.AddComponentB(); var allComponentIndices = e.GetComponentIndices(); allComponentIndices.should_contain(CID.ComponentA); allComponentIndices.should_contain(CID.ComponentB); allComponentIndices.Length.should_be(2); }; it["removes all components"] = () => { e.AddComponentA(); e.AddComponentB(); e.RemoveAllComponents(); e.HasComponentA().should_be_false(); e.HasComponentB().should_be_false(); e.GetComponents().should_be_empty(); e.GetComponentIndices().should_be_empty(); }; it["can ToString"] = () => { e.AddComponentA(); e.AddComponentB(); e.ToString().should_be("Entity_0(ComponentA, ComponentB)"); }; context["events"] = () => { Entity eventEntity = null; int eventIndex = CID.None; IComponent eventComponent = null; before = () => { eventEntity = null; eventIndex = CID.None; eventComponent = null; }; it["dispatches OnComponentAdded when adding a component"] = () => { e.OnComponentAdded += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.OnComponentReplaced += (entity, index, component) => this.Fail(); e.OnComponentWillBeRemoved += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.AddComponentA(); eventEntity.should_be_same(e); eventIndex.should_be(CID.ComponentA); eventComponent.should_be_same(Component.A); }; it["dispatches OnComponentRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.RemoveComponentA(); eventEntity.should_be_same(e); eventIndex.should_be(CID.ComponentA); eventComponent.should_be_same(Component.A); }; it["dispatches OnComponentWillBeRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentWillBeRemoved += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.RemoveComponentA(); eventEntity.should_be_same(e); eventIndex.should_be(CID.ComponentA); eventComponent.should_be_same(Component.A); }; it["dispatches OnComponentReplaced when replacing a component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); var didReplace = 0; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, component) => { entity.should_be_same(entity); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); didReplace++; }; e.OnComponentWillBeRemoved += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(newComponentA); didReplace.should_be(1); }; it["doesn't dispatch anything when replacing a non existing component with null"] = () => { e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, component) => this.Fail(); e.OnComponentWillBeRemoved += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(null); }; it["dispatches OnComponentWillBeRemoved when called manually and component exists"] = () => { e.AddComponentA(); e.OnComponentWillBeRemoved += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.WillRemoveComponent(CID.ComponentA); eventEntity.should_be_same(e); eventIndex.should_be(CID.ComponentA); eventComponent.should_be_same(Component.A); }; it["doesn't dispatch OnComponentWillBeRemoved when called manually and entity doesn't have"] = () => { e.OnComponentWillBeRemoved += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.WillRemoveComponent(CID.ComponentA); eventEntity.should_be_null(); eventIndex.should_be(CID.None); eventComponent.should_be_null(); }; it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => { e.OnComponentAdded += (entity, index, component) => { eventEntity = entity; eventIndex = index; eventComponent = component; }; e.OnComponentReplaced += (entity, index, component) => this.Fail(); e.OnComponentWillBeRemoved += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); eventEntity.should_be_same(e); eventIndex.should_be(CID.ComponentA); eventComponent.should_be_same(newComponentA); }; it["dispatches OnComponentRemoved when removing all components"] = () => { var removed = 0; e.AddComponentA(); e.AddComponentB(); e.OnComponentRemoved += (entity, index, component) => removed++; e.RemoveAllComponents(); removed.should_be(2); }; }; context["invalid operations"] = () => { it["throws when adding a component of the same type twice"] = expect<EntityAlreadyHasComponentException>(() => { e.AddComponentA(); e.AddComponentA(); }); it["throws when attempting to remove a component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.RemoveComponentA(); }); it["throws when attempting to get component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.GetComponentA(); }); }; context["internal caching"] = () => { context["components"] = () => { it["caches components"] = () => { e.AddComponentA(); var c = e.GetComponents(); e.GetComponents().should_be_same(c); }; it["updates cache when a new component was added"] = () => { e.AddComponentA(); var c = e.GetComponents(); e.AddComponentB(); e.GetComponents().should_not_be_same(c); }; it["updates cache when a component was removed"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponents(); e.RemoveComponentA(); e.GetComponents().should_not_be_same(c); }; it["updates cache when a component was replaced"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponents(); e.ReplaceComponentA(new ComponentA()); e.GetComponents().should_not_be_same(c); }; it["doesn't update cache when a component was replaced with same component"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponents(); e.ReplaceComponentA(Component.A); e.GetComponents().should_be_same(c); }; it["updates cache when all components were removed"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponents(); e.RemoveAllComponents(); e.GetComponents().should_not_be_same(c); }; }; context["component indices"] = () => { it["caches component indices"] = () => { e.AddComponentA(); var c = e.GetComponentIndices(); e.GetComponentIndices().should_be_same(c); }; it["updates cache when a new component was added"] = () => { e.AddComponentA(); var c = e.GetComponentIndices(); e.AddComponentB(); e.GetComponentIndices().should_not_be_same(c); }; it["updates cache when a component was removed"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponentIndices(); e.RemoveComponentA(); e.GetComponentIndices().should_not_be_same(c); }; it["doesn't update cache when a component was replaced"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponentIndices(); e.ReplaceComponentA(new ComponentA()); e.GetComponentIndices().should_be_same(c); }; it["updates cache when adding a new component with ReplaceComponent"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponentIndices(); e.ReplaceComponentC(Component.C); e.GetComponentIndices().should_not_be_same(c); }; it["updates cache when all components were removed"] = () => { e.AddComponentA(); e.AddComponentB(); var c = e.GetComponentIndices(); e.RemoveAllComponents(); e.GetComponentIndices().should_not_be_same(c); }; }; }; }
public void Before() { _e = new Entity(CP.NumComponents); _componentA = new ComponentA(); }
void when_created() { it["increments creationIndex"] = () => { _pool.CreateEntity().creationIndex.should_be(0); _pool.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new Pool(CID.NumComponents, 42).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { _pool.GetEntities().should_be_empty(); }; it["creates entity"] = () => { var e = _pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["gets total entity count"] = () => { _pool.CreateEntity(); _pool.Count.should_be(1); }; it["doesn't have entites that were not created with CreateEntity()"] = () => { _pool.HasEntity(this.CreateEntity()).should_be_false(); }; it["has entites that were created with CreateEntity()"] = () => { _pool.HasEntity(_pool.CreateEntity()).should_be_true(); }; it["returns all created entities"] = () => { var e1 = _pool.CreateEntity(); var e2 = _pool.CreateEntity(); var entities = _pool.GetEntities(); entities.should_contain(e1); entities.should_contain(e2); entities.Length.should_be(2); }; it["destroys entity and removes it"] = () => { var e = _pool.CreateEntity(); _pool.DestroyEntity(e); _pool.HasEntity(e).should_be_false(); }; it["destroys an entity and removes all its components"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); _pool.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entites"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); _pool.CreateEntity(); _pool.DestroyAllEntities(); _pool.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["caches entities"] = () => { _pool.CreateEntity(); var entities1 = _pool.GetEntities(); var entities2 = _pool.GetEntities(); entities1.should_be_same(entities2); _pool.DestroyEntity(_pool.CreateEntity()); _pool.GetEntities().should_not_be_same(entities1); }; context["events"] = () => { it["dispatches OnEntityCreated when creating a new entity"] = () => { Pool eventPool = null; Entity eventEntity = null; _pool.OnEntityCreated += (pool, entity) => { eventPool = pool; eventEntity = entity; }; var e = _pool.CreateEntity(); eventPool.should_be_same(_pool); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying a new entity"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); Pool eventPool = null; Entity eventEntity = null; _pool.OnEntityWillBeDestroyed += (pool, entity) => { eventPool = pool; eventEntity = entity; entity.HasComponentA().should_be_true(); }; _pool.DestroyEntity(e); eventPool.should_be_same(_pool); eventEntity.should_be_same(e); }; it["dispatches OnEntityDestroyed when destroying a new entity"] = () => { var e = _pool.CreateEntity(); Pool eventPool = null; Entity eventEntity = null; _pool.OnEntityDestroyed += (pool, entity) => { eventPool = pool; eventEntity = entity; entity.HasComponentA().should_be_false(); }; _pool.DestroyEntity(e); eventPool.should_be_same(_pool); eventEntity.should_be_same(e); }; it["dispatches OnGroupCreated when creating a new group"] = () => { Pool eventPool = null; Group eventGroup = null; _pool.OnGroupCreated += (pool, g) => { eventPool = pool; eventGroup = g; }; var group = _pool.GetGroup(Matcher.AllOf(0)); eventPool.should_be_same(_pool); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreeated when group alredy exists"] = () => { _pool.GetGroup(Matcher.AllOf(0)); _pool.OnGroupCreated += (pool, g) => this.Fail(); _pool.GetGroup(Matcher.AllOf(0)); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = _pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); _pool.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); _pool.DestroyEntity(e); var entity = _pool.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["returns new entity"] = () => { var e = _pool.CreateEntity(); e.AddComponentA(); _pool.DestroyEntity(e); _pool.CreateEntity(); var entityFromPool = _pool.CreateEntity(); entityFromPool.HasComponent(CID.ComponentA).should_be_false(); entityFromPool.should_not_be_same(e); }; it["sets up entity from pool"] = () => { _pool.DestroyEntity(_pool.CreateEntity()); var g = _pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); var e = _pool.CreateEntity(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when in object pool"] = () => { Entity e = null; before = () => { e = _pool.CreateEntity(); e.AddComponentA(); _pool.DestroyEntity(e); }; it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["get entities"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = _pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IMatcher matcher = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = _pool.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = _pool.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = _pool.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = _pool.GetGroup(matcher).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { _pool.GetGroup(matcher).should_be_same(_pool.GetGroup(matcher)); }; it["cached group contains newly created matching entity"] = () => { var g = _pool.GetGroup(matcher); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = _pool.GetGroup(matcher); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = _pool.GetGroup(matcher); _pool.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["throws when destroying an entity the pool doesn't contain"] = expect<PoolDoesNotContainEntityException>(() => { var e = _pool.CreateEntity(); _pool.DestroyEntity(e); _pool.DestroyEntity(e); }); it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = _pool.GetGroup(matcher); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = _pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; }; }; context["getGroup"] = () => { context["AnyOfCompoundMatcher"] = () => { AllOfMatcher allOfA = null; AllOfMatcher allOfB = null; AnyOfCompoundMatcher compound = null; Group group = null; Entity e = null; before = () => { allOfA = Matcher.AllOf(CID.ComponentA); allOfB = Matcher.AllOf(CID.ComponentB); compound = Matcher.AnyOf(allOfA, allOfB); group = _pool.GetGroup(compound); e = _pool.CreateEntity(); }; it["adds entity when matching"] = () => { e.AddComponentA(); compound.Matches(e).should_be_true(); group.Count.should_be(1); }; it["doesn't add entity when not matching"] = () => { e.AddComponentC(); compound.Matches(e).should_be_false(); group.Count.should_be(0); }; it["removes entity when not matching anymore"] = () => { e.AddComponentA(); e.RemoveComponentA(); group.Count.should_be(0); }; it["doesn't remove entity when still matching"] = () => { e.AddComponentA(); e.AddComponentB(); e.RemoveComponentB(); group.Count.should_be(1); }; }; context["AllOfOfCompoundMatcher containing a NoneOfMatcher"] = () => { AllOfMatcher allOfAB = null; NoneOfMatcher noneOfC = null; AllOfCompoundMatcher compound = null; Group group = null; Entity e = null; before = () => { allOfAB = Matcher.AllOf(CID.ComponentA, CID.ComponentB); noneOfC = Matcher.NoneOf(CID.ComponentC); compound = Matcher.AllOf(allOfAB, noneOfC); group = _pool.GetGroup(compound); e = _pool.CreateEntity(); }; it["adds entity when matching"] = () => { e.AddComponentA(); e.AddComponentB(); compound.Matches(e).should_be_true(); group.Count.should_be(1); }; it["doesn't add entity when not matching"] = () => { e.AddComponentA(); e.AddComponentB(); e.AddComponentC(); compound.Matches(e).should_be_false(); group.Count.should_be(0); }; it["removes entity when not matching anymore"] = () => { e.AddComponentA(); e.AddComponentB(); e.RemoveComponentB(); group.Count.should_be(0); }; it["doesn't remove entity when still matching"] = () => { e.AddComponentA(); e.AddComponentB(); e.AddComponentC(); e.RemoveComponentC(); group.Count.should_be(1); }; }; }; }
public ComponentF(ComponentA a) { Assert.IsNotNull(a); }
public ComponentC(ComponentA a, ComponentB b) { A = a; B = b; }
void when_entity() { context["when adding ComponentSuffix"] = () => { it["doesn't add component suffix to string ending with ComponentSuffix"] = () => { const string str = "Position" + EntityExtension.COMPONENT_SUFFIX; str.AddComponentSuffix().should_be_same(str); }; it["add ComponentSuffix to string not ending with ComponentSuffix"] = () => { const string str = "Position"; str.AddComponentSuffix().should_be("Position" + EntityExtension.COMPONENT_SUFFIX); }; }; context["when removeing ComponentSuffix"] = () => { it["doesn't change string when not ending with ComponentSuffix"] = () => { const string str = "Position"; str.RemoveComponentSuffix().should_be_same(str); }; it["removes ComponentSuffix when ending with ComponentSuffix"] = () => { const string str = "Position" + EntityExtension.COMPONENT_SUFFIX; str.RemoveComponentSuffix().should_be("Position"); }; }; context["when copying components"] = () => { Context ctx = null; Entity entity = null; Entity target = null; NameAgeComponent nameAge = null; before = () => { ctx = new Context(CID.TotalComponents); entity = ctx.CreateEntity(); target = ctx.CreateEntity(); nameAge = new NameAgeComponent { name = "Max", age = 42 }; }; it["doesn't change entity if original doesn't have any components"] = () => { entity.CopyTo(target); entity.creationIndex.should_be(0); target.creationIndex.should_be(1); target.GetComponents().Length.should_be(0); }; it["adds copies of all components to target entity"] = () => { entity.AddComponentA(); entity.AddComponent(CID.ComponentB, nameAge); entity.CopyTo(target); target.GetComponents().Length.should_be(2); target.HasComponentA().should_be_true(); target.HasComponentB().should_be_true(); target.GetComponentA().should_not_be_same(Component.A); target.GetComponent(CID.ComponentB).should_not_be_same(nameAge); var clonedComponent = (NameAgeComponent)target.GetComponent(CID.ComponentB); clonedComponent.name.should_be(nameAge.name); clonedComponent.age.should_be(nameAge.age); }; it["throws when target already has a component at index"] = base.expect<EntityAlreadyHasComponentException>(() => { entity.AddComponentA(); entity.AddComponent(CID.ComponentB, nameAge); var component = new NameAgeComponent(); target.AddComponent(CID.ComponentB, component); entity.CopyTo(target); }); it["replaces existing components when overwrite is set"] = () => { entity.AddComponentA(); entity.AddComponent(CID.ComponentB, nameAge); var component = new NameAgeComponent(); target.AddComponent(CID.ComponentB, component); entity.CopyTo(target, true); var copy = target.GetComponent(CID.ComponentB); copy.should_not_be_same(nameAge); copy.should_not_be_same(component); ((NameAgeComponent)copy).name.should_be(nameAge.name); ((NameAgeComponent)copy).age.should_be(nameAge.age); }; it["only adds copies of specified components to target entity"] = () => { entity.AddComponentA(); entity.AddComponentB(); entity.AddComponentC(); entity.CopyTo(target, false, CID.ComponentB, CID.ComponentC); target.GetComponents().Length.should_be(2); target.HasComponentB().should_be_true(); target.HasComponentC().should_be_true(); }; it["uses component pool"] = () => { entity.AddComponentA(); var component = new ComponentA(); target.GetComponentPool(CID.ComponentA).Push(component); entity.CopyTo(target); target.GetComponentA().should_be_same(component); }; }; }
void when_created() { Entity e = null; before = () => { e = this.CreateEntity(); }; context["initial state"] = () => { it["throws when attempting to get component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.GetComponentA(); }); it["gets empty array of components when no components were added"] = () => { e.GetComponents().should_be_empty(); }; it["gets empty array of component indices when no components were added"] = () => { e.GetComponentIndices().should_be_empty(); }; it["doesn't have component of type when no component of that type was added"] = () => { e.HasComponentA().should_be_false(); }; it["doesn't have components of types when no components of these types were added"] = () => { e.HasComponents(_indicesA).should_be_false(); }; it["doesn't have any components of types when no components of these types were added"] = () => { e.HasAnyComponent(_indicesA).should_be_false(); }; it["returns entity when adding a component"] = () => { e.AddComponent(0, null).should_be_same(e); }; it["adds a component"] = () => { e.AddComponentA(); assertHasComponentA(e); }; it["throws when attempting to remove a component of type which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.RemoveComponentA(); }); it["replacing a non existing component adds component"] = () => { e.ReplaceComponentA(Component.A); assertHasComponentA(e); }; }; context["when component added"] = () => { before = () => { e.AddComponentA(); }; it["throws when adding a component of the same type twice"] = expect<EntityAlreadyHasComponentException>(() => { e.AddComponentA(); e.AddComponentA(); }); it["returns entity when removing a component"] = () => { e.RemoveComponent(CID.ComponentA).should_be_same(e); }; it["removes a component of type"] = () => { e.RemoveComponentA(); assertHasNotComponentA(e); }; it["returns entity when replacing a component"] = () => { e.ReplaceComponent(CID.ComponentA, null).should_be_same(e); }; it["replaces existing component"] = () => { var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); assertHasComponentA(e, newComponentA); }; it["doesn't have components of types when not all components of these types were added"] = () => { e.HasComponents(_indicesAB).should_be_false(); }; it["has any components of types when any component of these types was added"] = () => { e.HasAnyComponent(_indicesAB).should_be_true(); }; context["when adding another component"] = () => { before = () => { e.AddComponentB(); }; it["gets all components"] = () => { var components = e.GetComponents(); components.Length.should_be(2); components.should_contain(Component.A); components.should_contain(Component.B); }; it["gets all component indices"] = () => { var componentIndices = e.GetComponentIndices(); componentIndices.Length.should_be(2); componentIndices.should_contain(CID.ComponentA); componentIndices.should_contain(CID.ComponentB); }; it["has other component"] = () => { e.HasComponentB().should_be_true(); }; it["has components of types when all components of these types were added"] = () => { e.HasComponents(_indicesAB).should_be_true(); }; it["removes all components"] = () => { e.RemoveAllComponents(); e.HasComponentA().should_be_false(); e.HasComponentB().should_be_false(); e.GetComponents().should_be_empty(); e.GetComponentIndices().should_be_empty(); }; it["can ToString"] = () => { e.AddComponent(0, new SomeComponent()); e.Retain(this); e.ToString().should_be("Entity_0(1)(Some, ComponentA, ComponentB)"); }; }; }; context["events"] = () => { int didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnComponentAdded when adding a component"] = () => { e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.AddComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.RemoveComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentReplaced when replacing a component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); previousComponent.should_be_same(Component.A); newComponent.should_be_same(newComponentA); }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with different component"] = () => { var prevComp = new ComponentA(); var newComp = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; e.AddComponent(CID.ComponentA, prevComp); e.ReplaceComponent(CID.ComponentA, newComp); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with same component"] = () => { e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(Component.A); newComponent.should_be_same(Component.A); }; e.AddComponentA(); e.ReplaceComponentA(Component.A); didDispatch.should_be(1); }; it["doesn't dispatch anything when replacing a non existing component with null"] = () => { e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(null); }; it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => { var newComponentA = new ComponentA(); e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); }; e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when replacing a component with null"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.ReplaceComponentA(null); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing all components"] = () => { e.AddComponentA(); e.AddComponentB(); e.OnComponentRemoved += (entity, index, component) => didDispatch += 1; e.RemoveAllComponents(); didDispatch.should_be(2); }; }; context["reference counting"] = () => { it["retains entity"] = () => { e.retainCount.should_be(0); e.Retain(this); e.retainCount.should_be(1); }; it["releases entity"] = () => { e.Retain(this); e.Release(this); e.retainCount.should_be(0); }; it["throws when releasing more than it has been retained"] = expect<EntityIsNotRetainedByOwnerException>(() => { e.Retain(this); e.Release(this); e.Release(this); }); it["throws when retaining twice with same owner"] = expect<EntityIsAlreadyRetainedByOwnerException>(() => { var owner1 = new object(); e.Retain(owner1); e.Retain(owner1); }); it["throws when releasing with unknown owner"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner = new object(); var unknownOwner = new object(); e.Retain(owner); e.Release(unknownOwner); }); it["throws when releasing with owner which doesn't retain entity anymore"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner1 = new object(); var owner2 = new object(); e.Retain(owner1); e.Retain(owner2); e.Release(owner2); e.Release(owner2); }); context["events"] = () => { it["doesn't dispatch OnEntityReleased when retaining"] = () => { e.OnEntityReleased += entity => this.Fail(); e.Retain(this); }; it["dispatches OnEntityReleased when retain and release"] = () => { var didDispatch = 0; e.OnEntityReleased += entity => { didDispatch += 1; entity.should_be_same(e); }; e.Retain(this); e.Release(this); }; }; }; context["internal caching"] = () => { context["components"] = () => { IComponent[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponents(); }; it["caches components"] = () => { e.GetComponents().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponents().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced with same component"] = () => { e.ReplaceComponentA(Component.A); e.GetComponents().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponents().should_not_be_same(cache); }; }; context["component indices"] = () => { int[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponentIndices(); }; it["caches component indices"] = () => { e.GetComponentIndices().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponentIndices().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponentIndices().should_be_same(cache); }; it["updates cache when adding a new component with ReplaceComponent"] = () => { e.ReplaceComponentC(Component.C); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponentIndices().should_not_be_same(cache); }; }; context["ToString"] = () => { context["when component was added"] = () => { string cache = null; before = () => { e.AddComponentA(); cache = e.ToString(); }; it["caches entity description"] = () => { e.ToString().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.ToString().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.ToString().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; }; it["updates cache when RemoveAllComponents is called, even if entity has no components"] = () => { var str = e.ToString(); e.RemoveAllComponents(); e.ToString().should_not_be_same(str); }; }; }; }
void when_created() { Pool pool = null; before = () => { pool = new Pool(CID.NumComponents); }; it["increments creationIndex"] = () => { pool.CreateEntity().creationIndex.should_be(0); pool.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new Pool(CID.NumComponents, 42, null).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { pool.GetEntities().should_be_empty(); }; it["gets total entity count"] = () => { pool.count.should_be(0); }; it["creates entity"] = () => { var e = pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["has default PoolMetaData"] = () => { pool.metaData.poolName.should_be("Unnamed Pool"); pool.metaData.componentNames.Length.should_be(CID.NumComponents); for (int i = 0; i < pool.metaData.componentNames.Length; i++) { pool.metaData.componentNames[i].should_be("Index " + i); } }; it["creates component pools"] = () => { pool.componentPools.should_not_be_null(); pool.componentPools.Length.should_be(CID.NumComponents); }; it["creates entity with component pools"] = () => { var e = pool.CreateEntity(); e.componentPools.should_be_same(pool.componentPools); }; it["throws when destroying an entity the pool doesn't contain"] = expect<PoolDoesNotContainEntityException>(() => { var e = pool.CreateEntity(); pool.DestroyEntity(e); pool.DestroyEntity(e); }); it["can ToString"] = () => { pool.ToString().should_be("Unnamed Pool"); }; context["when PoolMetaData set"] = () => { PoolMetaData metaData = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; metaData = new PoolMetaData("My Pool", componentNames); pool = new Pool(componentNames.Length, 0, metaData); }; it["has custom PoolMetaData"] = () => { pool.metaData.should_be_same(metaData); }; it["creates entity with same PoolMetaData"] = () => { pool.CreateEntity().poolMetaData.should_be_same(metaData); }; it["throws when componentNames is not same length as totalComponents"] = expect<PoolMetaDataException>(() => { new Pool(metaData.componentNames.Length + 1, 0, metaData); }); }; context["when entity created"] = () => { Entity e = null; before = () => { e = pool.CreateEntity(); e.AddComponentA(); }; it["gets total entity count"] = () => { pool.count.should_be(1); }; it["has entities that were created with CreateEntity()"] = () => { pool.HasEntity(e).should_be_true(); }; it["doesn't have entities that were not created with CreateEntity()"] = () => { pool.HasEntity(this.CreateEntity()).should_be_false(); }; it["returns all created entities"] = () => { var e2 = pool.CreateEntity(); var entities = pool.GetEntities(); entities.Length.should_be(2); entities.should_contain(e); entities.should_contain(e2); }; it["destroys entity and removes it"] = () => { pool.DestroyEntity(e); pool.HasEntity(e).should_be_false(); pool.count.should_be(0); pool.GetEntities().should_be_empty(); }; it["destroys an entity and removes all its components"] = () => { pool.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entities"] = () => { pool.CreateEntity(); pool.DestroyAllEntities(); pool.HasEntity(e).should_be_false(); pool.count.should_be(0); pool.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["ensures same deterministic order when getting entities after destroying all entities"] = () => { // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project const int numEntities = 10; for (int i = 0; i < numEntities; i++) { pool.CreateEntity(); } var order1 = new int[numEntities]; var entities1 = pool.GetEntities(); for (int i = 0; i < numEntities; i++) { order1[i] = entities1[i].creationIndex; } pool.DestroyAllEntities(); pool.ResetCreationIndex(); for (int i = 0; i < numEntities; i++) { pool.CreateEntity(); } var order2 = new int[numEntities]; var entities2 = pool.GetEntities(); for (int i = 0; i < numEntities; i++) { order2[i] = entities2[i].creationIndex; } for (int i = 0; i < numEntities; i++) { var index1 = order1[i]; var index2 = order2[i]; index1.should_be(index2); } }; it["throws when destroying all entities and there are still entities retained"] = expect<PoolStillHasRetainedEntitiesException>(() => { pool.CreateEntity().Retain(new object()); pool.DestroyAllEntities(); }); }; context["internal caching"] = () => { it["caches entities"] = () => { var entities = pool.GetEntities(); pool.GetEntities().should_be_same(entities); }; it["updates entities cache when creating an entity"] = () => { var entities = pool.GetEntities(); pool.CreateEntity(); pool.GetEntities().should_not_be_same(entities); }; it["updates entities cache when destroying an entity"] = () => { var e = pool.CreateEntity(); var entities = pool.GetEntities(); pool.DestroyEntity(e); pool.GetEntities().should_not_be_same(entities); }; }; context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityCreated when creating a new entity"] = () => { Entity eventEntity = null; pool.OnEntityCreated += (p, entity) => { didDispatch += 1; eventEntity = entity; p.should_be_same(p); }; var e = pool.CreateEntity(); didDispatch.should_be(1); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.OnEntityWillBeDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.HasComponentA().should_be_true(); entity.IsEnabled().should_be_true(); }; pool.DestroyEntity(e); didDispatch.should_be(1); }; it["dispatches OnEntityDestroyed when destroying an entity"] = () => { var e = pool.CreateEntity(); pool.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.HasComponentA().should_be_false(); entity.IsEnabled().should_be_false(); }; pool.DestroyEntity(e); didDispatch.should_be(1); }; it["Entity is released after OnEntityDestroyed"] = () => { var e = pool.CreateEntity(); pool.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(pool); entity.should_be_same(e); entity.retainCount.should_be(1); var newEntity = pool.CreateEntity(); newEntity.should_not_be_null(); newEntity.should_not_be_same(entity); }; pool.DestroyEntity(e); var reusedEntity = pool.CreateEntity(); reusedEntity.should_be_same(e); didDispatch.should_be(1); }; it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => { var e = pool.CreateEntity(); e.Release(pool); }); it["dispatches OnGroupCreated when creating a new group"] = () => { Group eventGroup = null; pool.OnGroupCreated += (p, g) => { didDispatch += 1; p.should_be_same(pool); eventGroup = g; }; var group = pool.GetGroup(Matcher.AllOf(0)); didDispatch.should_be(1); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => { pool.GetGroup(Matcher.AllOf(0)); pool.OnGroupCreated += (p, g) => this.Fail(); pool.GetGroup(Matcher.AllOf(0)); }; it["dispatches OnGroupCleared when clearing groups"] = () => { Group eventGroup = null; pool.OnGroupCleared += (p, g) => { didDispatch += 1; p.should_be_same(pool); eventGroup = g; }; pool.GetGroup(Matcher.AllOf(0)); var group2 = pool.GetGroup(Matcher.AllOf(1)); pool.ClearGroups(); didDispatch.should_be(2); eventGroup.should_be_same(group2); }; it["removes all external delegates when destroying an entity"] = () => { var e = pool.CreateEntity(); e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); pool.DestroyEntity(e); var e2 = pool.CreateEntity(); e2.should_be_same(e); e2.AddComponentA(); e2.ReplaceComponentA(Component.A); e2.RemoveComponentA(); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = pool.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); var entity = pool.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["only returns released entities"] = () => { var e1 = pool.CreateEntity(); e1.Retain(this); pool.DestroyEntity(e1); var e2 = pool.CreateEntity(); e2.should_not_be_same(e1); e1.Release(this); var e3 = pool.CreateEntity(); e3.should_be_same(e1); }; it["returns new entity"] = () => { var e1 = pool.CreateEntity(); e1.AddComponentA(); pool.DestroyEntity(e1); pool.CreateEntity(); var e2 = pool.CreateEntity(); e2.HasComponent(CID.ComponentA).should_be_false(); e2.should_not_be_same(e1); }; it["sets up entity from pool"] = () => { pool.DestroyEntity(pool.CreateEntity()); var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); var e = pool.CreateEntity(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when entity gets destroyed"] = () => { Entity e = null; before = () => { e = pool.CreateEntity(); e.AddComponentA(); pool.DestroyEntity(e); }; it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["groups"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IMatcher matcherAB = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = pool.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = pool.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = pool.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = pool.GetGroup(matcherAB).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { pool.GetGroup(matcherAB).should_be_same(pool.GetGroup(matcherAB)); }; it["cached group contains newly created matching entity"] = () => { var g = pool.GetGroup(matcherAB); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = pool.GetGroup(matcherAB); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = pool.GetGroup(matcherAB); pool.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = pool.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA })); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; context["event timing"] = () => { before = () => { pool = new Pool(CID.NumComponents); }; it["dispatches group.OnEntityAdded events after all groups are updated"] = () => { var groupA = pool.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); var groupB = pool.GetGroup(Matcher.AllOf(CID.ComponentB)); groupA.OnEntityAdded += delegate { groupB.count.should_be(1); }; var entity = pool.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); }; it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => { pool = new Pool(CID.NumComponents); var groupB = pool.GetGroup(Matcher.AllOf(CID.ComponentB)); var groupA = pool.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); groupB.OnEntityRemoved += delegate { groupA.count.should_be(0); }; var entity = pool.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentB(); }; }; }; }; context["reset"] = () => { context["groups"] = () => { it["resets and removes groups from pool"] = () => { var m = Matcher.AllOf(CID.ComponentA); var groupsCreated = 0; Group createdGroup = null; pool.OnGroupCreated += (p, g) => { groupsCreated += 1; createdGroup = g; }; var initialGroup = pool.GetGroup(m); pool.ClearGroups(); pool.GetGroup(m); pool.CreateEntity().AddComponentA(); groupsCreated.should_be(2); createdGroup.should_not_be_same(initialGroup); initialGroup.count.should_be(0); createdGroup.count.should_be(1); }; it["removes all event handlers from groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); var group = pool.GetGroup(m); group.OnEntityAdded += (g, entity, index, component) => this.Fail(); pool.ClearGroups(); var e = pool.CreateEntity(); e.AddComponentA(); group.HandleEntity(e, CID.ComponentA, Component.A); }; it["releases entities in groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); pool.GetGroup(m); var entity = pool.CreateEntity(); entity.AddComponentA(); pool.ClearGroups(); entity.retainCount.should_be(1); }; }; context["pool"] = () => { it["resets creation index"] = () => { pool.CreateEntity(); pool.ResetCreationIndex(); pool.CreateEntity().creationIndex.should_be(0); }; }; context["component pools"] = () => { before = () => { var entity = pool.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentA(); entity.RemoveComponentB(); }; it["clears all component pools"] = () => { pool.componentPools[CID.ComponentA].Count.should_be(1); pool.componentPools[CID.ComponentB].Count.should_be(1); pool.ClearComponentPools(); pool.componentPools[CID.ComponentA].Count.should_be(0); pool.componentPools[CID.ComponentB].Count.should_be(0); }; it["clears a specific component pool"] = () => { pool.ClearComponentPool(CID.ComponentB); pool.componentPools[CID.ComponentA].Count.should_be(1); pool.componentPools[CID.ComponentB].Count.should_be(0); }; it["only clears existing component pool"] = () => { pool.ClearComponentPool(CID.ComponentC); }; }; }; }
public EntityA( IInstantiateComponent<ComponentA> componentA) { ComponentA = componentA.Component; this.RegisterPublicComponent(componentA); }
void when_created() { Entity eA1 = null; Entity eA2 = null; before = () => { _groupA = new Group(Matcher.AllOf(new [] { CID.ComponentA })); eA1 = this.CreateEntity(); eA1.AddComponentA(); eA2 = this.CreateEntity(); eA2.AddComponentA(); }; context["initial state"] = () => { it["doesn't have entities which haven't been added"] = () => { _groupA.GetEntities().should_be_empty(); }; it["is empty"] = () => { _groupA.count.should_be(0); }; it["doesn't contain entity"] = () => { _groupA.ContainsEntity(eA1).should_be_false(); }; }; context["when entity is matching"] = () => { before = () => { handleSilently(eA1); }; it["adds matching entity"] = () => { assertContains(eA1); }; it["doesn't add same entity twice"] = () => { handleSilently(eA1); assertContains(eA1); }; context["when entity doesn't match anymore"] = () => { before = () => { eA1.RemoveComponentA(); handleSilently(eA1); }; it["removes entity"] = () => { assertContainsNot(eA1); }; }; }; it["doesn't add entity when not matching"] = () => { var e = this.CreateEntity(); e.AddComponentB(); handleSilently(e); assertContainsNot(e); }; it["gets null when single entity does not exist"] = () => { _groupA.GetSingleEntity().should_be_null(); }; it["gets single entity"] = () => { handleSilently(eA1); _groupA.GetSingleEntity().should_be_same(eA1); }; it["throws when attempting to get single entity and multiple matching entities exist"] = expect<SingleEntityException>(() => { handleSilently(eA1); handleSilently(eA2); _groupA.GetSingleEntity(); }); context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityAdded when matching entity added"] = () => { _groupA.OnEntityAdded += (group, entity, index, component) => { didDispatch++; group.should_be_same(_groupA); entity.should_be_same(eA1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; _groupA.OnEntityRemoved += (group, entity, index, component) => this.Fail(); _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => this.Fail(); handleAddEA(eA1); didDispatch.should_be(1); }; it["doesn't dispatches OnEntityAdded when matching entity already has been added"] = () => { handleAddEA(eA1); _groupA.OnEntityAdded += (group, entity, index, component) => didDispatch++; _groupA.OnEntityRemoved += (group, entity, index, component) => this.Fail(); _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => this.Fail(); handleAddEA(eA1); didDispatch.should_be(0); }; it["doesn't dispatches OnEntityAdded when entity is not matching"] = () => { var e = this.CreateEntity(); e.AddComponentB(); _groupA.OnEntityAdded += (group, entity, index, component) => this.Fail(); _groupA.OnEntityRemoved += (group, entity, index, component) => this.Fail(); _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => this.Fail(); handleAddEB(e); }; it["dispatches OnEntityRemoved when entity got removed"] = () => { handleSilently(eA1); _groupA.OnEntityRemoved += (group, entity, index, component) => { didDispatch++; group.should_be_same(_groupA); entity.should_be_same(eA1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; _groupA.OnEntityAdded += (group, entity, index, component) => this.Fail(); _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => this.Fail(); eA1.RemoveComponentA(); handleRemoveEA(eA1, Component.A); didDispatch.should_be(1); }; it["doesn't dispatch OnEntityRemoved when entity didn't get removed"] = () => { _groupA.OnEntityRemoved += (group, entity, index, component) => this.Fail(); eA1.RemoveComponentA(); handleRemoveEA(eA1, Component.A); }; it["dispatches OnEntityRemoved, OnEntityAdded and OnEntityUpdated when updating"] = () => { handleSilently(eA1); var removed = 0; var added = 0; var updated = 0; var newComponentA = new ComponentA(); _groupA.OnEntityRemoved += (group, entity, index, component) => { removed += 1; group.should_be(_groupA); entity.should_be(eA1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; _groupA.OnEntityAdded += (group, entity, index, component) => { added += 1; group.should_be(_groupA); entity.should_be(eA1); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); }; _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be(_groupA); entity.should_be(eA1); index.should_be(CID.ComponentA); previousComponent.should_be_same(Component.A); newComponent.should_be_same(newComponentA); }; updateEA(eA1, newComponentA); removed.should_be(1); added.should_be(1); updated.should_be(1); }; it["doesn't dispatch OnEntityRemoved and OnEntityAdded when updating when group doesn't contain entity"] = () => { _groupA.OnEntityRemoved += (group, entity, index, component) => this.Fail(); _groupA.OnEntityAdded += (group, entity, index, component) => this.Fail(); _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => this.Fail(); updateEA(eA1, new ComponentA()); }; }; context["internal caching"] = () => { context["GetEntities()"] = () => { Entity[] cache = null; before = () => { handleSilently(eA1); cache = _groupA.GetEntities(); }; it["gets cached entities"] = () => { _groupA.GetEntities().should_be_same(cache); }; it["updates cache when adding a new matching entity"] = () => { handleSilently(eA2); _groupA.GetEntities().should_not_be_same(cache); }; it["doesn't update cache when attempting to add a not matching entity"] = () => { var e = this.CreateEntity(); handleSilently(e); _groupA.GetEntities().should_be_same(cache); }; it["updates cache when removing an entity"] = () => { eA1.RemoveComponentA(); handleSilently(eA1); _groupA.GetEntities().should_not_be_same(cache); }; it["doesn't update cache when attempting to remove an entity that wasn't added before"] = () => { eA2.RemoveComponentA(); handleSilently(eA2); _groupA.GetEntities().should_be_same(cache); }; }; context["SingleEntity()"] = () => { Entity cache = null; before = () => { handleSilently(eA1); cache = _groupA.GetSingleEntity(); }; it["gets cached singleEntities"] = () => { _groupA.GetSingleEntity().should_be_same(cache); }; it["updates cache when new single entity was added"] = () => { eA1.RemoveComponentA(); handleSilently(eA1); handleSilently(eA2); _groupA.GetSingleEntity().should_not_be_same(cache); }; it["updates cache when single entity is removed"] = () => { eA1.RemoveComponentA(); handleSilently(eA1); _groupA.GetSingleEntity().should_not_be_same(cache); }; }; }; context["reference counting"] = () => { it["retains matched entity"] = () => { eA1.refCount.should_be(0); handleSilently(eA1); eA1.refCount.should_be(1); }; it["releases removed entity"] = () => { handleSilently(eA1); eA1.RemoveComponentA(); handleSilently(eA1); eA1.refCount.should_be(0); }; it["invalidates entitiesCache (silent mode)"] = () => { eA1.OnEntityReleased += entity => { _groupA.GetEntities().Length.should_be(0); }; handleSilently(eA1); _groupA.GetEntities(); eA1.RemoveComponentA(); handleSilently(eA1); }; it["invalidates entitiesCache"] = () => { eA1.OnEntityReleased += entity => { _groupA.GetEntities().Length.should_be(0); }; handleAddEA(eA1); _groupA.GetEntities(); eA1.RemoveComponentA(); handleRemoveEA(eA1, Component.A); }; it["invalidates singleEntityCache (silent mode)"] = () => { eA1.OnEntityReleased += entity => { _groupA.GetSingleEntity().should_be_null(); }; handleSilently(eA1); _groupA.GetSingleEntity(); eA1.RemoveComponentA(); handleSilently(eA1); }; it["invalidates singleEntityCache"] = () => { eA1.OnEntityReleased += entity => { _groupA.GetSingleEntity().should_be_null(); }; handleAddEA(eA1); _groupA.GetSingleEntity(); eA1.RemoveComponentA(); handleRemoveEA(eA1, Component.A); }; it["retains entity until removed"] = () => { handleAddEA(eA1); var didDispatch = 0; _groupA.OnEntityRemoved += (group, entity, index, component) => { didDispatch += 1; entity.refCount.should_be(1); }; eA1.RemoveComponentA(); handleRemoveEA(eA1, Component.A); didDispatch.should_be(1); eA1.refCount.should_be(0); }; }; it["can ToString"] = () => { var m = Matcher.AllOf(Matcher.AllOf(0), Matcher.AllOf(1)); var group = new Group(m); group.ToString().should_be("Group(AllOf(0, 1))"); }; }
public ComponentB(ComponentA a) { A = a; }
public static void ReplaceComponentA(this Entity e, ComponentA component) { e.ReplaceComponent(CID.ComponentA, component); }
public EntityD( IRequireComponent<ComponentA> componentA) { ComponentA = componentA.Component; this.RegisterPublicComponent(componentA); }