using System; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Exceptions; namespace Syntriax.Engine.Core; public abstract class Behaviour : IBehaviour { public Action? OnStateEnableAssigned { get; set; } = null; public Action? OnBehaviourControllerAssigned { get; set; } = null; public Action? OnInitialized { get; set; } = null; public Action? OnFinalized { get; set; } = null; public Action? OnPriorityChanged { get; set; } = null; private IBehaviourController _behaviourController = null!; private IStateEnable _stateEnable = null!; private bool _initialized = false; private int _priority = 0; public IStateEnable StateEnable => _stateEnable; public IBehaviourController BehaviourController => _behaviourController; public bool Initialized { get => _initialized; private set { if (value == _initialized) return; _initialized = value; if (value) OnInitialized?.Invoke(this); else OnFinalized?.Invoke(this); } } public int Priority { get => _priority; set { if (value == _priority) return; _priority = value; OnPriorityChanged?.Invoke(this); } } public bool Assign(IStateEnable stateEnable) { if (_initialized) return false; _stateEnable = stateEnable; _stateEnable.Assign(this); OnStateEnableAssigned?.Invoke(this); return true; } public bool Assign(IBehaviourController behaviourController) { if (_behaviourController is not null) return false; _behaviourController = behaviourController; OnBehaviourControllerAssigned?.Invoke(this); return true; } public bool Initialize() { if (Initialized) return false; NotAssignedException.Check(this, _behaviourController); NotAssignedException.Check(this, _stateEnable); Initialized = true; return true; } public bool Finalize() { if (!Initialized) return false; _behaviourController = null!; _stateEnable = null!; Initialized = false; return true; } }