Continue improve archetype ecs

Updated Archetype to support add and remove entity
Added EntityManager
Added EntityCommandBuffer
This commit is contained in:
2025-12-03 20:40:19 +09:00
parent 63a70f1a74
commit 948fae4401
8 changed files with 614 additions and 124 deletions

View File

@@ -1,11 +1,16 @@
using System.Runtime.CompilerServices;
using Ghost.Core;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices;
namespace Ghost.ArcEntities;
public partial class World
{
private static List<World> s_worlds = new(4);
private static Queue<WorldID> s_freeWorldSlots = new();
private static List<World?> s_worlds = new(4);
private static Queue<Identifier<World>> s_freeWorldSlots = new();
internal static Identifier<Archetype> EmptyArchetypeID => new Identifier<Archetype>(0);
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
@@ -15,41 +20,60 @@ public partial class World
{
if (s_freeWorldSlots.TryDequeue(out var index))
{
s_worlds[index] = new World(index, entityCapacity);
s_worlds[index.value] = new World(index, entityCapacity);
}
else
{
if (s_worlds.Count >= WorldID.MaxValue)
{
throw new InvalidOperationException("Maximum number of worlds reached");
}
index = (WorldID)s_worlds.Count;
index = new Identifier<World>(s_worlds.Count);
s_worlds.Add(new World(index, entityCapacity));
}
return s_worlds[index];
return s_worlds[index.value]!;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static World GetWorld(int index)
public static Result<World, ResultStatus> GetWorld(Identifier<World> id)
{
return s_worlds[index];
if (id.value < 0 || id.value >= s_worlds.Count)
{
return Result.Create(default(World)!, ResultStatus.NotFound);
}
var world = s_worlds[id.value];
if (world is null)
{
return Result.Create(default(World)!, ResultStatus.NotFound);
}
return Result.Create(world, ResultStatus.Success);
}
}
public partial class World : IDisposable, IEquatable<World>
public partial class World : IIdentifierType, IDisposable, IEquatable<World>
{
private readonly WorldID _id;
private readonly Identifier<World> _id;
private bool _isDisposed = false;
private UnsafeList<Archetype> _archetypes;
private UnsafeHashMap<int, Identifier<Archetype>> _archetypeLookup; // Signature Hash to Archetype ID
private EntityManager _entityManager;
public WorldID ID => _id;
private bool _disposed = false;
private World(WorldID id, int entityCapacity)
public Identifier<World> ID => _id;
public EntityManager EntityManager => _entityManager;
private World(Identifier<World> id, int entityCapacity)
{
_id = id;
_archetypes = new UnsafeList<Archetype>(16, Allocator.Persistent);
_archetypeLookup = new UnsafeHashMap<int, Identifier<Archetype>>(16, Allocator.Persistent);
_entityManager = new EntityManager(this);
// Create the empty archetype
CreateArchetype(ReadOnlySpan<int>.Empty, 0);
}
~World()
@@ -57,19 +81,32 @@ public partial class World : IDisposable, IEquatable<World>
Dispose();
}
internal Identifier<Archetype> CreateArchetype(ReadOnlySpan<int> componentTypeIDs, int signatureHash)
{
var arcID = new Identifier<Archetype>(_archetypes.Count);
_archetypes.Add(new Archetype(arcID, _id, componentTypeIDs));
_archetypeLookup.Add(signatureHash, arcID);
return arcID;
}
internal ref Archetype GetArchetypeReference(Identifier<Archetype> id)
{
return ref _archetypes[id.value];
}
internal Identifier<Archetype> GetArchetypeIDBySignatureHash(int signatureHash)
{
if (_archetypeLookup.TryGetValue(signatureHash, out var arcID))
{
return arcID;
}
return Identifier<Archetype>.Invalid;
}
public bool Equals(World? other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return _id == other._id;
return other is not null && _id == other._id;
}
public override int GetHashCode()
@@ -94,14 +131,17 @@ public partial class World : IDisposable, IEquatable<World>
public void Dispose()
{
if (_isDisposed)
if (_disposed)
{
return;
}
_archetypes.Dispose();
_archetypeLookup.Dispose();
s_freeWorldSlots.Enqueue(_id);
_isDisposed = true;
_disposed = true;
GC.SuppressFinalize(this);
}