|
|
|
@ -1,4 +1,6 @@ |
|
|
|
|
using System; |
|
|
|
|
using System.Collections; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using gaemstone.Utility; |
|
|
|
|
using static flecs_hub.flecs; |
|
|
|
|
|
|
|
|
@ -9,6 +11,11 @@ public unsafe class Rule |
|
|
|
|
{ |
|
|
|
|
public World World { get; } |
|
|
|
|
public ecs_rule_t* Handle { get; } |
|
|
|
|
public Filter Filter { get; } |
|
|
|
|
|
|
|
|
|
private VariableCollection? _variables; |
|
|
|
|
public VariableCollection Variables => _variables ??= new(this); |
|
|
|
|
public Iterator.Variable? ThisVar => Filter.ThisVar; |
|
|
|
|
|
|
|
|
|
public Rule(World world, FilterDesc desc) |
|
|
|
|
{ |
|
|
|
@ -16,6 +23,7 @@ public unsafe class Rule |
|
|
|
|
var flecsDesc = desc.ToFlecs(alloc); |
|
|
|
|
World = world; |
|
|
|
|
Handle = ecs_rule_init(world, &flecsDesc); |
|
|
|
|
Filter = new(World, ecs_rule_get_filter(Handle)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
@ -28,4 +36,41 @@ public unsafe class Rule |
|
|
|
|
=> ecs_rule_str(Handle).FlecsToStringAndFree()!; |
|
|
|
|
|
|
|
|
|
public static implicit operator ecs_rule_t*(Rule q) => q.Handle; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public unsafe class VariableCollection |
|
|
|
|
: IReadOnlyCollection<Iterator.Variable> |
|
|
|
|
{ |
|
|
|
|
private readonly List<Iterator.Variable> _variables = new(); |
|
|
|
|
|
|
|
|
|
public Rule Rule { get; } |
|
|
|
|
|
|
|
|
|
public int Count => _variables.Count; |
|
|
|
|
public Iterator.Variable? this[int index] |
|
|
|
|
=> _variables.Find(v => v.Index == index); |
|
|
|
|
public Iterator.Variable? this[string name] |
|
|
|
|
=> _variables.Find(v => string.Equals( |
|
|
|
|
v.Name, name, StringComparison.OrdinalIgnoreCase)); |
|
|
|
|
|
|
|
|
|
internal VariableCollection(Rule rule) |
|
|
|
|
{ |
|
|
|
|
Rule = rule; |
|
|
|
|
|
|
|
|
|
// Find the $This variable, if the rule has one. |
|
|
|
|
var thisIndex = ecs_filter_find_this_var(ecs_rule_get_filter(Rule)); |
|
|
|
|
if (thisIndex >= 0) _variables.Add(new(thisIndex, "This")); |
|
|
|
|
|
|
|
|
|
// Find all the other "accessible" variables. |
|
|
|
|
var count = ecs_rule_var_count(Rule); |
|
|
|
|
for (var i = 0; i < count; i++) { |
|
|
|
|
if ((i == thisIndex) || !ecs_rule_var_is_entity(Rule, i)) continue; |
|
|
|
|
var name = ecs_rule_var_name(Rule, i).FlecsToString()!; |
|
|
|
|
_variables.Add(new(i, name)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IEnumerable implementation |
|
|
|
|
public IEnumerator<Iterator.Variable> GetEnumerator() => _variables.GetEnumerator(); |
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => _variables.GetEnumerator(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|