protected EventSubsystem(EntityManager entityManager, params Type[] types) { Contract.Requires <ArgumentNullException>(entityManager != null, "entityManager"); Contract.Requires <ArgumentNullException>(types != null, "types"); Collection = entityManager.Get(types); Collection.OnEntityAdd += EntityAddedToCollection; Collection.OnEntityRemove += EntityRemovedFromCollection; }
/// <summary> /// Return all entities containing all of the component types with a comparer /// </summary> /// <typeparam name="T"></typeparam> /// <param name="componentTypes"></param> /// <param name="comparer"></param> /// <returns></returns> public FilteredCollection Get(IComparer <Entity> comparer, params Type[] componentTypes) { var hashCode = FilteredCollection.GetHashCode(componentTypes, comparer); if (!_filteredCollections.ContainsKey(hashCode)) { _filteredCollections.Add(hashCode, new FilteredCollection(this, componentTypes, comparer)); } return(_filteredCollections[hashCode]); }
/// <summary> /// Constructor with a null comparer /// </summary> /// <param name="entityManager"></param> /// <param name="types"></param> /// <param name = "comparer"></param> internal FilteredCollection(EntityManager entityManager, Type[] types, IComparer <Entity> comparer = null) { _entities = comparer == null ? new SortedSet <Entity>() : new SortedSet <Entity>(comparer); _hashCode = FilteredCollection.GetHashCode(types, comparer); // Check that all types are really components if (!types.All(t => typeof(Component).IsAssignableFrom(t))) { throw new Exception("Type is not of IComponent - cannot filter."); } _manager = entityManager; _filter = types; // Add any existing entities _manager.Each(e => Add(e)); }