Files
GhostEngine/Ghost.Entities/Entity.cs
Misaki bab3be2508 Refactor project structure and improve performance
Changed the `ProjectRepository` class to be static for easier usage.
Changed `ProjectService` constants to public properties for accessibility.
Changed `App.xaml` to consolidate theme resources into `Override.xaml`.
Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management.
Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure.
Changed `ProjectPage` to use the `ExplorerItem` model for asset display.
Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface.
Changed the `Logger` class to introduce structured logging functionality.
Changed the system architecture to support dependency management for better organization.
Changed the `QueryEnumerable` class to allow for more flexible entity queries.
Changed the `TypeHandle` class to improve efficiency in retrieving type handles.
Changed the `World` class to support robust world management and multiple worlds.
Updated the `Test` class to demonstrate the new entity and component management system.
2025-06-05 21:45:50 +09:00

93 lines
2.3 KiB
C#

using System.Runtime.CompilerServices;
namespace Ghost.Entities;
[SkipLocalsInit]
public struct Entity : IEquatable<Entity>, IComparable<Entity>
{
// Is 256 generations enough? If not, increase the size of GenerationID or make generation as a separate int field.
public const int GENERATION_BITS = sizeof(GenerationID) * 8;
public const int INDEX_BITS = sizeof(EntityID) * 8 - GENERATION_BITS;
private const int _GENERATION_MASK = (1 << GENERATION_BITS) - 1;
private const int _INDEX_MASK = (1 << INDEX_BITS) - 1;
public const EntityID INVALID_ID = -1;
private EntityID _id;
public readonly bool IsValid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ID != INVALID_ID;
}
public readonly EntityID ID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _id & _INDEX_MASK;
}
public readonly GenerationID Generation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (GenerationID)(_id >> INDEX_BITS & _GENERATION_MASK);
}
public static Entity Invalid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(INVALID_ID, 0);
}
internal Entity(EntityID id, GenerationID generation)
{
_id = generation << INDEX_BITS | id;
}
internal void IncrementGeneration()
{
var generation = Generation + 1;
if (generation >= _GENERATION_MASK)
{
throw new InvalidOperationException("Generation overflow");
}
_id = _id & ~(_GENERATION_MASK << INDEX_BITS) | generation << INDEX_BITS;
}
public readonly bool Equals(Entity other)
{
return _id == other._id;
}
public readonly int CompareTo(Entity other)
{
return _id.CompareTo(other._id);
}
public override readonly bool Equals(object? obj)
{
return obj is Entity other && Equals(other);
}
public override readonly int GetHashCode()
{
return _id.GetHashCode();
}
public static bool operator ==(Entity left, Entity right)
{
return left.Equals(right);
}
public static bool operator !=(Entity left, Entity right)
{
return !(left == right);
}
public override readonly string ToString()
{
return $"Entity {{ Index: {ID}, Generation: {Generation} }}";
}
}