feat: IActive interface added for hierarchy active state
This commit is contained in:
@@ -7,17 +7,14 @@ namespace Syntriax.Engine.Core;
|
||||
public abstract class BehaviourBase : BaseEntity, IBehaviour
|
||||
{
|
||||
public event IHasBehaviourController.BehaviourControllerAssignedEventHandler? OnBehaviourControllerAssigned = null;
|
||||
|
||||
public event IBehaviour.PriorityChangedEventHandler? OnPriorityChanged = null;
|
||||
public event IActive.ActiveChangedEventHandler? OnActiveChanged = null;
|
||||
|
||||
private IBehaviourController _behaviourController = null!;
|
||||
|
||||
private int _priority = 0;
|
||||
|
||||
public IBehaviourController BehaviourController => _behaviourController;
|
||||
|
||||
public override bool IsActive => base.IsActive && BehaviourController.HierarchyObject.StateEnable.Enabled;
|
||||
|
||||
private int _priority = 0;
|
||||
public int Priority
|
||||
{
|
||||
get => _priority;
|
||||
@@ -32,18 +29,40 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsActive { get; private set; } = false;
|
||||
|
||||
protected virtual void OnAssign(IBehaviourController behaviourController) { }
|
||||
public bool Assign(IBehaviourController behaviourController)
|
||||
{
|
||||
if (IsInitialized)
|
||||
return false;
|
||||
|
||||
_behaviourController = behaviourController;
|
||||
OnAssign(behaviourController);
|
||||
behaviourController.OnHierarchyObjectAssigned += OnHierarchyObjectAssigned;
|
||||
if (behaviourController.HierarchyObject is not null)
|
||||
OnHierarchyObjectAssigned(behaviourController);
|
||||
OnBehaviourControllerAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnHierarchyObjectAssigned(IHasHierarchyObject sender)
|
||||
{
|
||||
sender.HierarchyObject.OnActiveChanged += OnHierarchyObjectActiveChanged;
|
||||
UpdateActive();
|
||||
}
|
||||
|
||||
protected override void OnAssign(IStateEnable stateEnable)
|
||||
{
|
||||
base.OnAssign(stateEnable);
|
||||
|
||||
stateEnable.OnEnabledChanged += OnStateEnabledChanged;
|
||||
}
|
||||
|
||||
protected override void UnassignInternal()
|
||||
{
|
||||
StateEnable.OnEnabledChanged -= OnStateEnabledChanged;
|
||||
BehaviourController.OnHierarchyObjectAssigned -= OnHierarchyObjectAssigned;
|
||||
base.UnassignInternal();
|
||||
_behaviourController = null!;
|
||||
}
|
||||
@@ -54,4 +73,16 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
|
||||
NotAssignedException.Check(this, _behaviourController);
|
||||
NotAssignedException.Check(this, StateEnable);
|
||||
}
|
||||
|
||||
private void OnStateEnabledChanged(IStateEnable sender, bool previousState) => UpdateActive();
|
||||
private void OnHierarchyObjectActiveChanged(IActive sender, bool previousState) => UpdateActive();
|
||||
|
||||
private void UpdateActive()
|
||||
{
|
||||
bool previousActive = IsActive;
|
||||
IsActive = StateEnable.Enabled && _behaviourController.HierarchyObject.IsActive;
|
||||
|
||||
if (previousActive != IsActive)
|
||||
OnActiveChanged?.Invoke(this, previousActive);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user