DragonECS/src/Executors/EcsWhereExecutor.cs

136 lines
4.6 KiB
C#
Raw Normal View History

2024-08-05 11:13:13 +08:00
using DCFApixels.DragonECS.Internal;
using System.Runtime.CompilerServices;
2024-08-24 12:29:58 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
2023-12-24 15:40:19 +08:00
2024-08-24 12:29:58 +08:00
namespace DCFApixels.DragonECS.Internal
{
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-08-24 13:10:50 +08:00
internal class EcsWhereExecutorCore : EcsQueryExecutorCore
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
private EcsMaskIterator _iterator;
private int[] _filteredEntities = new int[32];
private int _filteredEntitiesCount = 0;
private long _lastWorldVersion = 0;
2024-08-23 22:31:43 +08:00
private PoolVersionsChecker _versionsChecker;
2023-12-24 15:40:19 +08:00
#region Properties
2024-08-24 12:29:58 +08:00
public long Version
2023-12-24 15:40:19 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-24 12:29:58 +08:00
get { return _lastWorldVersion; }
2023-12-24 15:40:19 +08:00
}
#endregion
2024-08-24 13:10:50 +08:00
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
2023-12-24 15:40:19 +08:00
{
2024-08-24 13:10:50 +08:00
_versionsChecker = new PoolVersionsChecker(Mask);
_iterator = Mask.GetIterator();
2023-12-24 15:40:19 +08:00
}
2024-08-24 13:10:50 +08:00
protected sealed override void OnDestroy() { }
2023-12-24 15:40:19 +08:00
#endregion
#region Methods
2024-08-23 22:31:43 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Filter(EcsSpan span)
{
2024-08-24 12:29:58 +08:00
_filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities);
2024-08-24 13:10:50 +08:00
_lastWorldVersion = World.Version;
2024-08-24 12:29:58 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute()
{
2024-08-24 13:10:50 +08:00
return ExecuteFor(World.Entities);
2024-08-24 12:29:58 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
2024-08-24 13:10:50 +08:00
if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); }
2024-08-24 12:29:58 +08:00
#endif
2024-08-24 13:10:50 +08:00
World.ReleaseDelEntityBufferAllAuto();
if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false)
2024-08-24 12:29:58 +08:00
{
Filter(span);
}
2024-08-24 13:10:50 +08:00
return new EcsSpan(World.id, _filteredEntities, _filteredEntitiesCount);
2024-08-23 22:31:43 +08:00
}
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public EcsSpan Execute(Comparison<int> comparison)
// {
// return ExecuteFor(_aspect.World.Entities, comparison);
// }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public EcsSpan ExecuteFor(EcsSpan span, Comparison<int> comparison)
// {
//#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
// if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
// if (span.WorldID != WorldID) { Throw.Quiery_ArgumentDifferentWorldsException(); }
//#endif
// World.ReleaseDelEntityBufferAllAuto();
// if (_lastWorldVersion != World.Version)
// {
// Filter(span);
// }
// Array.Sort<int>(_filteredEntities, 0, _filteredEntitiesCount, comparison);
// }
2024-08-24 12:29:58 +08:00
#endregion
}
}
namespace DCFApixels.DragonECS
{
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
public sealed class EcsWhereExecutor<TAspect> : EcsQueryExecutor where TAspect : EcsAspect, new()
{
private TAspect _aspect;
private EcsWhereExecutorCore _core;
#region Properties
public TAspect Aspect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _aspect; }
}
public sealed override long Version
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _core.Version; }
}
#endregion
2024-08-23 22:31:43 +08:00
2024-08-24 12:29:58 +08:00
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
{
_aspect = World.GetAspect<TAspect>();
2024-08-24 13:10:50 +08:00
_core = Mediator.GetCore<EcsWhereExecutorCore>(_aspect.Mask);
2024-08-24 12:29:58 +08:00
}
protected sealed override void OnDestroy() { }
#endregion
#region Methods
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-25 23:05:00 +08:00
public EcsSpan Execute()
{
2024-08-24 12:29:58 +08:00
return _core.Execute();
2024-02-25 23:05:00 +08:00
}
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
return _core.ExecuteFor(span);
2023-12-24 15:40:19 +08:00
}
#endregion
}
}