2023-11-23 22:07:49 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
public abstract class BehaviourOverride : Behaviour
|
|
|
|
{
|
2023-11-30 10:39:40 +03:00
|
|
|
private bool isInitializedThisFrame = false;
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
protected IGameObject GameObject => BehaviourController.GameObject;
|
|
|
|
protected ITransform Transform => BehaviourController.GameObject.Transform;
|
|
|
|
|
|
|
|
public BehaviourOverride()
|
|
|
|
{
|
|
|
|
OnInitialized += OnInitialize;
|
|
|
|
OnFinalized += OnFinalize;
|
2023-11-24 17:20:43 +03:00
|
|
|
OnUnassigned += OnUnassign;
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 17:20:43 +03:00
|
|
|
protected virtual void OnUnassign() { }
|
|
|
|
private void OnUnassign(IAssignable assignable) => OnUnassign();
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
protected virtual void OnInitialize() { }
|
|
|
|
private void OnInitialize(IInitialize _)
|
|
|
|
{
|
2023-11-30 10:39:40 +03:00
|
|
|
isInitializedThisFrame = true;
|
|
|
|
|
|
|
|
BehaviourController.OnPreUpdate += PreUpdate;
|
2023-11-23 22:07:49 +03:00
|
|
|
BehaviourController.OnPreDraw += PreDraw;
|
|
|
|
BehaviourController.OnUpdate += Update;
|
|
|
|
OnInitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void OnFinalize() { }
|
|
|
|
private void OnFinalize(IInitialize _)
|
|
|
|
{
|
2023-11-30 10:39:40 +03:00
|
|
|
BehaviourController.OnPreUpdate -= PreUpdate;
|
2023-11-23 22:07:49 +03:00
|
|
|
BehaviourController.OnPreDraw -= PreDraw;
|
|
|
|
BehaviourController.OnUpdate -= Update;
|
|
|
|
OnFinalize();
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
protected virtual void OnPreUpdatePreEnabledCheck() { }
|
|
|
|
protected virtual void OnPreUpdate() { }
|
|
|
|
private void PreUpdate(IBehaviourController _)
|
2023-11-30 10:39:40 +03:00
|
|
|
{
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreUpdatePreEnabledCheck();
|
2023-11-30 10:39:40 +03:00
|
|
|
|
|
|
|
if (!StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isInitializedThisFrame)
|
2024-01-22 22:45:40 +03:00
|
|
|
FirstActiveFrame();
|
2023-11-30 10:39:40 +03:00
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreUpdate();
|
2023-11-30 10:39:40 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
protected virtual void OnFirstActiveFrame() { }
|
|
|
|
private void FirstActiveFrame()
|
2023-11-30 10:39:40 +03:00
|
|
|
{
|
2024-01-22 22:45:40 +03:00
|
|
|
OnFirstActiveFrame();
|
2023-11-30 10:39:40 +03:00
|
|
|
isInitializedThisFrame = false;
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
protected virtual void OnUpdatePreEnabledCheck() { }
|
|
|
|
protected virtual void OnUpdate() { }
|
|
|
|
private void Update(IBehaviourController _)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-01-22 22:45:40 +03:00
|
|
|
OnUpdatePreEnabledCheck();
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
if (!StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnUpdate();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
protected virtual void OnPreDrawPreEnabledCheck() { }
|
|
|
|
protected virtual void OnPreDraw() { }
|
|
|
|
private void PreDraw(IBehaviourController _)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreDrawPreEnabledCheck();
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
if (!StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreDraw();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
}
|