From 5a01b01215c74b47d0ef1abcac51b04102b68f4d Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 24 Nov 2023 17:03:21 +0300 Subject: [PATCH] feat: Added IAssignable.Unassign() --- Engine.Core/Abstract/Assignable/IAssignable.cs | 18 +++++++++++++++++- Engine.Core/Behaviour.cs | 14 ++++++++++++++ Engine.Core/BehaviourController.cs | 11 +++++++++++ Engine.Core/GameManager.cs | 11 +++++++++++ Engine.Core/GameObject.cs | 14 ++++++++++++++ Engine.Core/StateEnable.cs | 13 ++++++++++++- 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Engine.Core/Abstract/Assignable/IAssignable.cs b/Engine.Core/Abstract/Assignable/IAssignable.cs index d377e57..32fdb9c 100644 --- a/Engine.Core/Abstract/Assignable/IAssignable.cs +++ b/Engine.Core/Abstract/Assignable/IAssignable.cs @@ -1,6 +1,22 @@ +using System; + namespace Syntriax.Engine.Core.Abstract; /// /// Indicates the class implementing it has Assignable fields that are necessary for the engine to work properly. /// -public interface IAssignable { } +public interface IAssignable +{ + /// + /// Callback triggered when the 's fields are unassigned and completely ready to recycle. + /// + Action? OnUnassigned { get; set; } + + /// + /// Unassign 's all fields and make it ready to recycle. + /// + /// + /// , if the fields are unsigned successfully, if not. + /// + bool Unassign(); +} diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index abe8a3d..34bf242 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -6,6 +6,7 @@ namespace Syntriax.Engine.Core; public abstract class Behaviour : IBehaviour { + public Action? OnUnassigned { get; set; } = null; public Action? OnStateEnableAssigned { get; set; } = null; public Action? OnBehaviourControllerAssigned { get; set; } = null; @@ -73,6 +74,19 @@ public abstract class Behaviour : IBehaviour return true; } + public bool Unassign() + { + if (Initialized) + return false; + + _stateEnable = null!; + _behaviourController = null!; + _stateEnable.Unassign(); + + OnUnassigned?.Invoke(this); + return true; + } + public bool Initialize() { if (Initialized) diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index 3bf59d4..76aa214 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -15,6 +15,7 @@ public class BehaviourController : IBehaviourController public Action? OnBehaviourAdded { get; set; } = null; public Action? OnBehaviourRemoved { get; set; } = null; + public Action? OnUnassigned { get; set; } = null; public Action? OnGameObjectAssigned { get; set; } = null; @@ -95,6 +96,16 @@ public class BehaviourController : IBehaviourController return true; } + public bool Unassign() + { + if (GameObject.Initialized) + return false; + + _gameObject = null!; + OnUnassigned?.Invoke(this); + return true; + } + public void Update(GameTime gameTime) { if (!GameObject.StateEnable.Enabled) diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index c6efd69..64450cd 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -14,6 +14,7 @@ public class GameManager : IInitialize { public Action? OnInitialized { get; set; } = null; public Action? OnFinalized { get; set; } = null; + public Action? OnUnassigned { get; set; } = null; public Action? OnStateEnableAssigned { get; set; } = null; @@ -98,6 +99,16 @@ public class GameManager : IInitialize return true; } + public bool Unassign() + { + if (Initialized) + return false; + + _stateEnable = null!; + OnUnassigned?.Invoke(this); + return true; + } + public void Update(GameTime time) { foreach (var gameObject in GameObjects) diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 8fc6918..47d6acc 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -11,6 +11,7 @@ public class GameObject : IGameObject { public Action? OnStateEnableAssigned { get; set; } = null; public Action? OnTransformAssigned { get; set; } = null; + public Action? OnUnassigned { get; set; } = null; public Action? OnBehaviourControllerAssigned { get; set; } = null; public Action? OnNameChanged { get; set; } = null; @@ -126,6 +127,19 @@ public class GameObject : IGameObject return true; } + public bool Unassign() + { + if (Initialized) + return false; + + _stateEnable = null!; + _transform = null!; + _behaviourController = null!; + + OnUnassigned?.Invoke(this); + return true; + } + public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; } private void ConnectBehaviourController(IAssignableBehaviourController controller) { diff --git a/Engine.Core/StateEnable.cs b/Engine.Core/StateEnable.cs index 1c047cb..9815430 100644 --- a/Engine.Core/StateEnable.cs +++ b/Engine.Core/StateEnable.cs @@ -6,6 +6,7 @@ namespace Syntriax.Engine.Core; public class StateEnable : IStateEnable { + public Action? OnUnassigned { get; set; } = null; public Action? OnEntityAssigned { get; set; } = null; public Action? OnEnabledChanged { get; set; } = null; @@ -29,11 +30,21 @@ public class StateEnable : IStateEnable public bool Assign(IEntity entity) { - if (_entity is not null) // TODO: IInitialize Maybe? + if (_entity is not null) // TODO: Add IInitialize to IAssignable or IEntity maybe? return false; _entity = entity; OnEntityAssigned?.Invoke(this); return true; } + + public bool Unassign() + { + if (_entity is null) + return false; + + _entity = null!; + OnUnassigned?.Invoke(this); + return true; + } }