From c03d74dbe0949d75411cd368c8b07dbafd871a20 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Thu, 30 Nov 2023 17:54:32 +0300 Subject: [PATCH] feat: Added a RemoveBehaviour with Parameter --- Engine.Core/Abstract/IBehaviourController.cs | 12 +++++++++--- Engine.Core/BehaviourController.cs | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Engine.Core/Abstract/IBehaviourController.cs b/Engine.Core/Abstract/IBehaviourController.cs index 28548bb..040aa58 100644 --- a/Engine.Core/Abstract/IBehaviourController.cs +++ b/Engine.Core/Abstract/IBehaviourController.cs @@ -63,17 +63,23 @@ public interface IBehaviourController : IAssignableGameObject /// bool TryGetBehaviour([NotNullWhen(returnValue: true)] out T? behaviour); - /// An implemented class or of + /// An implemented class or . /// Returns a list of all the matching s found in the . - IList GetBehaviours() where T : IBehaviour; + IList GetBehaviours(); /// /// Removes the found in the . /// /// If all of the instances of the given Type is to be removed or not. /// An implemented class or of - void RemoveBehaviour(bool removeAll = false) where T : IBehaviour; + void RemoveBehaviour(bool removeAll = false) where T : class, IBehaviour; + /// + /// Removes the found in the . + /// + /// If all of the instances of the given Type is to be removed or not. + /// An implemented class or of + void RemoveBehaviour(T behaviour) where T : class, IBehaviour; /// /// To be called in every frame of the engine. Responsible for notifying 's under the 's control that a new frame is happening. diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index cffe44b..ab2a278 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -55,7 +55,7 @@ public class BehaviourController : IBehaviourController return false; } - public IList GetBehaviours() where T : IBehaviour + public IList GetBehaviours() { IList behaviours = new List(); foreach (var behaviourItem in this.behaviours) @@ -70,23 +70,31 @@ public class BehaviourController : IBehaviourController return behaviours; } - public void RemoveBehaviour(bool removeAll = false) where T : IBehaviour + public void RemoveBehaviour(bool removeAll = false) where T : class, IBehaviour { for (int i = behaviours.Count; i >= 0; i--) { if (behaviours[i] is not T behaviour) continue; - behaviour.OnPriorityChanged -= OnPriorityChange; - behaviour.Finalize(); - behaviours.RemoveAt(i); - OnBehaviourRemoved?.Invoke(this, behaviour); + RemoveBehaviour(behaviour); if (!removeAll) return; } } + public void RemoveBehaviour(T behaviour) where T : class, IBehaviour + { + if (!behaviours.Contains(behaviour)) + throw new Exception($"{behaviour.GetType().Name} does not exist in {GameObject.Name}'s {nameof(IBehaviourController)}."); + + behaviour.OnPriorityChanged -= OnPriorityChange; + behaviour.Finalize(); + behaviours.Remove(behaviour); + OnBehaviourRemoved?.Invoke(this, behaviour); + } + public bool Assign(IGameObject gameObject) { if (GameObject is not null && GameObject.Initialized)