k/ECS/Core/Filter.cs
using System;
using System.Collections.Generic;

namespace Sandbox.k.ECS.Core;

public class Filter
{
	private World _world;
	private List<Type> _with = new(); 
	private List<Type> _without = new(); 
	
	public Filter( World world )
	{
		_world = world;
	}

	public Filter With<T>()
	{
		_with.Add(typeof(T));
		return this;
	}

	public Filter Without<T>()
	{
		_without.Add(typeof(T));
		return this;
	}

	public IEnumerable<int> GetEntities()
	{
		var entities = _world.EntityManager.Entities;
		var result = new List<int>();
		foreach ( var entity in entities )
		{
			if ( _world.EntityManager.IsAlive(entity) )
			{
				bool hasAll = true;
				foreach ( var type in _with )
				{
					if ( !_world.HasComponent(type, entity) )
					{
						hasAll = false;
						break;
					}
				}

				if ( !hasAll ) continue;

				foreach ( var type in _without )
				{
					if ( _world.HasComponent(type, entity) )
					{
						hasAll = false;
						break;
					}
				}

				if ( hasAll )
					result.Add(entity);
			}
		}

		return result;
	}

	public void Clear()
	{
		_with.Clear();
		_without.Clear();
	}
}