public EntityRepositoryObserver(EntityRepository repo, IEntityMatcher matcher, EntityCollectionEventType eventType) { _collectedEntities = new HashSet <Entity>(EntityEqualityComparer.comparer); _collection = repo.GetCollection(matcher); _eventType = eventType; Activate(); }
public EntityCollection GetCollection(IEntityMatcher matcher) { EntityCollection collection; if (!_collections.TryGetValue(matcher, out collection)) { collection = new EntityCollection(matcher); foreach (var entity in _entities) { collection.AddEntityIfMatching(entity); } _collections.Add(matcher, collection); for (int i = 0, indicesLength = matcher.indices.Length; i < indicesLength; i++) { var index = matcher.indices[i]; if (_collectionsForIndex[index] == null) { _collectionsForIndex[index] = new List <EntityCollection>(); } _collectionsForIndex[index].Add(collection); } } return(collection); }
public bool detachChildren(IEntityMatcher pEntityMatcher) { if (this.mChildren == null) { return(false); } return(this.mChildren.RemoveAll(pEntityMatcher, Entity.PARAMETERCALLABLE_DETACHCHILD)); }
public void Before() { _m = Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB, CP.ComponentC }); }
public IEntity detachChild(IEntityMatcher pEntityMatcher) { if (this.mChildren == null) { return(null); } return(this.mChildren.Remove(pEntityMatcher)); }
public IEntity findChild(IEntityMatcher pEntityMatcher) { if (this.mChildren == null) { return(null); } return(this.mChildren.Find(pEntityMatcher)); }
void when_created() { it["has no entities when no entities were created"] = () => { _repo.GetEntities().should_be_empty(); }; it["creates entity"] = () => { var e = _repo.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["doesn't have entites that were not created with CreateEntity()"] = () => { _repo.HasEntity(this.CreateEntity()).should_be_false(); }; it["has entites that were created with CreateEntity()"] = () => { _repo.HasEntity(_repo.CreateEntity()).should_be_true(); }; it["returns all created entities"] = () => { var e1 = _repo.CreateEntity(); var e2 = _repo.CreateEntity(); var entities = _repo.GetEntities(); entities.should_contain(e1); entities.should_contain(e2); entities.Length.should_be(2); }; it["destroys entity and removes it"] = () => { var e = _repo.CreateEntity(); _repo.DestroyEntity(e); _repo.HasEntity(e).should_be_false(); }; it["destroys an entity and removes all its components"] = () => { var e = _repo.CreateEntity(); e.AddComponentA(); _repo.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entites"] = () => { var e = _repo.CreateEntity(); e.AddComponentA(); _repo.CreateEntity(); _repo.DestroyAllEntities(); _repo.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["caches entities"] = () => { _repo.CreateEntity(); var entities1 = _repo.GetEntities(); var entities2 = _repo.GetEntities(); entities1.should_be_same(entities2); _repo.DestroyEntity(_repo.CreateEntity()); _repo.GetEntities().should_not_be_same(entities1); }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = _repo.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = this.CreateEntity(); e.AddComponentA(); _repo.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = this.CreateEntity(); e.AddComponentA(); _repo.DestroyEntity(e); var entity = _repo.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["returns new entity"] = () => { var e = this.CreateEntity(); e.AddComponentA(); _repo.DestroyEntity(e); _repo.CreateEntity(); var entityFromPool = _repo.CreateEntity(); entityFromPool.HasComponent(CID.ComponentA).should_be_false(); entityFromPool.should_not_be_same(e); }; it["sets up entity from pool"] = () => { _repo.DestroyEntity(_repo.CreateEntity()); var c = _repo.GetCollection(Matcher.AllOf(new [] { CID.ComponentA })); var e = _repo.CreateEntity(); e.AddComponentA(); c.GetEntities().should_contain(e); }; }; context["get entities"] = () => { it["gets empty collection for matcher when no entities were created"] = () => { var c = _repo.GetCollection(Matcher.AllOf(new [] { CID.ComponentA })); c.should_not_be_null(); c.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IEntityMatcher matcher = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = _repo.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = _repo.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = _repo.CreateEntity(); eA.AddComponentA(); }; it["gets collection with matching entities"] = () => { var c = _repo.GetCollection(matcher).GetEntities(); c.Length.should_be(2); c.should_contain(eAB1); c.should_contain(eAB2); }; it["gets cached collection"] = () => { _repo.GetCollection(matcher).should_be_same(_repo.GetCollection(matcher)); }; it["cached collection contains newly created matching entity"] = () => { var c = _repo.GetCollection(matcher); eA.AddComponentB(); c.GetEntities().should_contain(eA); }; it["cached collection doesn't contain entity which are not matching anymore"] = () => { var c = _repo.GetCollection(matcher); eAB1.RemoveComponentA(); c.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var c = _repo.GetCollection(matcher); _repo.DestroyEntity(eAB1); c.GetEntities().should_not_contain(eAB1); }; it["ignores adding components to destroyed entity"] = () => { var c = _repo.GetCollection(matcher); _repo.DestroyEntity(eA); eA.AddComponentA(); eA.AddComponentB(); c.GetEntities().should_not_contain(eA); }; it["collections dispatches OnEntityWillBeRemoved, OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var c = _repo.GetCollection(matcher); var didDispatchWillBeRemoved = 0; var didDispatchRemoved = 0; var didDispatchAdded = 0; EntityCollection eventCollectionWillBeRemoved = null; EntityCollection eventCollectionRemoved = null; EntityCollection eventCollectionAdded = null; Entity eventEntityWillBeRemoved = null; Entity eventEntityRemoved = null; Entity eventEntityAdded = null; c.OnEntityWillBeRemoved += (collection, entity) => { eventCollectionWillBeRemoved = collection; eventEntityWillBeRemoved = entity; didDispatchWillBeRemoved++; }; c.OnEntityRemoved += (collection, entity) => { eventCollectionRemoved = collection; eventEntityRemoved = entity; didDispatchRemoved++; }; c.OnEntityAdded += (collection, entity) => { eventCollectionAdded = collection; eventEntityAdded = entity; didDispatchAdded++; }; eAB1.WillRemoveComponent(CID.ComponentA); eAB1.ReplaceComponentA(new ComponentA()); eventCollectionWillBeRemoved.should_be_same(c); eventCollectionRemoved.should_be_same(c); eventCollectionAdded.should_be_same(c); eventEntityWillBeRemoved.should_be_same(eAB1); eventEntityRemoved.should_be_same(eAB1); eventEntityAdded.should_be_same(eAB1); didDispatchWillBeRemoved.should_be(1); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; }; }; }
public ReactiveSubSystemSpy(IEntityMatcher matcher, EntityCollectionEventType eventType) { _matcher = matcher; _eventType = eventType; }
public EntityCollection(IEntityMatcher matcher) { _matcher = matcher; }
public SingleEntityException(IEntityMatcher matcher) : base("Multiple entites exist matching " + matcher) { }
public MatcherException(IEntityMatcher matcher) : base("Matcher must have exactely one index, had " + matcher.indices.Length + ".\n" + matcher) { }
void when_creating_matcher() { Entity eA = null; Entity eC = null; Entity eAB = null; Entity eABC = null; before = () => { eA = this.CreateEntity(); eC = this.CreateEntity(); eAB = this.CreateEntity(); eABC = this.CreateEntity(); eA.AddComponentA(); eC.AddComponentC(); eAB.AddComponentA(); eAB.AddComponentB(); eABC.AddComponentA(); eABC.AddComponentB(); eABC.AddComponentC(); }; context["allOf"] = () => { IEntityMatcher m = null; before = () => m = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentA, CID.ComponentB }); it["doesn't match"] = () => { m.Matches(eA).should_be_false(); }; it["matches"] = () => { m.Matches(eAB).should_be_true(); m.Matches(eABC).should_be_true(); }; it["gets triggering types without duplicates"] = () => { m.indices.Length.should_be(2); m.indices.should_contain(CID.ComponentA); m.indices.should_contain(CID.ComponentB); }; }; context["equals"] = () => { it["equals equal AllOfEntityMatcher"] = () => { var m1 = allOfAB(); var m2 = allOfAB(); m1.should_not_be_same(m2); m1.Equals(m2).should_be_true(); }; it["equals equal AllOfEntityMatcher independent from the order of indices"] = () => { var m1 = allOfAB(); var m2 = Matcher.AllOf(new [] { CID.ComponentB, CID.ComponentA }); m1.should_not_be_same(m2); m1.Equals(m2).should_be_true(); }; it["doesn't equal different AllOfEntityMatcher"] = () => { var m1 = Matcher.AllOf(new [] { CID.ComponentA }); var m2 = allOfAB(); m1.Equals(m2).should_be_false(); }; it["generates same hash for equal AllOfEntityMatcher"] = () => { var m1 = allOfAB(); var m2 = allOfAB(); m1.GetHashCode().should_be(m2.GetHashCode()); }; it["generates same hash independent from the order of indices"] = () => { var m1 = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); var m2 = Matcher.AllOf(new [] { CID.ComponentB, CID.ComponentA }); m1.GetHashCode().should_be(m2.GetHashCode()); }; }; }
public static EntityRepositoryObserver CreateObserver(this EntityRepository repo, IEntityMatcher matcher, EntityCollectionEventType eventType = EntityCollectionEventType.OnEntityAdded) { return(new EntityRepositoryObserver(repo, matcher, eventType)); }
public static Entity[] GetEntities(this EntityRepository repo, IEntityMatcher matcher) { return(repo.GetCollection(matcher).GetEntities()); }