public virtual void Initialize(IEventSystem eventSystem, IPoolManager poolManager) { Components = new Type[] { typeof(T1), typeof(T2) }; EventSystem = eventSystem; EntityPool = poolManager.GetPool(); cachedEntities.ObserveAdd().Select(x => x.Value).Subscribe(entity => { if (Predicates.Count == 0) { PreAdd(entity); AddEntity(entity); return; } var bools = new List <ReactiveProperty <bool> >(); foreach (var predicate in Predicates) { bools.Add(predicate.Invoke(entity)); } var onLatest = Observable.CombineLatest(bools.ToArray()); onLatest.DistinctUntilChanged().Subscribe(values => { if (values.All(value => value == true)) { PreAdd(entity); AddEntity(entity); } else { PreRemove(entity); RemoveEntity(entity); } }).AddTo(this.Disposer);; }).AddTo(this.Disposer); cachedEntities.ObserveRemove().Select(x => x.Value).Subscribe(entity => { PreRemove(entity); RemoveEntity(entity); }).AddTo(this.Disposer); foreach (IEntity entity in EntityPool.Entities) { if (entity.Has(Components.ToArray())) { cachedEntities.Add(entity); } } EventSystem.OnEvent <EntityAddedEvent>().Where(_ => _.Entity.Has(Components.ToArray()) && cachedEntities.Contains(_.Entity) == false).Subscribe(_ => { cachedEntities.Add(_.Entity); }); EventSystem.OnEvent <EntityRemovedEvent>().Where(_ => cachedEntities.Contains(_.Entity)).Subscribe(_ => { cachedEntities.Remove(_.Entity); }); EventSystem.OnEvent <ComponentAddedEvent>().Where(_ => _.Entity.Has(Components.ToArray()) && cachedEntities.Contains(_.Entity) == false).Subscribe(_ => { cachedEntities.Add(_.Entity); }); EventSystem.OnEvent <ComponentRemovedEvent>().Where(_ => Components.Contains(_.Component.GetType()) && cachedEntities.Contains(_.Entity)).Subscribe(_ => { cachedEntities.Remove(_.Entity); }); }
public virtual void Initialize(IEventSystem eventSystem, IPoolManager poolManager) { EventSystem = eventSystem; EntityPool = poolManager.GetPool(); cachedEntities.ObserveAdd().Select(e => e.Value).Subscribe(entity => { if (Predicates.Count == 0) { PreAdd(entity); AddEntity(entity); return; } var bools = new List <ReactiveProperty <bool> >(); for (int i = 0; i < Predicates.Count; i++) { bools.Add(Predicates[i].Invoke(entity)); } var onLatest = Observable.CombineLatest(bools.ToArray()); var predicateDisposable = onLatest.DistinctUntilChanged().Subscribe(values => { for (int i = 0; i < values.Count; i++) { if (!values[i]) { if (Entities.Contains(entity)) { PreRemove(entity); RemoveEntity(entity); } return; } } PreAdd(entity); AddEntity(entity); }).AddTo(this.Disposer); predicatesTable.Add(entity, predicateDisposable); }).AddTo(this.Disposer); cachedEntities.ObserveRemove().Select(e => e.Value).Subscribe(entity => { if (predicatesTable.ContainsKey(entity)) { predicatesTable[entity].Dispose(); predicatesTable.Remove(entity); } PreRemove(entity); RemoveEntity(entity); }).AddTo(this.Disposer); foreach (IEntity entity in EntityPool.Entities) { if (entity.HasComponents(Components)) { cachedEntities.Add(entity); } } EventSystem.OnEvent <EntityAddedEvent>().Subscribe(evt => { if (!cachedEntities.Contains(evt.Entity) && evt.Entity.HasComponents(Components)) { cachedEntities.Add(evt.Entity); } }).AddTo(this); EventSystem.OnEvent <EntityRemovedEvent>().Subscribe(evt => { if (cachedEntities.Contains(evt.Entity)) { cachedEntities.Remove(evt.Entity); } }).AddTo(this); EventSystem.OnEvent <ComponentsAddedEvent>().Subscribe(evt => { if (!cachedEntities.Contains(evt.Entity) && evt.Entity.HasComponents(Components)) { cachedEntities.Add(evt.Entity); } }).AddTo(this); EventSystem.OnEvent <ComponentsRemovedEvent>().Subscribe(evt => { if (cachedEntities.Contains(evt.Entity)) { foreach (var component in evt.Components) { for (int i = 0; i < Components.Length; i++) { if (Components[i] == component.GetType()) { cachedEntities.Remove(evt.Entity); return; } } } } }).AddTo(this); }
public virtual void Initialize(IEventSystem eventSystem, IPoolManager poolManager) { EventSystem = eventSystem; EntityPool = poolManager.GetPool(); cachedEntities.ObserveAdd().Select(x => x.Value).Subscribe(entity => { cachedEntitySet.Add(entity); if (Predicates.Count == 0) { PreAdd(entity); AddEntity(entity); return; } var bools = new List <IReadOnlyReactiveProperty <bool> >(); foreach (var predicate in Predicates) { bools.Add(predicate.Invoke(entity)); } var onLatest = Observable.CombineLatest(bools.ToArray()); var predicateDisposable = onLatest.DistinctUntilChanged().Subscribe(values => { if (values.All(value => value == true)) { PreAdd(entity); AddEntity(entity); } else if (Entities.Contains(entity)) { PreRemove(entity); RemoveEntity(entity); } }).AddTo(this.Disposer); entityPredicateTable.Add(entity, predicateDisposable); }).AddTo(this.Disposer); cachedEntities.ObserveRemove().Select(x => x.Value).Subscribe(entity => { cachedEntitySet.Remove(entity); if (entityPredicateTable.ContainsKey(entity)) { entityPredicateTable[entity].Dispose(); entityPredicateTable.Remove(entity); } if (Entities.Contains(entity)) { PreRemove(entity); RemoveEntity(entity); } }).AddTo(this.Disposer); foreach (IEntity entity in EntityPool.Entities) { if (entity.HasComponents(Components)) { cachedEntities.Add(entity); } } EventSystem.OnEvent <EntityAddedEvent> ().Subscribe(evt => { if (!cachedEntitySet.Contains(evt.Entity) && evt.Entity.HasComponents(Components)) { cachedEntities.Add(evt.Entity); } }).AddTo(this.Disposer); EventSystem.OnEvent <EntityRemovedEvent> ().Subscribe(evt => { if (cachedEntitySet.Contains(evt.Entity)) { cachedEntities.Remove(evt.Entity); } }).AddTo(this.Disposer); EventSystem.OnEvent <ComponentAddedEvent> ().Subscribe(evt => { if (Components.Contains(evt.Component.GetType()) && !cachedEntitySet.Contains(evt.Entity) && evt.Entity.HasComponents(Components)) { cachedEntities.Add(evt.Entity); } }).AddTo(this.Disposer); EventSystem.OnEvent <ComponentsAddedEvent> ().Subscribe(evt => { if (!cachedEntitySet.Contains(evt.Entity) && evt.Entity.HasComponents(Components)) { cachedEntities.Add(evt.Entity); } }).AddTo(this.Disposer); EventSystem.OnEvent <ComponentRemovedEvent> ().Subscribe(evt => { if (Components.Contains(evt.Component.GetType()) && cachedEntitySet.Contains(evt.Entity)) { cachedEntities.Remove(evt.Entity); } }).AddTo(this.Disposer); }