From fed288859f629025a8588fb689b23fa0d2c2fa36 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 6 Feb 2024 12:47:58 +0300 Subject: [PATCH] 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. --- Engine.Core/Abstract/ITransform.cs | 2 +- Engine.Core/Factory/GameObjectFactory.cs | 3 +++ Engine.Core/Transform.cs | 27 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Engine.Core/Abstract/ITransform.cs b/Engine.Core/Abstract/ITransform.cs index 724bc4a..f78adb8 100644 --- a/Engine.Core/Abstract/ITransform.cs +++ b/Engine.Core/Abstract/ITransform.cs @@ -6,7 +6,7 @@ namespace Syntriax.Engine.Core.Abstract; /// /// Represents the transformation properties of an object such as position, scale, and rotation. /// -public interface ITransform : IEnumerable +public interface ITransform : IAssignableGameObject, IEnumerable { /// /// Event triggered when the of the changes. diff --git a/Engine.Core/Factory/GameObjectFactory.cs b/Engine.Core/Factory/GameObjectFactory.cs index f53ea9e..6713d39 100644 --- a/Engine.Core/Factory/GameObjectFactory.cs +++ b/Engine.Core/Factory/GameObjectFactory.cs @@ -22,6 +22,9 @@ public class GameObjectFactory behaviourController ??= TypeFactory.Get(); stateEnable ??= TypeFactory.Get(); + if (!transform.Assign(gameObject)) + throw AssignException.From(transform, gameObject); + if (!behaviourController.Assign(gameObject)) throw AssignException.From(behaviourController, gameObject); if (!stateEnable.Assign(gameObject)) diff --git a/Engine.Core/Transform.cs b/Engine.Core/Transform.cs index 4a0c54d..6007b34 100644 --- a/Engine.Core/Transform.cs +++ b/Engine.Core/Transform.cs @@ -6,9 +6,13 @@ using Syntriax.Engine.Core.Abstract; 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 Action? OnGameObjectAssigned { get; set; } = null; + + public Action? OnUnassigned { get; set; } = null; + public Action? OnPositionChanged { get; set; } = null; public Action? OnScaleChanged { get; set; } = null; public Action? OnRotationChanged { get; set; } = null; @@ -28,6 +32,7 @@ public class Transform : ITransform private readonly List _children = []; + public IGameObject GameObject { get; private set; } = null!; public ITransform? Parent { get; private set; } = null; public IReadOnlyList Children => _children; @@ -245,4 +250,24 @@ public class Transform : ITransform else _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; + } }