DragonECS/src/Executors/EcsWhereExecutor.cs

119 lines
4.0 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-06-22 14:31:13 +08:00
private TAspect _aspect;
2023-05-27 15:59:46 +08:00
private EcsGroup _filteredGroup;
2023-12-24 15:40:19 +08:00
private long _version;
2023-06-02 03:33:33 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-12-24 15:40:19 +08:00
private readonly EcsProfilerMarker _executeMarker = new EcsProfilerMarker("Where");
2023-06-02 03:33:33 +08:00
#endif
#region Properties
2023-12-24 15:40:19 +08:00
public TAspect Aspect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _aspect;
}
public sealed override long Version
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _version;
}
#endregion
2023-05-27 15:59:46 +08:00
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
{
2023-06-22 14:31:13 +08:00
_aspect = World.GetAspect<TAspect>();
2023-05-27 15:59:46 +08:00
_filteredGroup = EcsGroup.New(World);
}
protected sealed override void OnDestroy()
{
2023-06-10 00:55:00 +08:00
_filteredGroup.Dispose();
}
#endregion
#region Methods
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-22 14:31:13 +08:00
public EcsReadonlyGroup Execute() => ExecuteFor(_aspect.World.Entities);
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-02 03:33:33 +08:00
public EcsReadonlyGroup ExecuteFor(EcsReadonlyGroup sourceGroup)
{
2023-06-02 03:33:33 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-12-24 15:40:19 +08:00
_executeMarker.Begin();
2023-06-12 21:59:27 +08:00
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
2023-12-24 15:40:19 +08:00
if (sourceGroup.WorldID != WorldID) throw new System.ArgumentException();//TODO составить текст исключения.
2023-06-02 03:33:33 +08:00
#endif
2023-12-24 15:40:19 +08:00
unchecked { _version++; }
2023-06-22 14:31:13 +08:00
_aspect.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup);
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-12-24 15:40:19 +08:00
_executeMarker.End();
#endif
2023-06-12 21:59:27 +08:00
return _filteredGroup.Readonly;
}
2023-06-22 14:39:12 +08:00
#endregion
}
2023-12-24 15:40:19 +08:00
public sealed class EcsWhereSpanExecutor<TAspect> : EcsQueryExecutor where TAspect : EcsAspect
{
private TAspect _aspect;
private int[] _filteredEntities;
private long _version;
#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 => _version;
}
#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)]
public EcsSpan Execute() => ExecuteFor(_aspect.World.Entities);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsReadonlyGroup sourceGroup)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_executeMarker.Begin();
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
if (sourceGroup.WorldID != WorldID) throw new System.ArgumentException();//TODO составить текст исключения.
#endif
unchecked { _version++; }
EcsSpan result = _aspect.GetIteratorFor(sourceGroup).CopyToSpan(ref _filteredEntities);
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_executeMarker.End();
#endif
return result;
}
#endregion
}
}