forked from Misaki/GhostEngine
Changed project name
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
using Ghost.Entities.Components;
|
||||
using Ghost.Entities.Query;
|
||||
using Ghost.Entities.Systems;
|
||||
using System.Diagnostics;
|
||||
using Ghost.Core;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Entities;
|
||||
|
||||
// TODO: Archetype system for better performance
|
||||
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;
|
||||
|
||||
@@ -21,53 +20,63 @@ 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 EntityManager _entityManager;
|
||||
private readonly ComponentStorage _componentStorage;
|
||||
private readonly SystemStorage _systemStorage;
|
||||
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;
|
||||
private EntityCommandBuffer _entityCommandBuffer;
|
||||
|
||||
internal ComponentStorage ComponentStorage => _componentStorage;
|
||||
private bool _disposed = false;
|
||||
|
||||
public WorldID ID => _id;
|
||||
public Identifier<World> ID => _id;
|
||||
public EntityManager EntityManager => _entityManager;
|
||||
public SystemStorage SystemStorage => _systemStorage;
|
||||
public EntityCommandBuffer EntityCommandBuffer => _entityCommandBuffer;
|
||||
|
||||
public event Action<World, Entity, Type>? ComponentChanged;
|
||||
|
||||
private World(WorldID id, int entityCapacity)
|
||||
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, entityCapacity);
|
||||
_componentStorage = new ComponentStorage(this);
|
||||
_systemStorage = new SystemStorage(this);
|
||||
_entityCommandBuffer = new EntityCommandBuffer(_entityManager);
|
||||
|
||||
// Create the empty archetype
|
||||
CreateArchetype(ReadOnlySpan<Identifier<IComponent>>.Empty, 0);
|
||||
}
|
||||
|
||||
~World()
|
||||
@@ -75,53 +84,33 @@ public partial class World : IDisposable, IEquatable<World>
|
||||
Dispose();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public CompRef<T> GetSingleton<T>()
|
||||
where T : unmanaged, IComponentData
|
||||
internal Identifier<Archetype> CreateArchetype(ReadOnlySpan<Identifier<IComponent>> componentTypeIDs, int signatureHash)
|
||||
{
|
||||
ref var component = ref CollectionsMarshal.GetValueRefOrAddDefault(SingletonContainer<T>.container, _id, out _);
|
||||
return new CompRef<T>(ref component);
|
||||
var arcID = new Identifier<Archetype>(_archetypes.Count);
|
||||
_archetypes.Add(new Archetype(arcID, _id, componentTypeIDs));
|
||||
_archetypeLookup.Add(signatureHash, arcID);
|
||||
|
||||
return arcID;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IEnumerable<ScriptComponent> QueryScript()
|
||||
internal ref Archetype GetArchetypeReference(Identifier<Archetype> id)
|
||||
{
|
||||
if (_componentStorage.ScriptComponentPool.IsInitialized)
|
||||
return ref _archetypes[id.value];
|
||||
}
|
||||
|
||||
internal Identifier<Archetype> GetArchetypeIDBySignatureHash(int signatureHash)
|
||||
{
|
||||
if (_archetypeLookup.TryGetValue(signatureHash, out var arcID))
|
||||
{
|
||||
return _componentStorage.ScriptComponentPool.ExecutionList!;
|
||||
return arcID;
|
||||
}
|
||||
|
||||
return Enumerable.Empty<ScriptComponent>();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[Conditional("GHOST_EDITOR")]
|
||||
public void NotifyComponentChanged(Entity entity, Type type)
|
||||
{
|
||||
ComponentChanged?.Invoke(this, entity, type);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[Conditional("GHOST_EDITOR")]
|
||||
public void NotifyComponentChanged<T>(Entity entity)
|
||||
where T : unmanaged, IComponentData
|
||||
{
|
||||
NotifyComponentChanged(entity, typeof(T));
|
||||
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()
|
||||
@@ -146,19 +135,21 @@ public partial class World : IDisposable, IEquatable<World>
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isDisposed)
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_entityManager.Dispose();
|
||||
_componentStorage.Dispose();
|
||||
_systemStorage.Dispose();
|
||||
|
||||
_archetypes.Dispose();
|
||||
_archetypeLookup.Dispose();
|
||||
|
||||
s_freeWorldSlots.Enqueue(_id);
|
||||
|
||||
_isDisposed = true;
|
||||
_disposed = true;
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user