Syntriax.Engine/Engine.Core/Behaviour.cs

60 lines
1.6 KiB
C#
Raw Permalink Normal View History

2023-11-23 22:07:49 +03:00
using System;
2023-11-24 17:20:43 +03:00
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
{
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;
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;
_priority = value;
OnPriorityChanged?.Invoke(this);
}
}
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
}
}