refactor: ITransformWithGameObject

This commit is contained in:
Syntriax 2024-02-06 12:57:01 +03:00
parent fed288859f
commit f96c58cbd4
5 changed files with 47 additions and 29 deletions

View File

@ -6,7 +6,7 @@ namespace Syntriax.Engine.Core.Abstract;
/// <summary> /// <summary>
/// Represents the transformation properties of an object such as position, scale, and rotation. /// Represents the transformation properties of an object such as position, scale, and rotation.
/// </summary> /// </summary>
public interface ITransform : IAssignableGameObject, IEnumerable<ITransform> public interface ITransform : IEnumerable<ITransform>
{ {
/// <summary> /// <summary>
/// Event triggered when the <see cref="Position"/> of the <see cref="ITransform"/> changes. /// Event triggered when the <see cref="Position"/> of the <see cref="ITransform"/> changes.

View File

@ -0,0 +1,6 @@
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Represents an <see cref="ITransform"/> attached to an <see cref="IGameObject"/>.
/// </summary>
public interface ITransformWithGameObject : ITransform, IAssignableGameObject { }

View File

@ -9,7 +9,7 @@ public class GameObjectFactory
=> Instantiate<T>(transform: null, behaviourController: null, stateEnable: null, args); => Instantiate<T>(transform: null, behaviourController: null, stateEnable: null, args);
public T Instantiate<T>( public T Instantiate<T>(
ITransform? transform = null, ITransformWithGameObject? transform = null,
IBehaviourController? behaviourController = null, IBehaviourController? behaviourController = null,
IStateEnable? stateEnable = null, IStateEnable? stateEnable = null,
params object?[]? args params object?[]? args
@ -18,7 +18,7 @@ public class GameObjectFactory
{ {
T gameObject = TypeFactory.Get<T>(args); T gameObject = TypeFactory.Get<T>(args);
transform ??= TypeFactory.Get<Transform>(); transform ??= TypeFactory.Get<TransformWithGameObject>();
behaviourController ??= TypeFactory.Get<BehaviourController>(); behaviourController ??= TypeFactory.Get<BehaviourController>();
stateEnable ??= TypeFactory.Get<StateEnable>(); stateEnable ??= TypeFactory.Get<StateEnable>();

View File

@ -6,13 +6,9 @@ using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {GameObject.Name, nq} Position: {Position.ToString(), nq}, Scale: {Scale.ToString(), nq}, Rotation: {Rotation}")] [System.Diagnostics.DebuggerDisplay("Position: {Position.ToString(), nq}, Scale: {Scale.ToString(), nq}, Rotation: {Rotation}")]
public class Transform : ITransform public class Transform : ITransform
{ {
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<ITransform>? OnPositionChanged { get; set; } = null; public Action<ITransform>? OnPositionChanged { get; set; } = null;
public Action<ITransform>? OnScaleChanged { get; set; } = null; public Action<ITransform>? OnScaleChanged { get; set; } = null;
public Action<ITransform>? OnRotationChanged { get; set; } = null; public Action<ITransform>? OnRotationChanged { get; set; } = null;
@ -32,7 +28,6 @@ public class Transform : ITransform
private readonly List<ITransform> _children = []; private readonly List<ITransform> _children = [];
public IGameObject GameObject { get; private set; } = null!;
public ITransform? Parent { get; private set; } = null; public ITransform? Parent { get; private set; } = null;
public IReadOnlyList<ITransform> Children => _children; public IReadOnlyList<ITransform> Children => _children;
@ -250,24 +245,4 @@ public class Transform : ITransform
else else
_rotation = Parent.Rotation + LocalRotation; _rotation = Parent.Rotation + LocalRotation;
} }
public bool Assign(IGameObject gameObject)
{
if (GameObject is not null && GameObject.Initialized)
return false;
GameObject = gameObject;
OnGameObjectAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (GameObject is not null && GameObject.Initialized)
return false;
GameObject = null!;
OnUnassigned?.Invoke(this);
return true;
}
} }

View File

@ -0,0 +1,37 @@
using System;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {GameObject.Name, nq} Position: {Position.ToString(), nq}, Scale: {Scale.ToString(), nq}, Rotation: {Rotation}")]
public class TransformWithGameObject : Transform, ITransformWithGameObject
{
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public IGameObject GameObject { get; private set; } = null!;
public bool Assign(IGameObject gameObject)
{
if (GameObject is not null && GameObject.Initialized)
return false;
GameObject = gameObject;
OnGameObjectAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (GameObject is not null && GameObject.Initialized)
return false;
GameObject = null!;
OnUnassigned?.Invoke(this);
return true;
}
}