Files
GhostEngine/Ghost.Entities/World.cs
Misaki 1724072f7e Add component editors and UI controls
Added the `HierarchyEditor` and `LocalToWorldEditor` classes to implement custom component editing functionality.
Added the `Vector3Field` control for 3D vector manipulation and its corresponding XAML definition.
Added the `ComponentDataView` and `ComponentObject` classes to manage component data display and access.
Added the `CustomEditorAttribute` to mark classes as custom editors for specific components.

Changed the `IInspectable` interface to use properties for `Icon`, `HeaderContent`, and `InspectorContent`.
Changed the `PropertyField` class to enhance UI control binding capabilities.
Changed the `EditorWorldManager` to improve world data loading and deserialization processes.
Changed the `EntityNode` and `WorldNode` classes to update entity construction and component querying.
Changed the `StaticResource` class to include new binding flags for component properties.
Changed the `InspectorService` to remove old contract references and adopt new interfaces.
Changed the `QueryEnumerable` and related files to update generic constraints for improved type safety.
Changed the `QueryItem` class to reflect new generic constraints and enhance deconstruction.
Changed the `World.Query` methods to utilize the updated generic constraints.

Updated the `SerializationTest` to align with new entity creation and management practices.
2025-06-20 20:19:14 +09:00

130 lines
3.4 KiB
C#

using Ghost.Entities.Components;
using Ghost.Entities.Query;
using Ghost.Entities.Systems;
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();
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
public static World Create(int entityCapacity = 16)
{
lock (s_worlds)
{
if (s_freeWorldSlots.TryDequeue(out var index))
{
s_worlds[index] = new World(index, entityCapacity);
}
else
{
if (s_worlds.Count >= WorldID.MaxValue)
{
throw new InvalidOperationException("Maximum number of worlds reached");
}
index = (WorldID)s_worlds.Count;
s_worlds.Add(new World(index, entityCapacity));
}
return s_worlds[index];
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static World GetWorld(int index)
{
return s_worlds[index];
}
}
public partial class World : IDisposable, IEquatable<World>
{
private readonly WorldID _id;
private readonly EntityManager _entityManager;
private readonly ComponentStorage _componentStorage;
private readonly SystemStorage _systemStorage;
internal ComponentStorage ComponentStorage => _componentStorage;
public WorldID ID => _id;
public EntityManager EntityManager => _entityManager;
public SystemStorage SystemStorage => _systemStorage;
private World(WorldID id, int entityCapacity)
{
_id = id;
_entityManager = new EntityManager(this, entityCapacity);
_componentStorage = new ComponentStorage(this);
_systemStorage = new SystemStorage(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Ref<T> GetSingleton<T>()
where T : unmanaged, IComponentData
{
ref var component = ref CollectionsMarshal.GetValueRefOrAddDefault(SingletonContainer<T>.container, _id, out _);
return new Ref<T>(ref component);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IEnumerable<ScriptComponent> QueryScript()
{
if (_componentStorage.ScriptComponentPool.IsInitialized)
{
return _componentStorage.ScriptComponentPool.ExecutionList!;
}
return Enumerable.Empty<ScriptComponent>();
}
public void Dispose()
{
_entityManager.Dispose();
_componentStorage.Dispose();
_systemStorage.Dispose();
s_freeWorldSlots.Enqueue(_id);
}
public bool Equals(World? other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return _id == other._id;
}
public override int GetHashCode()
{
return _id.GetHashCode();
}
public override bool Equals(object? obj)
{
return obj is World other && Equals(other);
}
public static bool operator ==(World? left, World? right)
{
return left?.Equals(right) ?? right is null;
}
public static bool operator !=(World? left, World? right)
{
return !(left == right);
}
}