feat: Added a RemoveBehaviour with Parameter

This commit is contained in:
2023-11-30 17:54:32 +03:00
parent bb6990a80c
commit c03d74dbe0
2 changed files with 23 additions and 9 deletions

View File

@@ -55,7 +55,7 @@ public class BehaviourController : IBehaviourController
return false;
}
public IList<T> GetBehaviours<T>() where T : IBehaviour
public IList<T> GetBehaviours<T>()
{
IList<T> behaviours = new List<T>();
foreach (var behaviourItem in this.behaviours)
@@ -70,23 +70,31 @@ public class BehaviourController : IBehaviourController
return behaviours;
}
public void RemoveBehaviour<T>(bool removeAll = false) where T : IBehaviour
public void RemoveBehaviour<T>(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>(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)