|
|
|
using System;
|
|
|
|
using gaemstone.ECS.Utility;
|
|
|
|
using static flecs_hub.flecs;
|
|
|
|
|
|
|
|
namespace gaemstone.ECS;
|
|
|
|
|
|
|
|
public unsafe class Query<TContext>
|
|
|
|
: IDisposable
|
|
|
|
{
|
|
|
|
public World<TContext> World { get; }
|
|
|
|
public ecs_query_t* Handle { get; }
|
|
|
|
public Filter<TContext> Filter { get; }
|
|
|
|
|
|
|
|
public Variable? ThisVar => Filter.ThisVar;
|
|
|
|
|
|
|
|
public Query(World<TContext> world, QueryDesc desc)
|
|
|
|
{
|
|
|
|
using var alloc = TempAllocator.Use();
|
|
|
|
var flecsDesc = desc.ToFlecs(alloc);
|
|
|
|
World = world;
|
|
|
|
Handle = ecs_query_init(world, &flecsDesc);
|
|
|
|
Filter = new(world, ecs_query_get_filter(Handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
=> ecs_query_fini(this);
|
|
|
|
|
|
|
|
public QueryIterator<TContext> Iter()
|
|
|
|
=> new(ecs_query_iter(World, this));
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
=> ecs_query_str(Handle).FlecsToStringAndFree()!;
|
|
|
|
|
|
|
|
public static implicit operator ecs_query_t*(Query<TContext> query) => query.Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public unsafe class QueryIterator<TContext>
|
|
|
|
: Iterator<TContext>
|
|
|
|
{
|
|
|
|
internal QueryIterator(ecs_iter_t value)
|
|
|
|
: base(value) { }
|
|
|
|
public override bool Next()
|
|
|
|
=> ecs_query_next(Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class QueryDesc : FilterDesc
|
|
|
|
{
|
|
|
|
public QueryDesc(params Term[] terms) : base(terms) { }
|
|
|
|
public QueryDesc(string expression) : base(expression) { }
|
|
|
|
|
|
|
|
public new unsafe ecs_query_desc_t ToFlecs(IAllocator alloc)
|
|
|
|
{
|
|
|
|
var desc = new ecs_query_desc_t {
|
|
|
|
filter = base.ToFlecs(alloc),
|
|
|
|
// TODO: Implement more Query features.
|
|
|
|
};
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
}
|