forked from Misaki/GhostEngine
Refactor entity-component system and related classes
Changed the `Component` class to an interface `IComponentData` to support a data-oriented design. Changed the `Transform` class from a class to a struct, implementing `IComponentData` and updating properties. Changed the `GameObject` class to use a dictionary for components and added properties for state management. Changed the `PlayerLoopService` class to `GameLoopService` and updated methods to integrate with the new `SceneManager`. Changed the `World` class to manage multiple worlds and enhance entity management with new querying methods. Added the `Scene` class to manage root game objects and their lifecycle. Added new utility classes like `ComponentMask`, `Box<T>`, and `TypeHandle<T>` for better component management. Added the `ScriptComponent` class to allow for modular scriptable components attached to entities. Added the `QueryEnumerable` class to facilitate flexible querying of entities with specific components. Updated the `Test` class in `Program.cs` to demonstrate the new entity and component management system. Updated project files to include new references and settings supporting the changes made in the codebase.
This commit is contained in:
98
Ghost.Entities/Query/QueryFilter.cs
Normal file
98
Ghost.Entities/Query/QueryFilter.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
namespace Ghost.Entities.Query;
|
||||
|
||||
[Flags]
|
||||
internal enum FilterMode
|
||||
{
|
||||
All = 1 << 0,
|
||||
Any = 1 << 1,
|
||||
Absent = 1 << 2,
|
||||
Disabled = 1 << 3,
|
||||
}
|
||||
|
||||
internal readonly struct FilterEntry(nint id, FilterMode mode)
|
||||
{
|
||||
public readonly nint typeHandle = id;
|
||||
public readonly FilterMode mode = mode;
|
||||
}
|
||||
|
||||
internal struct QueryFilter()
|
||||
{
|
||||
internal List<nint> _all = new(6);
|
||||
internal List<nint> _any = new(6);
|
||||
internal List<nint> _absent = new(6);
|
||||
internal List<nint> _disabled = new(6);
|
||||
|
||||
public readonly void ComputeFilterBitMask(World world, ref BitSet result)
|
||||
{
|
||||
BitSet allMask = default;
|
||||
BitSet anyMask = default;
|
||||
BitSet absentMask = default;
|
||||
|
||||
var hasAll = false;
|
||||
var hasAny = false;
|
||||
var hasAbsent = false;
|
||||
|
||||
// Compute All mask (intersection)
|
||||
foreach (var typeHandle in _all)
|
||||
{
|
||||
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
|
||||
|
||||
if (!hasAll)
|
||||
{
|
||||
allMask = new BitSet(mask.Length);
|
||||
allMask.SetAll();
|
||||
hasAll = true;
|
||||
}
|
||||
|
||||
allMask &= mask;
|
||||
}
|
||||
|
||||
// Compute Any mask (union)
|
||||
foreach (var typeHandle in _any)
|
||||
{
|
||||
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
|
||||
|
||||
if (!hasAny)
|
||||
{
|
||||
anyMask = new BitSet(mask.Length);
|
||||
hasAny = true;
|
||||
}
|
||||
|
||||
anyMask |= mask;
|
||||
}
|
||||
|
||||
// Compute Absent mask (union for exclusion)
|
||||
foreach (var typeHandle in _absent)
|
||||
{
|
||||
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
|
||||
|
||||
if (!hasAbsent)
|
||||
{
|
||||
absentMask = new BitSet(mask.Length);
|
||||
hasAbsent = true;
|
||||
}
|
||||
|
||||
absentMask |= mask;
|
||||
}
|
||||
|
||||
result = new BitSet(world.EntityManager.EntityCount);
|
||||
result.SetAll();
|
||||
|
||||
if (hasAll)
|
||||
{
|
||||
result &= allMask;
|
||||
}
|
||||
|
||||
if (hasAny)
|
||||
{
|
||||
result &= anyMask;
|
||||
}
|
||||
|
||||
if (hasAbsent)
|
||||
{
|
||||
result &= ~absentMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user