Syntriax.Engine/Engine.Core/Behaviour.cs

60 lines
1.6 KiB
C#
Raw Normal View History

2023-11-23 22:07:49 +03:00
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("{GetType().Name, nq}, Priority: {Priority}, Initialized: {Initialized}")]
2024-02-02 12:11:51 +03:00
public abstract class Behaviour : BaseEntity, IBehaviour
2023-11-23 22:07:49 +03:00
{
2024-07-15 01:13:39 +03:00
public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null;
2023-11-23 22:07:49 +03:00
2024-07-15 01:13:39 +03:00
public event IBehaviour.OnPriorityChangedDelegate? OnPriorityChanged = null;
2023-11-23 22:07:49 +03:00
private IBehaviourController _behaviourController = null!;
private int _priority = 0;
2024-07-15 01:13:39 +03:00
2023-11-23 22:07:49 +03:00
public IBehaviourController BehaviourController => _behaviourController;
2024-02-02 12:11:51 +03:00
public override bool IsActive => base.IsActive && BehaviourController.GameObject.StateEnable.Enabled;
2023-11-23 22:07:49 +03:00
public int Priority
{
get => _priority;
set
{
if (value == _priority)
return;
2024-07-15 01:13:39 +03:00
int previousPriority = _priority;
2023-11-23 22:07:49 +03:00
_priority = value;
2024-07-15 01:13:39 +03:00
OnPriorityChanged?.Invoke(this, previousPriority);
2023-11-23 22:07:49 +03:00
}
}
public bool Assign(IBehaviourController behaviourController)
{
if (Initialized)
2023-11-23 22:07:49 +03:00
return false;
_behaviourController = behaviourController;
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
2024-02-02 12:11:51 +03:00
protected override void UnassignInternal()
2023-11-24 17:03:21 +03:00
{
2024-02-02 12:11:51 +03:00
base.UnassignInternal();
2023-11-24 17:03:21 +03:00
_behaviourController = null!;
}
2024-02-02 12:11:51 +03:00
protected override void InitializeInternal()
2023-11-23 22:07:49 +03:00
{
2024-02-02 12:11:51 +03:00
base.InitializeInternal();
2023-11-23 22:07:49 +03:00
NotAssignedException.Check(this, _behaviourController);
2024-02-02 12:11:51 +03:00
NotAssignedException.Check(this, StateEnable);
2023-11-23 22:07:49 +03:00
}
}