DragonECS/src/Executors/EcsWhereExecutor.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2023-12-24 15:40:19 +08:00
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
2023-06-22 14:31:13 +08:00
public sealed class EcsWhereExecutor<TAspect> : EcsQueryExecutor where TAspect : EcsAspect
2023-12-24 15:40:19 +08:00
{
private TAspect _aspect;
private int[] _filteredEntities;
2024-02-26 07:20:19 +08:00
private int _filteredEntitiesCount;
2023-12-24 15:40:19 +08:00
private long _lastWorldVersion;
2023-12-24 15:40:19 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
private readonly EcsProfilerMarker _executeMarker = new EcsProfilerMarker("Where");
#endif
#region Properties
public TAspect Aspect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _aspect;
}
public sealed override long Version
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _lastWorldVersion;
2023-12-24 15:40:19 +08:00
}
#endregion
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
{
_aspect = World.GetAspect<TAspect>();
_filteredEntities = new int[32];
}
protected sealed override void OnDestroy() { }
#endregion
#region Methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-25 23:05:00 +08:00
public EcsSpan Execute()
{
return ExecuteFor(_aspect.World.Entities);
}
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
2023-12-24 15:40:19 +08:00
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_executeMarker.Begin();
if (span.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
if (span.WorldID != WorldID) throw new System.ArgumentException();//TODO составить текст исключения.
2023-12-24 15:40:19 +08:00
#endif
EcsSpan result;
if (_lastWorldVersion != World.Version)
{
2024-02-11 01:29:18 +08:00
result = _aspect.GetIteratorFor(span).CopyToSpan(ref _filteredEntities);
2024-02-26 07:20:19 +08:00
_filteredEntitiesCount = result.Length;
_lastWorldVersion = World.Version;
}
else
{
2024-02-26 07:20:19 +08:00
result = new EcsSpan(WorldID, _filteredEntities, _filteredEntitiesCount);
}
2023-12-24 15:40:19 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_executeMarker.End();
#endif
return result;
}
#endregion
}
}