60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using Syntriax.Engine.Core.Abstract;
|
|
using Syntriax.Engine.Core.Exceptions;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
[System.Diagnostics.DebuggerDisplay("{GetType().Name, nq}, Priority: {Priority}, Initialized: {Initialized}")]
|
|
public abstract class Behaviour : BaseEntity, IBehaviour
|
|
{
|
|
public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null;
|
|
|
|
public event IBehaviour.OnPriorityChangedDelegate? OnPriorityChanged = null;
|
|
|
|
|
|
private IBehaviourController _behaviourController = null!;
|
|
|
|
private int _priority = 0;
|
|
|
|
|
|
public IBehaviourController BehaviourController => _behaviourController;
|
|
|
|
public override bool IsActive => base.IsActive && BehaviourController.GameObject.StateEnable.Enabled;
|
|
|
|
public int Priority
|
|
{
|
|
get => _priority;
|
|
set
|
|
{
|
|
if (value == _priority)
|
|
return;
|
|
|
|
int previousPriority = _priority;
|
|
_priority = value;
|
|
OnPriorityChanged?.Invoke(this, previousPriority);
|
|
}
|
|
}
|
|
|
|
public bool Assign(IBehaviourController behaviourController)
|
|
{
|
|
if (Initialized)
|
|
return false;
|
|
|
|
_behaviourController = behaviourController;
|
|
OnBehaviourControllerAssigned?.Invoke(this);
|
|
return true;
|
|
}
|
|
|
|
protected override void UnassignInternal()
|
|
{
|
|
base.UnassignInternal();
|
|
_behaviourController = null!;
|
|
}
|
|
|
|
protected override void InitializeInternal()
|
|
{
|
|
base.InitializeInternal();
|
|
NotAssignedException.Check(this, _behaviourController);
|
|
NotAssignedException.Check(this, StateEnable);
|
|
}
|
|
}
|