fix create/delete entity methods

This commit is contained in:
Mikhail 2023-04-23 17:19:52 +08:00
parent 514cf706e5
commit 54b88a3a98
3 changed files with 24 additions and 16 deletions

View File

@ -1,18 +1,18 @@
namespace DCFApixels.DragonECS
{
//TODO использовать этот мир для холостых вызовов. чтоб все нулевые ентити стучались в него, так можно попробовать сократить число проверок
//internal sealed class EcsDeathWrold : EcsWorld<EcsDefaultWrold>
//internal sealed class EcsDeathWrold : EcsWorld<EcsDefaultWorld>
//{
// public EcsDeathWrold(EcsPipeline pipeline) : base(pipeline) { }
//}
public sealed class EcsDefaultWrold : EcsWorld<EcsDefaultWrold>
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld>
{
public EcsDefaultWrold(EcsPipeline pipeline = null) : base(pipeline) { }
public EcsDefaultWorld(EcsPipeline pipeline = null) : base(pipeline) { }
}
public sealed class EcsEventWrold : EcsWorld<EcsEventWrold>
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld>
{
public EcsEventWrold(EcsPipeline pipeline = null) : base(pipeline) { }
public EcsEventWorld(EcsPipeline pipeline = null) : base(pipeline) { }
}
}

View File

@ -22,6 +22,9 @@ namespace DCFApixels.DragonECS
public abstract class EcsWorld : IEcsWorld
{
private const short GEN_BITS = 0x7fff;
private const short DEATH_GEN_BIT = short.MinValue;
public static EcsWorld[] Worlds = new EcsWorld[8];
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
public readonly short uniqueID;
@ -72,6 +75,8 @@ namespace DCFApixels.DragonECS
#region Constructors/Destroy
public EcsWorld(EcsPipeline pipline)
{
_entitesCapacity = 512;
uniqueID = (short)_worldIdDispenser.GetFree();
if (uniqueID >= Worlds.Length)
Array.Resize(ref Worlds, Worlds.Length << 1);
@ -86,10 +91,10 @@ namespace DCFApixels.DragonECS
_pools = new EcsPoolBase[512];
ArrayUtility.Fill(_pools, _nullPool);
_gens = new short[512];
_entitesCapacity = _gens.Length;
_gens = new short[_entitesCapacity];
ArrayUtility.Fill(_gens, DEATH_GEN_BIT);
_delEntBufferCount = 0;
_delEntBuffer = new int[_gens.Length >> DEL_ENT_BUFFER_SIZE_OFFSET];
_delEntBuffer = new int[_entitesCapacity >> DEL_ENT_BUFFER_SIZE_OFFSET];
_groups = new List<WeakReference<EcsGroup>>();
_allEntites = GetGroupFromPool();
@ -193,7 +198,9 @@ namespace DCFApixels.DragonECS
if (_gens.Length <= entityID)
{
Array.Resize(ref _gens, _gens.Length << 1);
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
_entitesCapacity = _gens.Length;
for (int i = 0; i < _groups.Count; i++)
{
if (_groups[i].TryGetTarget(out EcsGroup group))
@ -210,8 +217,9 @@ namespace DCFApixels.DragonECS
foreach (var item in _pools)
item.InvokeOnWorldResize(_gens.Length);
}
_gens[entityID] |= short.MinValue;
EcsEntity entity = new EcsEntity(entityID, _gens[entityID]++, uniqueID);
_gens[entityID] &= GEN_BITS;
EcsEntity entity = new EcsEntity(entityID, ++_gens[entityID], uniqueID);
UnityEngine.Debug.Log($"{entityID} {_gens[entityID]} {uniqueID}");
_entityCreate.OnEntityCreate(entity);
_allEntites.Add(entityID);
return entity;
@ -220,7 +228,7 @@ namespace DCFApixels.DragonECS
{
_allEntites.Remove(entity.id);
_delEntBuffer[_delEntBufferCount++] = entity.id;
_gens[entity.id] |= short.MinValue;
_gens[entity.id] |= DEATH_GEN_BIT;
_entitiesCount--;
_entityDestry.OnEntityDestroy(entity);

View File

@ -8,17 +8,17 @@ namespace DCFApixels.DragonECS
// gen - 16 bits
// world - 16 bits
/// <summary>Strong identifier/Permanent entity identifier</summary>
[StructLayout(LayoutKind.Explicit, Pack = 2, Size = 8)]
[StructLayout(LayoutKind.Explicit, Pack =2, Size = 8)]
public readonly partial struct EcsEntity : IEquatable<long>, IEquatable<EcsEntity>
{
public static readonly EcsEntity NULL = default;
[FieldOffset(0)]
internal readonly long full; //Union
[FieldOffset(3)]
public readonly int id;
[FieldOffset(1)]
public readonly short gen;
[FieldOffset(0)]
public readonly int id;
[FieldOffset(4)]
public readonly short gen;
[FieldOffset(6)]
public readonly short world;
//[MethodImpl(MethodImplOptions.AggressiveInlining)]