fix worldID

This commit is contained in:
Mikhail 2024-04-16 12:46:09 +08:00
parent 6613375779
commit ee500fadfd
7 changed files with 54 additions and 31 deletions

View File

@ -150,7 +150,7 @@ namespace DCFApixels.DragonECS
internal bool _isReleased = true;
#region Properties
public int WorldID
public short WorldID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _source.id; }

View File

@ -9,8 +9,8 @@ namespace DCFApixels.DragonECS
[DebuggerTypeProxy(typeof(DebuggerProxy))]
public readonly ref struct EcsSpan
{
private readonly int _worldID;
private readonly ReadOnlySpan<int> _values;
private readonly short _worldID;
#region Properties
public bool IsNull
@ -46,24 +46,24 @@ namespace DCFApixels.DragonECS
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, ReadOnlySpan<int> span)
internal EcsSpan(short worldID, ReadOnlySpan<int> span)
{
_worldID = worldID;
_values = span;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, int[] array)
internal EcsSpan(short worldID, int[] array)
{
_worldID = worldID;
_values = new ReadOnlySpan<int>(array);
}
internal EcsSpan(int worldID, int[] array, int length)
internal EcsSpan(short worldID, int[] array, int length)
{
_worldID = worldID;
_values = new ReadOnlySpan<int>(array, 0, length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, int[] array, int start, int length)
internal EcsSpan(short worldID, int[] array, int start, int length)
{
_worldID = worldID;
_values = new ReadOnlySpan<int>(array, start, length);
@ -110,7 +110,7 @@ namespace DCFApixels.DragonECS
internal class DebuggerProxy
{
private int[] _values;
private int _worldID;
private short _worldID;
public EcsWorld World { get { return EcsWorld.GetWorld(_worldID); } }
public EntitySlotInfo[] Entities
{

View File

@ -260,7 +260,7 @@ namespace DCFApixels.DragonECS
#region Iterator
public ref struct Iterator
{
public readonly int worldID;
public readonly short worldID;
public readonly EcsAspect aspect;
private EcsSpan _span;

View File

@ -11,7 +11,7 @@ namespace DCFApixels.DragonECS
public sealed class EcsMask : IEquatable<EcsMask>
{
internal readonly int id;
internal readonly int worldID;
internal readonly short worldID;
internal readonly EcsMaskChunck[] incChunckMasks;
internal readonly EcsMaskChunck[] excChunckMasks;
internal readonly int[] inc; //Sorted
@ -22,7 +22,7 @@ namespace DCFApixels.DragonECS
{
get { return id; }
}
public int WorldID
public short WorldID
{
get { return worldID; }
}
@ -51,7 +51,7 @@ namespace DCFApixels.DragonECS
{
return new Builder(world);
}
internal EcsMask(int id, int worldID, int[] inc, int[] exc)
internal EcsMask(int id, short worldID, int[] inc, int[] exc)
{
#if DEBUG
CheckConstraints(inc, exc);
@ -223,7 +223,7 @@ namespace DCFApixels.DragonECS
return false;
}
#endif
private static string CreateLogString(int worldID, int[] inc, int[] exc)
private static string CreateLogString(short worldID, int[] inc, int[] exc)
{
#if (DEBUG && !DISABLE_DEBUG)
string converter(int o) { return EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); }
@ -237,7 +237,7 @@ namespace DCFApixels.DragonECS
{
public readonly int ID;
public readonly EcsWorld world;
private readonly int _worldID;
private readonly short _worldID;
public readonly EcsMaskChunck[] includedChunkMasks;
public readonly EcsMaskChunck[] excludedChunkMasks;
public readonly int[] included;

View File

@ -239,31 +239,39 @@ namespace DCFApixels.DragonECS
return entityID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void NewEntity(int entityID)
public int NewEntity(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
if (IsUsed(entityID)) { Throw.World_EntityIsAlreadyСontained(entityID); }
#endif
_entityDispenser.Use(entityID);
CreateConcreteEntity(entityID);
return entityID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CreateConcreteEntity(int entityID)
{
UpVersionLeaked();
_entitiesCount++;
if (_entitiesCapacity <= entityID)
{
OnEntityDispenserResized(_entities.Length << 1);
ref var slot = ref _entities[entityID];
slot.isUsed = true;
if(slot.gen >= 0)
{ //если gen был пробужен у не мертвой сущности, то для отличия от мертвой, нужно инкрементировать и усыпить
slot.gen++;
slot.gen &= SLEEP_GEN_MASK;
}
_entities[entityID].isUsed = true;
_entityListeners.InvokeOnNewEntity(entityID);
}
public entlong NewEntityLong()
{
int e = NewEntity();
return GetEntityLong(e);
int entityID = NewEntity();
return GetEntityLong(entityID);
}
public entlong NewEntityLong(int entityID)
{
NewEntity(entityID);
return GetEntityLong(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -275,6 +283,11 @@ namespace DCFApixels.DragonECS
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DelEntity(entlong entity)
{
DelEntity(entity.ID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DelEntity(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
@ -325,15 +338,14 @@ namespace DCFApixels.DragonECS
{
unchecked
{
ref short slotGen = ref _entities[entityID].gen;
ref var slotGen = ref _entities[entityID].gen;
if (slotGen < 0)
{ //up gen
{ //если gen меньше 0 значит он спящий, спящие нужно инкремировать
slotGen++;
slotGen &= GEN_MASK;
slotGen &= WAKE_UP_GEN_MASK;
}
return slotGen;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short GetComponentsCount(int entityID)

View File

@ -9,8 +9,8 @@ namespace DCFApixels.DragonECS
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 4)]
public struct EcsWorldCmp<T> where T : struct
{
private int _worldID;
public EcsWorldCmp(int worldID) => _worldID = worldID;
private short _worldID;
public EcsWorldCmp(short worldID) => _worldID = worldID;
public EcsWorld World => EcsWorld.GetWorld(_worldID);
public ref T Value => ref EcsWorld.GetData<T>(_worldID);
}
@ -18,7 +18,9 @@ namespace DCFApixels.DragonECS
{
private const short NULL_WORLD_ID = 0;
private const short GEN_MASK = 0x7fff;
private const short WAKE_UP_GEN_MASK = 0x7fff;
private const short SLEEP_GEN_MASK = ~WAKE_UP_GEN_MASK;
private const short SLEEPING_GEN_FLAG = short.MinValue;
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5;
private const int DEL_ENT_BUFFER_MIN_SIZE = 64;
@ -45,12 +47,21 @@ namespace DCFApixels.DragonECS
Array.Resize(ref _worlds, newSize);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsWorld GetWorld(int worldID) => _worlds[worldID];
public static EcsWorld GetWorld(short worldID)
{
return _worlds[worldID];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetData<T>(int worldID) => ref WorldComponentPool<T>.GetForWorld(worldID);
public static ref T GetData<T>(int worldID)
{
return ref WorldComponentPool<T>.GetForWorld(worldID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetDataUnchecked<T>(int worldID) => ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
public static ref T GetDataUnchecked<T>(int worldID)
{
return ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
}
private abstract class DataReleaser
{

View File

@ -279,7 +279,7 @@ namespace DCFApixels.DragonECS.Internal
#endregion
#region UsedToEcsSpan
public EcsSpan UsedToEcsSpan(int worldID)
public EcsSpan UsedToEcsSpan(short worldID)
{
return new EcsSpan(worldID, _dense, 1, _usedCount - 1);
}