feat: Added a RemoveBehaviour with Parameter
This commit is contained in:
parent
bb6990a80c
commit
c03d74dbe0
|
@ -63,17 +63,23 @@ public interface IBehaviourController : IAssignableGameObject
|
||||||
/// </returns>
|
/// </returns>
|
||||||
bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour);
|
bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour);
|
||||||
|
|
||||||
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
|
/// <typeparam name="T">An implemented class or <see cref="interface"/>.</typeparam>
|
||||||
/// <returns>Returns a list of all the matching <see cref="IBehaviour"/>s found in the <see cref="IBehaviourController"/>.</returns>
|
/// <returns>Returns a list of all the matching <see cref="IBehaviour"/>s found in the <see cref="IBehaviourController"/>.</returns>
|
||||||
IList<T> GetBehaviours<T>() where T : IBehaviour;
|
IList<T> GetBehaviours<T>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the <see cref="IBehaviour"/> found in the <see cref="IBehaviourController"/>.
|
/// Removes the <see cref="IBehaviour"/> found in the <see cref="IBehaviourController"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="removeAll">If all of the instances of the given Type is to be removed or not.</param>
|
/// <param name="removeAll">If all of the instances of the given Type is to be removed or not.</param>
|
||||||
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
|
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
|
||||||
void RemoveBehaviour<T>(bool removeAll = false) where T : IBehaviour;
|
void RemoveBehaviour<T>(bool removeAll = false) where T : class, IBehaviour;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the <see cref="IBehaviour"/> found in the <see cref="IBehaviourController"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="removeAll">If all of the instances of the given Type is to be removed or not.</param>
|
||||||
|
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
|
||||||
|
void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// To be called in every frame of the engine. Responsible for notifying <see cref="IBehaviour"/>'s under the <see cref="IBehaviourController"/>'s control that a new frame is happening.
|
/// To be called in every frame of the engine. Responsible for notifying <see cref="IBehaviour"/>'s under the <see cref="IBehaviourController"/>'s control that a new frame is happening.
|
||||||
|
|
|
@ -55,7 +55,7 @@ public class BehaviourController : IBehaviourController
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<T> GetBehaviours<T>() where T : IBehaviour
|
public IList<T> GetBehaviours<T>()
|
||||||
{
|
{
|
||||||
IList<T> behaviours = new List<T>();
|
IList<T> behaviours = new List<T>();
|
||||||
foreach (var behaviourItem in this.behaviours)
|
foreach (var behaviourItem in this.behaviours)
|
||||||
|
@ -70,23 +70,31 @@ public class BehaviourController : IBehaviourController
|
||||||
return behaviours;
|
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--)
|
for (int i = behaviours.Count; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (behaviours[i] is not T behaviour)
|
if (behaviours[i] is not T behaviour)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
behaviour.OnPriorityChanged -= OnPriorityChange;
|
RemoveBehaviour(behaviour);
|
||||||
behaviour.Finalize();
|
|
||||||
behaviours.RemoveAt(i);
|
|
||||||
OnBehaviourRemoved?.Invoke(this, behaviour);
|
|
||||||
|
|
||||||
if (!removeAll)
|
if (!removeAll)
|
||||||
return;
|
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)
|
public bool Assign(IGameObject gameObject)
|
||||||
{
|
{
|
||||||
if (GameObject is not null && GameObject.Initialized)
|
if (GameObject is not null && GameObject.Initialized)
|
||||||
|
|
Loading…
Reference in New Issue