2023-04-07 18:16:13 +08:00
|
|
|
|
using System;
|
2023-04-07 05:08:48 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-04-07 16:03:42 +08:00
|
|
|
|
using System.Reflection;
|
2023-04-07 05:08:48 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
using Unity.Profiling;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2023-04-07 05:08:48 +08:00
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2023-04-12 23:09:50 +08:00
|
|
|
|
public abstract class EcsQuery
|
2023-04-07 15:11:48 +08:00
|
|
|
|
{
|
2023-04-09 02:52:39 +08:00
|
|
|
|
internal EcsGroup groupFilter;
|
2023-04-07 15:11:48 +08:00
|
|
|
|
internal EcsQueryMask mask;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
public IEcsWorld World => groupFilter.World;
|
2023-04-07 16:03:42 +08:00
|
|
|
|
|
2023-04-09 02:52:39 +08:00
|
|
|
|
private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsQuery.GetEnumerator");
|
2023-04-07 05:08:48 +08:00
|
|
|
|
|
2023-04-09 02:52:39 +08:00
|
|
|
|
public EcsGroup.Enumerator GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
using (_getEnumerator.Auto())
|
|
|
|
|
{
|
2023-04-10 22:22:17 +08:00
|
|
|
|
// groupFilter.Clear();
|
|
|
|
|
var pools = World.GetAllPools();
|
|
|
|
|
//
|
|
|
|
|
// if (mask.Inc.Length > 0)
|
|
|
|
|
// {
|
|
|
|
|
// groupFilter.CopyFrom(pools[mask.Inc[0]].entities);
|
|
|
|
|
// for (int i = 1; i < mask.Inc.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// groupFilter.AndWith(pools[mask.Inc[i]].entities);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// groupFilter.CopyFrom(World.Entities);
|
|
|
|
|
// }
|
|
|
|
|
// for (int i = 0; i < mask.Exc.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// groupFilter.RemoveGroup(pools[mask.Exc[i]].entities);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// groupFilter.Sort();
|
|
|
|
|
// return groupFilter.GetEnumerator();
|
|
|
|
|
//
|
|
|
|
|
EcsReadonlyGroup sum = World.Entities;
|
|
|
|
|
for (int i = 0; i < mask.Inc.Length; i++)
|
2023-04-09 02:52:39 +08:00
|
|
|
|
{
|
2023-04-10 22:22:17 +08:00
|
|
|
|
sum = EcsGroup.And(sum.GetGroupInternal(), pools[mask.Inc[i]].entities);
|
|
|
|
|
// Debug.Log("inc " + sum.ToString());
|
2023-04-09 02:52:39 +08:00
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < mask.Exc.Length; i++)
|
|
|
|
|
{
|
2023-04-10 22:22:17 +08:00
|
|
|
|
sum = EcsGroup.Remove(sum.GetGroupInternal(), pools[mask.Exc[i]].entities);
|
|
|
|
|
// Debug.Log("exc " + sum.ToString());
|
2023-04-09 02:52:39 +08:00
|
|
|
|
}
|
2023-04-10 22:22:17 +08:00
|
|
|
|
//sum.GetGroupInternal().Sort();
|
|
|
|
|
return sum.GetEnumerator();
|
2023-04-09 02:52:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 16:03:42 +08:00
|
|
|
|
protected virtual void Init(Builder b) { }
|
|
|
|
|
|
2023-04-07 05:08:48 +08:00
|
|
|
|
#region Builder
|
2023-04-07 14:03:29 +08:00
|
|
|
|
public sealed class Builder : EcsQueryBuilder
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
private IEcsWorld _world;
|
|
|
|
|
private List<int> _inc;
|
|
|
|
|
private List<int> _exc;
|
|
|
|
|
|
2023-04-12 23:09:50 +08:00
|
|
|
|
internal static TQuery Build<TQuery>(IEcsWorld world) where TQuery : EcsQuery
|
2023-04-07 16:03:42 +08:00
|
|
|
|
{
|
|
|
|
|
Builder builder = new Builder(world);
|
|
|
|
|
|
|
|
|
|
Type queryType = typeof(TQuery);
|
|
|
|
|
ConstructorInfo constructorInfo = queryType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(Builder) }, null);
|
2023-04-12 23:09:50 +08:00
|
|
|
|
EcsQuery newQuery;
|
2023-04-07 16:03:42 +08:00
|
|
|
|
if (constructorInfo != null)
|
|
|
|
|
{
|
2023-04-12 23:09:50 +08:00
|
|
|
|
newQuery = (EcsQuery)constructorInfo.Invoke(new object[] { builder });
|
2023-04-07 16:03:42 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-12 23:09:50 +08:00
|
|
|
|
newQuery = (EcsQuery)Activator.CreateInstance(typeof(TQuery));
|
2023-04-07 16:03:42 +08:00
|
|
|
|
newQuery.Init(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.End(out newQuery.mask);
|
2023-04-09 02:52:39 +08:00
|
|
|
|
newQuery.groupFilter = new EcsGroup(world);
|
2023-04-07 16:03:42 +08:00
|
|
|
|
return (TQuery)(object)newQuery;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Builder(IEcsWorld world)
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
|
|
|
|
_inc = new List<int>(8);
|
|
|
|
|
_exc = new List<int>(4);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 18:16:13 +08:00
|
|
|
|
#region Init query member methods
|
2023-04-07 05:08:48 +08:00
|
|
|
|
public override inc<TComponent> Include<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
_inc.Add(_world.GetComponentID<TComponent>());
|
|
|
|
|
return new inc<TComponent>(_world.GetPool<TComponent>());
|
|
|
|
|
}
|
|
|
|
|
public override exc<TComponent> Exclude<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
_exc.Add(_world.GetComponentID<TComponent>());
|
|
|
|
|
return new exc<TComponent>(_world.GetPool<TComponent>());
|
|
|
|
|
}
|
|
|
|
|
public override opt<TComponent> Optional<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
return new opt<TComponent>(_world.GetPool<TComponent>());
|
|
|
|
|
}
|
2023-04-07 18:16:13 +08:00
|
|
|
|
#endregion
|
2023-04-07 05:08:48 +08:00
|
|
|
|
|
2023-04-07 16:03:42 +08:00
|
|
|
|
private void End(out EcsQueryMask mask)
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
_inc.Sort();
|
|
|
|
|
_exc.Sort();
|
2023-04-07 14:03:29 +08:00
|
|
|
|
mask = new EcsQueryMask(_world.ArchetypeType, _inc.ToArray(), _exc.ToArray());
|
2023-04-07 16:03:42 +08:00
|
|
|
|
|
2023-04-07 05:08:48 +08:00
|
|
|
|
_world = null;
|
|
|
|
|
_inc.Clear();
|
|
|
|
|
_inc = null;
|
|
|
|
|
_exc.Clear();
|
|
|
|
|
_exc = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 18:16:13 +08:00
|
|
|
|
public class EcsQueryMask : EcsComponentMask
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
2023-04-07 14:03:29 +08:00
|
|
|
|
public EcsQueryMask(Type worldArchetypeType, int[] inc, int[] exc)
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
WorldArchetypeType = worldArchetypeType;
|
|
|
|
|
Inc = inc;
|
|
|
|
|
Exc = exc;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 17:15:31 +08:00
|
|
|
|
public abstract class EcsQueryBuilder
|
|
|
|
|
{
|
|
|
|
|
public abstract inc<TComponent> Include<TComponent>() where TComponent : struct;
|
|
|
|
|
public abstract exc<TComponent> Exclude<TComponent>() where TComponent : struct;
|
|
|
|
|
public abstract opt<TComponent> Optional<TComponent>() where TComponent : struct;
|
|
|
|
|
}
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|