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:
63
Ghost.Entities/Template/QueryItem.tt
Normal file
63
Ghost.Entities/Template/QueryItem.tt
Normal file
@@ -0,0 +1,63 @@
|
||||
<#@ template language="C#" debug="false" hostspecific="true" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ include file="Helpers.ttinclude" #>
|
||||
<#@ output extension=".cs" #>
|
||||
|
||||
using Ghost.Entities.Query;
|
||||
|
||||
namespace Ghost.Entities;
|
||||
|
||||
<# for (int arity = 1; arity <= Amount; arity++)
|
||||
{
|
||||
var generics = AppendGenerics(arity);
|
||||
var restrictions = AppendGenericRestrictions(arity, "struct, IComponentData");
|
||||
var constructorParams = Enumerable.Range(0, arity)
|
||||
.Select(i => $"ComponentPool<T{i}> pool{i}")
|
||||
.Aggregate((a, b) => a + ", " + b);
|
||||
var deconstructParams = Enumerable.Range(0, arity)
|
||||
.Select(i => {
|
||||
var name = $"c{i}";
|
||||
return arity == 1
|
||||
? $"out Ref<T0> {name}"
|
||||
: $"out Ref<T{i}> {name}";
|
||||
})
|
||||
.Aggregate((a, b) => a + ", " + b);
|
||||
var deconstructAssigns = Enumerable.Range(0, arity)
|
||||
.Select(i => {
|
||||
var name = $"c{i}";
|
||||
return $"{name} = new (ref _pool{i}.GetRef(_entity));";
|
||||
})
|
||||
.Aggregate((a, b) => a + b);
|
||||
#>
|
||||
public readonly struct QueryItem<<#= generics #>>
|
||||
<#= restrictions #>
|
||||
{
|
||||
private readonly Entity _entity;
|
||||
<# for (int i = 0; i < arity; i++){ #>
|
||||
private readonly ComponentPool<T<#= i #>> _pool<#= i #>;
|
||||
<# } #>
|
||||
|
||||
internal QueryItem(Entity entity, <#= constructorParams #>)
|
||||
{
|
||||
_entity = entity;
|
||||
<# for (int i = 0; i < arity; i++){ #>
|
||||
_pool<#= i #> = pool<#= i #>;
|
||||
<# } #>
|
||||
}
|
||||
|
||||
public Entity Entity => _entity;
|
||||
|
||||
<# for (int i = 0; i < arity; i++){ #>
|
||||
public ref T<#= i #> Component<#= i #> => ref _pool<#= i #>.GetRef(_entity);
|
||||
<# } #>
|
||||
|
||||
// Deconstruct into tuple-like values
|
||||
public void Deconstruct(out Entity entity, <#= deconstructParams #>)
|
||||
{
|
||||
entity = _entity;
|
||||
<#= deconstructAssigns #>
|
||||
}
|
||||
}
|
||||
|
||||
<# } #>
|
||||
Reference in New Issue
Block a user