fix entity gen management

This commit is contained in:
DCFApixels 2024-11-15 15:08:44 +08:00
parent fe8b1d9ecd
commit f585140f70
2 changed files with 11 additions and 12 deletions

View File

@ -290,10 +290,9 @@ namespace DCFApixels.DragonECS
_entitiesCount++; _entitiesCount++;
ref var slot = ref _entities[entityID]; ref var slot = ref _entities[entityID];
slot.isUsed = true; slot.isUsed = true;
if (slot.gen >= 0) if (slot.gen >= GEN_STATUS_SEPARATOR)
{ //если gen был пробужен у не мертвой сущности, то для отличия от мертвой, нужно инкрементировать и усыпить {
slot.gen++; slot.gen |= GEN_SLEEP_MASK;
slot.gen &= SLEEP_GEN_MASK;
} }
_entityListeners.InvokeOnNewEntity(entityID); _entityListeners.InvokeOnNewEntity(entityID);
} }
@ -374,10 +373,10 @@ namespace DCFApixels.DragonECS
unchecked unchecked
{ {
ref var slotGen = ref _entities[entityID].gen; ref var slotGen = ref _entities[entityID].gen;
if (slotGen < 0) if (slotGen < GEN_STATUS_SEPARATOR)
{ //если gen меньше 0 значит он спящий, спящие нужно инкремировать { //если gen меньше 0 значит он спящий, спящие нужно инкремировать перед выдачей
slotGen++; slotGen++;
slotGen &= WAKE_UP_GEN_MASK; slotGen &= GEN_WAKEUP_MASK;
} }
return slotGen; return slotGen;
} }
@ -719,7 +718,7 @@ namespace DCFApixels.DragonECS
{ {
int e = buffer[i]; int e = buffer[i];
_entityDispenser.Release(e); _entityDispenser.Release(e);
_entities[e].gen |= SLEEPING_GEN_FLAG; _entities[e].gen |= GEN_SLEEP_MASK;
} }
Densify(); Densify();
} }
@ -1061,7 +1060,7 @@ namespace DCFApixels.DragonECS
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
private struct EntitySlot private struct EntitySlot
{ {
public static readonly EntitySlot Empty = new EntitySlot(SLEEPING_GEN_FLAG, 0, false); public static readonly EntitySlot Empty = new EntitySlot(GEN_SLEEP_MASK, 0, false);
public short gen; public short gen;
public short componentsCount; public short componentsCount;
public bool isUsed; public bool isUsed;

View File

@ -19,10 +19,10 @@ namespace DCFApixels.DragonECS
{ {
private const short NULL_WORLD_ID = 0; private const short NULL_WORLD_ID = 0;
private const short WAKE_UP_GEN_MASK = 0x7fff; private const short GEN_STATUS_SEPARATOR = 0;
private const short SLEEP_GEN_MASK = ~WAKE_UP_GEN_MASK; private const short GEN_WAKEUP_MASK = 0x7fff;
private const short GEN_SLEEP_MASK = ~GEN_WAKEUP_MASK;
private const short SLEEPING_GEN_FLAG = short.MinValue;
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5; private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5;
private const int DEL_ENT_BUFFER_MIN_SIZE = 64; private const int DEL_ENT_BUFFER_MIN_SIZE = 64;