Syntriax.Engine/Engine.Core/Behaviour.cs

60 lines
1.6 KiB
C#

using System;
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 Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IBehaviour>? OnPriorityChanged { get; set; } = 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;
_priority = value;
OnPriorityChanged?.Invoke(this);
}
}
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);
}
}