Syntriax.Engine/Engine.Core/Behaviour.cs

107 lines
3.1 KiB
C#
Raw Normal View History

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