feat: IAssignableGameObject to ITransform

I originally didn't want ITransform to have a reference to any IGameObject, since it didn't seem necessary to couple these two, and to make ITransform more flexible and reusable but without it we can't get a reference to the IGameObject(s) that's using that ITransform without doing some very stupid workarounds. I'll try to find a better way for this.
This commit is contained in:
Syntriax 2024-02-06 12:47:58 +03:00
parent 6e4c9b0ef8
commit fed288859f
3 changed files with 30 additions and 2 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 : IEnumerable<ITransform> public interface ITransform : IAssignableGameObject, 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

@ -22,6 +22,9 @@ public class GameObjectFactory
behaviourController ??= TypeFactory.Get<BehaviourController>(); behaviourController ??= TypeFactory.Get<BehaviourController>();
stateEnable ??= TypeFactory.Get<StateEnable>(); stateEnable ??= TypeFactory.Get<StateEnable>();
if (!transform.Assign(gameObject))
throw AssignException.From(transform, gameObject);
if (!behaviourController.Assign(gameObject)) if (!behaviourController.Assign(gameObject))
throw AssignException.From(behaviourController, gameObject); throw AssignException.From(behaviourController, gameObject);
if (!stateEnable.Assign(gameObject)) if (!stateEnable.Assign(gameObject))

View File

@ -6,9 +6,13 @@ using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Position: {Position.ToString(), nq}, Scale: {Scale.ToString(), nq}, Rotation: {Rotation}")] [System.Diagnostics.DebuggerDisplay("Name: {GameObject.Name, nq} 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;
@ -28,6 +32,7 @@ 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;
@ -245,4 +250,24 @@ 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;
}
} }