2023-11-23 22:07:49 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
public abstract class Behaviour : BehaviourBase
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-09-30 12:18:13 +03:00
|
|
|
private bool isInitializedThisFrame = false;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-11-17 21:57:00 +03:00
|
|
|
protected IGameManager GameManager => BehaviourController.GameObject.GameManager;
|
2024-09-30 12:18:13 +03:00
|
|
|
protected IGameObject GameObject => BehaviourController.GameObject;
|
|
|
|
protected ITransform Transform => BehaviourController.GameObject.Transform;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
public Behaviour()
|
|
|
|
{
|
|
|
|
OnInitialized += OnInitialize;
|
|
|
|
OnFinalized += OnFinalize;
|
|
|
|
OnUnassigned += OnUnassign;
|
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnUnassign() { }
|
|
|
|
private void OnUnassign(IAssignable assignable) => OnUnassign();
|
2024-07-15 01:13:39 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnInitialize() { }
|
|
|
|
private void OnInitialize(IInitialize _)
|
|
|
|
{
|
|
|
|
isInitializedThisFrame = true;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
BehaviourController.OnPreUpdate += PreUpdate;
|
|
|
|
BehaviourController.OnPreDraw += PreDraw;
|
|
|
|
BehaviourController.OnUpdate += Update;
|
2024-10-22 22:02:38 +03:00
|
|
|
BehaviourController.GameObject.OnEnteredHierarchy += EnteredHierarchy;
|
|
|
|
BehaviourController.GameObject.OnExitedHierarchy += ExitedHierarchy;
|
2024-11-03 19:58:57 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
OnInitialize();
|
2024-11-03 19:58:57 +03:00
|
|
|
|
|
|
|
if (GameObject.IsInHierarchy)
|
2024-11-17 21:57:00 +03:00
|
|
|
EnteredHierarchy(GameObject, GameManager);
|
2024-09-30 12:18:13 +03:00
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnFinalize() { }
|
|
|
|
private void OnFinalize(IInitialize _)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-09-30 12:18:13 +03:00
|
|
|
BehaviourController.OnPreUpdate -= PreUpdate;
|
|
|
|
BehaviourController.OnPreDraw -= PreDraw;
|
|
|
|
BehaviourController.OnUpdate -= Update;
|
2024-10-22 22:02:38 +03:00
|
|
|
BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy;
|
|
|
|
BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy;
|
2024-11-03 20:13:25 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
OnFinalize();
|
2024-11-03 20:13:25 +03:00
|
|
|
|
|
|
|
if (GameObject.IsInHierarchy)
|
2024-11-17 21:57:00 +03:00
|
|
|
ExitedHierarchy(GameObject, GameManager);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnPreUpdatePreActiveCheck() { }
|
|
|
|
protected virtual void OnPreUpdate() { }
|
|
|
|
private void PreUpdate(IBehaviourController _)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-09-30 12:18:13 +03:00
|
|
|
OnPreUpdatePreActiveCheck();
|
|
|
|
|
|
|
|
if (!IsActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isInitializedThisFrame)
|
|
|
|
FirstActiveFrame();
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
OnPreUpdate();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnFirstActiveFrame() { }
|
|
|
|
private void FirstActiveFrame()
|
2023-11-24 17:03:21 +03:00
|
|
|
{
|
2024-09-30 12:18:13 +03:00
|
|
|
OnFirstActiveFrame();
|
|
|
|
isInitializedThisFrame = false;
|
2023-11-24 17:03:21 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:18:13 +03:00
|
|
|
protected virtual void OnUpdatePreActiveCheck() { }
|
|
|
|
protected virtual void OnUpdate() { }
|
|
|
|
private void Update(IBehaviourController _)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-09-30 12:18:13 +03:00
|
|
|
OnUpdatePreActiveCheck();
|
|
|
|
|
|
|
|
if (!IsActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OnUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void OnPreDrawPreActiveCheck() { }
|
|
|
|
protected virtual void OnPreDraw() { }
|
|
|
|
private void PreDraw(IBehaviourController _)
|
|
|
|
{
|
|
|
|
OnPreDrawPreActiveCheck();
|
|
|
|
|
|
|
|
if (!StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OnPreDraw();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
2024-10-22 22:02:38 +03:00
|
|
|
|
2024-11-03 20:13:25 +03:00
|
|
|
protected virtual void OnEnteredHierarchy(IGameManager gameManager) { }
|
|
|
|
private void EnteredHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnEnteredHierarchy(gameManager);
|
2024-10-22 22:02:38 +03:00
|
|
|
|
2024-11-03 20:13:25 +03:00
|
|
|
protected virtual void OnExitedHierarchy(IGameManager gameManager) { }
|
|
|
|
private void ExitedHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnExitedHierarchy(gameManager);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|