internal int RegisterEntityFilter(Type requires, Type excludes, Type requiresOne, Action <Entity> OnAdded, Action <Entity> OnRemoved) { ComponentsFilter newOne = new ComponentsFilter() { RequiresAll = requires.GetGenericArguments(), RequiresOne = excludes?.GetGenericArguments() ?? ComponentsFilter.EmptyTypes, Excludes = requiresOne?.GetGenericArguments() ?? ComponentsFilter.EmptyTypes, }; int filterID; if (m_RegisteredFiltersList.IndexOf(newOne) > -1) { filterID = m_RegisteredFiltersList.IndexOf(newOne); m_RegisteredFiltersCallbacks[filterID].OnAdded += OnAdded; m_RegisteredFiltersCallbacks[filterID].OnRemoved += OnRemoved; return(filterID); } m_RegisteredFiltersList.Add(newOne); m_FilteredEntities.Add(new List <Entity>()); m_RegisteredFiltersCallbacks.Add(new Callbacks()); filterID = m_RegisteredFiltersList.Count - 1; m_RegisteredFiltersCallbacks[filterID].OnAdded += OnAdded; m_RegisteredFiltersCallbacks[filterID].OnRemoved += OnRemoved; FilterCreatedEntities(filterID); return(filterID); }
internal bool DoesEntityPassFilter(Entity entity, int FilterId) { bool result = true; ComponentsFilter Filter = m_RegisteredFiltersList[FilterId]; // Entity must have all of these components foreach (Type componentType in Filter.RequiresAll) { if (!entity.HasComponent(componentType)) { result = false; break; } } // Entity must have at least one of these components if (Filter.RequiresOne.Length > 0) { bool hasOne = false; foreach (Type componentType in Filter.RequiresOne) { if (entity.HasComponent(componentType)) { hasOne = true; break; } } result &= hasOne; } // Entity must have none of these components if (Filter.Excludes.Length > 0) { bool hasOne = false; foreach (Type componentType in Filter.Excludes) { if (entity.HasComponent(componentType)) { hasOne = true; break; } } result &= !hasOne; } return(result); }