forked from Misaki/GhostEngine
Implement core entity management features
Added `Archetype` struct with chunk management and disposal. Added `BitSet` class for managing collections of bits. Added `Class1.cs` as a placeholder for graphics functionality. Added `ComponentData` struct and `ComponentPool` class for component management. Added `ComponentRegistry` for efficient component registration. Added `EntityChangeQueue` as a placeholder for future changes. Added `Helpers.ttinclude` and `QueryRefComponent.tt` for code generation. Added `Signature` struct for managing component signatures. Added `World` struct to manage the game world and entities. Added `QueryRefComponent` delegates for querying entities. Changed `Archetype.cs` to implement `IDisposable`. Changed `AssemblyInfo.cs` to update global using directives. Changed `Chunk.cs` to introduce `ChunkCollection` for chunk management. Changed `Component.cs` to refine component management methods. Changed `Entity.cs` to improve properties and methods. Changed `Ghost.Entities.csproj` to update project properties. Changed `Program.cs` to demonstrate entity creation and querying. Changed `World.Query.cs` to facilitate querying with components.
This commit is contained in:
@@ -5,24 +5,25 @@ namespace Ghost.Entities;
|
||||
[SkipLocalsInit]
|
||||
public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
{
|
||||
private const EntityID _WORLD_INDEX_BITS = 4u;
|
||||
private const EntityID _GENERATION_BITS = 8u;
|
||||
private const EntityID _INDEX_BITS = sizeof(EntityID) * 8 - _WORLD_INDEX_BITS - _GENERATION_BITS;
|
||||
private const int _WORLD_INDEX_BITS = 4;
|
||||
private const int _GENERATION_BITS = 8;
|
||||
private const int _INDEX_BITS = sizeof(EntityID) * 8 - _WORLD_INDEX_BITS - _GENERATION_BITS;
|
||||
|
||||
private const EntityID _WORLD_INDEX_MASK = (1u << (int)_WORLD_INDEX_BITS) - 1;
|
||||
private const EntityID _GENERATION_MASK = (1u << (int)_GENERATION_BITS) - 1;
|
||||
private const EntityID _INDEX_MASK = (1u << (int)_INDEX_BITS) - 1;
|
||||
private const EntityID _ID_MASK = EntityID.MaxValue;
|
||||
private const int _WORLD_INDEX_MASK = (1 << _WORLD_INDEX_BITS) - 1;
|
||||
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 != _ID_MASK;
|
||||
get => ID != INVALID_ID;
|
||||
}
|
||||
|
||||
public readonly EntityID Index
|
||||
public readonly EntityID ID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _id & _INDEX_MASK;
|
||||
@@ -31,29 +32,29 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
public readonly GenerationID Generation
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (GenerationID)(_id >> (int)_INDEX_BITS & _GENERATION_MASK);
|
||||
get => (GenerationID)(_id >> _INDEX_BITS & _GENERATION_MASK);
|
||||
}
|
||||
|
||||
public readonly WorldID WorldIndex
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (WorldID)(_id >> (int)(_INDEX_BITS + _GENERATION_BITS) & _WORLD_INDEX_MASK);
|
||||
get => (WorldID)(_id >> (_INDEX_BITS + _GENERATION_BITS) & _WORLD_INDEX_MASK);
|
||||
}
|
||||
|
||||
public void IncrementGeneration()
|
||||
{
|
||||
var generation = Generation + 1u;
|
||||
var generation = Generation + 1;
|
||||
if (generation >= _GENERATION_MASK)
|
||||
{
|
||||
throw new InvalidOperationException("Generation overflow");
|
||||
}
|
||||
|
||||
_id = _id & ~(_GENERATION_MASK << (int)_INDEX_BITS) | generation << (int)_INDEX_BITS;
|
||||
_id = _id & ~(_GENERATION_MASK << _INDEX_BITS) | generation << _INDEX_BITS;
|
||||
}
|
||||
|
||||
internal Entity(EntityID index, EntityID generation, EntityID worldIndex)
|
||||
{
|
||||
_id = worldIndex << (int)(_INDEX_BITS + _GENERATION_BITS) | generation << (int)_INDEX_BITS | index;
|
||||
_id = worldIndex << (_INDEX_BITS + _GENERATION_BITS) | generation << _INDEX_BITS | index;
|
||||
}
|
||||
|
||||
public readonly bool Equals(Entity other)
|
||||
@@ -88,6 +89,6 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return $"Entity {{ Index: {Index}, Generation: {Generation}, WorldIndex: {WorldIndex} }}";
|
||||
return $"Entity {{ Index: {ID}, Generation: {Generation}, WorldIndex: {WorldIndex} }}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user