private void Entity_ComponentChanged(object sender, NotifyCollectionChangedEventArgs e) { Entity entity = (Entity)sender; switch (e.Action) { case NotifyCollectionChangedAction.Add: break; case NotifyCollectionChangedAction.Move: break; case NotifyCollectionChangedAction.Remove: if (!ComponentTypes.All(entity.Has)) { Remove(entity); } break; case NotifyCollectionChangedAction.Replace: break; case NotifyCollectionChangedAction.Reset: Remove(entity); break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Tries the add. /// </summary> /// <param name="entity">The entity.</param> public void TryAdd(Entity entity) { if (Entities.Contains(entity)) { return; } if (!ComponentTypes.All(entity.Has)) { return; } entity.CollectionChanged += Entity_ComponentChanged; Entities.Add(entity); OnEntityAdded(entity); }
public void OnEntityComponentAdded(ComponentAddedEvent args) { var originalComponentTypes = args.Entity.Components.Select(x => x.GetType()).ToList(); originalComponentTypes.Remove(args.Component.GetType()); var previouslyMatched = ComponentTypes.All(x => originalComponentTypes.Contains(x)); if (previouslyMatched) { return; } var newComponentMatches = ComponentTypes.Contains(args.Component.GetType()); if (newComponentMatches) { OnEntityAdded.OnNext(args.Entity); } }
/// <summary> /// Determins whether an entity has the required component types specified by the processor. /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool Matches(Entity entity) { return(ComponentTypes.All(t => entity.Components.ContainsKey(t))); }