feat: IActive interface added for hierarchy active state
This commit is contained in:
@@ -16,7 +16,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
||||
|
||||
private readonly List<T> monitoringBehaviours = new(32);
|
||||
protected readonly List<T> activeBehaviours = new(32);
|
||||
protected readonly Dictionary<IStateEnable, T> monitoringStateToBehaviour = new(32);
|
||||
protected readonly Dictionary<IActive, T> monitoringActiveToBehaviour = new(32);
|
||||
|
||||
public IReadOnlyList<T> Behaviours => activeBehaviours;
|
||||
public IGameManager GameManager { get; private set; } = null!;
|
||||
@@ -51,23 +51,22 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
||||
return;
|
||||
|
||||
monitoringBehaviours.Add(tBehaviour);
|
||||
monitoringStateToBehaviour.Add(tBehaviour.StateEnable, tBehaviour);
|
||||
tBehaviour.StateEnable.OnEnabledChanged += OnBehaviourStateChanged;
|
||||
OnBehaviourStateChanged(tBehaviour.StateEnable, !tBehaviour.StateEnable.Enabled);
|
||||
monitoringActiveToBehaviour.Add(tBehaviour, tBehaviour);
|
||||
tBehaviour.OnActiveChanged += OnBehaviourStateChanged;
|
||||
OnBehaviourStateChanged(tBehaviour, !tBehaviour.IsActive);
|
||||
}
|
||||
|
||||
private void OnBehaviourStateChanged(IStateEnable sender, bool previousState)
|
||||
private void OnBehaviourStateChanged(IActive sender, bool previousState)
|
||||
{
|
||||
T behaviour = monitoringStateToBehaviour[sender];
|
||||
if (sender.Enabled)
|
||||
T behaviour = monitoringActiveToBehaviour[sender];
|
||||
if (sender.IsActive)
|
||||
{
|
||||
activeBehaviours.Add(behaviour);
|
||||
OnBehaviourAdd(behaviour);
|
||||
OnCollected?.Invoke(this, behaviour);
|
||||
}
|
||||
else if (activeBehaviours.Contains(behaviour))
|
||||
else if (activeBehaviours.Remove(behaviour))
|
||||
{
|
||||
activeBehaviours.Remove(behaviour);
|
||||
OnBehaviourRemove(behaviour);
|
||||
OnRemoved?.Invoke(this, behaviour);
|
||||
}
|
||||
@@ -79,10 +78,15 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
||||
if (behaviour is not T tBehaviour)
|
||||
return;
|
||||
|
||||
if (!monitoringBehaviours.Remove(tBehaviour) || !monitoringStateToBehaviour.Remove(tBehaviour.StateEnable))
|
||||
if (!monitoringBehaviours.Remove(tBehaviour) || !monitoringActiveToBehaviour.Remove(tBehaviour))
|
||||
return;
|
||||
|
||||
tBehaviour.StateEnable.OnEnabledChanged -= OnBehaviourStateChanged;
|
||||
tBehaviour.OnActiveChanged -= OnBehaviourStateChanged;
|
||||
if (activeBehaviours.Remove(tBehaviour))
|
||||
{
|
||||
OnBehaviourRemove(tBehaviour);
|
||||
OnRemoved?.Invoke(this, tBehaviour);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Assign(IGameManager gameManager)
|
||||
|
Reference in New Issue
Block a user