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.
77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
using Ghost.Engine.Helpers;
|
|
using Ghost.Entities;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Ghost.Engine.Components;
|
|
|
|
public struct Transform : IComponentData
|
|
{
|
|
private Vector3 _position = Vector3.Zero;
|
|
public Vector3 Position
|
|
{
|
|
get => _position;
|
|
set
|
|
{
|
|
_position = value;
|
|
hasChanged = true;
|
|
UpdateMatrices();
|
|
}
|
|
}
|
|
|
|
private Quaternion _rotation = Quaternion.Identity;
|
|
public Quaternion Rotation
|
|
{
|
|
get => _rotation;
|
|
set
|
|
{
|
|
_rotation = value;
|
|
hasChanged = true;
|
|
UpdateMatrices();
|
|
}
|
|
}
|
|
|
|
private Vector3 _scale = Vector3.One;
|
|
public Vector3 Scale
|
|
{
|
|
get => _scale;
|
|
set
|
|
{
|
|
_scale = value;
|
|
hasChanged = true;
|
|
UpdateMatrices();
|
|
}
|
|
}
|
|
|
|
public bool hasChanged;
|
|
|
|
private Matrix4x4 _localToWorldMatrix;
|
|
private Matrix4x4 _worldToLocalMatrix;
|
|
|
|
public readonly Matrix4x4 LocalToWorldMatrix => _localToWorldMatrix;
|
|
public readonly Matrix4x4 WorldToLocalMatrix => _worldToLocalMatrix;
|
|
|
|
public static Transform Default
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => new(Vector3.Zero, Quaternion.Identity, Vector3.One);
|
|
}
|
|
|
|
public Transform(Vector3 position, Quaternion rotation, Vector3 scale)
|
|
{
|
|
_position = position;
|
|
_rotation = rotation;
|
|
_scale = scale;
|
|
hasChanged = false;
|
|
_localToWorldMatrix = Matrix4x4.Identity;
|
|
_worldToLocalMatrix = Matrix4x4.Identity;
|
|
|
|
UpdateMatrices();
|
|
}
|
|
|
|
private void UpdateMatrices()
|
|
{
|
|
_localToWorldMatrix = MatrixHelpers.CreateTRS(_position, _rotation, _scale);
|
|
Matrix4x4.Invert(_localToWorldMatrix, out _worldToLocalMatrix);
|
|
}
|
|
} |