using Syntriax.Engine.Core.Abstract; namespace Syntriax.Engine.Core; public abstract class Behaviour : BehaviourBase { private bool isInitializedThisFrame = false; protected IGameObject GameObject => BehaviourController.GameObject; protected ITransform Transform => BehaviourController.GameObject.Transform; public Behaviour() { OnInitialized += OnInitialize; OnFinalized += OnFinalize; OnUnassigned += OnUnassign; } protected virtual void OnUnassign() { } private void OnUnassign(IAssignable assignable) => OnUnassign(); protected virtual void OnInitialize() { } private void OnInitialize(IInitialize _) { isInitializedThisFrame = true; BehaviourController.OnPreUpdate += PreUpdate; BehaviourController.OnPreDraw += PreDraw; BehaviourController.OnUpdate += Update; BehaviourController.GameObject.OnEnteredHierarchy += EnteredHierarchy; BehaviourController.GameObject.OnExitedHierarchy += ExitedHierarchy; OnInitialize(); if (GameObject.IsInHierarchy) EnteredHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error")); } protected virtual void OnFinalize() { } private void OnFinalize(IInitialize _) { BehaviourController.OnPreUpdate -= PreUpdate; BehaviourController.OnPreDraw -= PreDraw; BehaviourController.OnUpdate -= Update; BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy; BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy; OnFinalize(); if (GameObject.IsInHierarchy) ExitedHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error")); } protected virtual void OnPreUpdatePreActiveCheck() { } protected virtual void OnPreUpdate() { } private void PreUpdate(IBehaviourController _) { OnPreUpdatePreActiveCheck(); if (!IsActive) return; if (isInitializedThisFrame) FirstActiveFrame(); OnPreUpdate(); } protected virtual void OnFirstActiveFrame() { } private void FirstActiveFrame() { OnFirstActiveFrame(); isInitializedThisFrame = false; } protected virtual void OnUpdatePreActiveCheck() { } protected virtual void OnUpdate() { } private void Update(IBehaviourController _) { OnUpdatePreActiveCheck(); if (!IsActive) return; OnUpdate(); } protected virtual void OnPreDrawPreActiveCheck() { } protected virtual void OnPreDraw() { } private void PreDraw(IBehaviourController _) { OnPreDrawPreActiveCheck(); if (!StateEnable.Enabled) return; OnPreDraw(); } 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); }