2023-04-21 03:16:05 +08:00
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using Unity.Profiling;
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-04-22 23:40:09 +08:00
|
|
|
public sealed class EcsTagPool<T> : EcsPoolBase<T>
|
2023-04-21 14:21:24 +08:00
|
|
|
where T : struct, IEcsTagComponent
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
|
|
|
private EcsWorld _source;
|
|
|
|
|
2023-04-21 14:21:24 +08:00
|
|
|
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
2023-04-21 03:16:05 +08:00
|
|
|
private int _count;
|
|
|
|
|
|
|
|
private PoolRunners _poolRunners;
|
|
|
|
|
|
|
|
#region Properites
|
|
|
|
public int Count => _count;
|
|
|
|
public sealed override EcsWorld World => _source;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Init
|
|
|
|
protected override void Init(EcsWorld world)
|
|
|
|
{
|
|
|
|
_source = world;
|
|
|
|
|
2023-04-21 14:21:24 +08:00
|
|
|
_mapping = new bool[world.Capacity];
|
2023-04-21 03:16:05 +08:00
|
|
|
_count = 0;
|
|
|
|
|
|
|
|
_poolRunners = new PoolRunners(world.Pipeline);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Add/Has/Del
|
|
|
|
private ProfilerMarker _addMark = new ProfilerMarker("EcsPoo.Add");
|
|
|
|
private ProfilerMarker _hasMark = new ProfilerMarker("EcsPoo.Has");
|
|
|
|
private ProfilerMarker _delMark = new ProfilerMarker("EcsPoo.Del");
|
|
|
|
public void Add(int entityID)
|
|
|
|
{
|
|
|
|
// using (_addMark.Auto())
|
|
|
|
// {
|
2023-04-21 14:21:24 +08:00
|
|
|
if (_mapping[entityID] == false)
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
2023-04-21 14:21:24 +08:00
|
|
|
_count++;
|
|
|
|
_mapping[entityID] = true;
|
2023-04-21 03:16:05 +08:00
|
|
|
_poolRunners.add.OnComponentAdd<T>(entityID);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public sealed override bool Has(int entityID)
|
|
|
|
{
|
|
|
|
// using (_hasMark.Auto())
|
2023-04-21 14:21:24 +08:00
|
|
|
return _mapping[entityID];
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
|
|
|
public void Del(int entityID)
|
|
|
|
{
|
|
|
|
// using (_delMark.Auto())
|
|
|
|
// {
|
2023-04-21 14:21:24 +08:00
|
|
|
_mapping[entityID] = false;
|
2023-04-21 03:16:05 +08:00
|
|
|
_count--;
|
|
|
|
_poolRunners.del.OnComponentDel<T>(entityID);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region WorldCallbacks
|
|
|
|
protected override void OnWorldResize(int newSize)
|
|
|
|
{
|
|
|
|
Array.Resize(ref _mapping, newSize);
|
|
|
|
}
|
|
|
|
protected override void OnDestroy() { }
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
2023-04-21 03:46:51 +08:00
|
|
|
public interface IEcsTagComponent { }
|
|
|
|
public static class EcsTagPoolExt
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
2023-04-21 16:03:50 +08:00
|
|
|
public static EcsTagPool<TTagComponent> GetPool<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
|
|
|
return self.GetPool<TTagComponent, EcsTagPool<TTagComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static EcsTagPool<TTagComponent> Include<TTagComponent>(this EcsQueryBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
|
|
|
{
|
|
|
|
return self.Include<TTagComponent, EcsTagPool<TTagComponent>>();
|
|
|
|
}
|
|
|
|
public static EcsTagPool<TTagComponent> Exclude<TTagComponent>(this EcsQueryBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
|
|
|
{
|
|
|
|
return self.Exclude<TTagComponent, EcsTagPool<TTagComponent>>();
|
|
|
|
}
|
|
|
|
public static EcsTagPool<TTagComponent> Optional<TTagComponent>(this EcsQueryBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
|
|
|
{
|
|
|
|
return self.Optional<TTagComponent, EcsTagPool<TTagComponent>>();
|
|
|
|
}
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
|
|
|
}
|