Syntriax.Engine/Engine.Core/BehaviourOverride.cs

88 lines
2.2 KiB
C#

using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public abstract class BehaviourOverride : Behaviour
{
private bool isInitializedThisFrame = false;
protected IGameObject GameObject => BehaviourController.GameObject;
protected ITransform Transform => BehaviourController.GameObject.Transform;
public BehaviourOverride()
{
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;
OnInitialize();
}
protected virtual void OnFinalize() { }
private void OnFinalize(IInitialize _)
{
BehaviourController.OnPreUpdate -= PreUpdate;
BehaviourController.OnPreDraw -= PreDraw;
BehaviourController.OnUpdate -= Update;
OnFinalize();
}
protected virtual void OnPreUpdatePreEnabledCheck() { }
protected virtual void OnPreUpdate() { }
private void PreUpdate(IBehaviourController _)
{
OnPreUpdatePreEnabledCheck();
if (!StateEnable.Enabled)
return;
if (isInitializedThisFrame)
FirstActiveFrame();
OnPreUpdate();
}
protected virtual void OnFirstActiveFrame() { }
private void FirstActiveFrame()
{
OnFirstActiveFrame();
isInitializedThisFrame = false;
}
protected virtual void OnUpdatePreEnabledCheck() { }
protected virtual void OnUpdate() { }
private void Update(IBehaviourController _)
{
OnUpdatePreEnabledCheck();
if (!StateEnable.Enabled)
return;
OnUpdate();
}
protected virtual void OnPreDrawPreEnabledCheck() { }
protected virtual void OnPreDraw() { }
private void PreDraw(IBehaviourController _)
{
OnPreDrawPreEnabledCheck();
if (!StateEnable.Enabled)
return;
OnPreDraw();
}
}