Syntriax.Engine/Engine.Core/BehaviourOverride.cs

88 lines
2.2 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 BehaviourOverride : Behaviour
{
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 _)
{
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 _)
{
BehaviourController.OnPreUpdate -= PreUpdate;
2023-11-23 22:07:49 +03:00
BehaviourController.OnPreDraw -= PreDraw;
BehaviourController.OnUpdate -= Update;
OnFinalize();
}
2024-01-28 14:56:44 +03:00
protected virtual void OnPreUpdatePreActiveCheck() { }
protected virtual void OnPreUpdate() { }
private void PreUpdate(IBehaviourController _)
{
2024-01-28 14:56:44 +03:00
OnPreUpdatePreActiveCheck();
2024-01-28 14:56:44 +03:00
if (!IsActive)
return;
if (isInitializedThisFrame)
FirstActiveFrame();
OnPreUpdate();
}
protected virtual void OnFirstActiveFrame() { }
private void FirstActiveFrame()
{
OnFirstActiveFrame();
isInitializedThisFrame = false;
}
2024-01-28 14:56:44 +03:00
protected virtual void OnUpdatePreActiveCheck() { }
protected virtual void OnUpdate() { }
private void Update(IBehaviourController _)
2023-11-23 22:07:49 +03:00
{
2024-01-28 14:56:44 +03:00
OnUpdatePreActiveCheck();
2023-11-23 22:07:49 +03:00
2024-01-28 14:56:44 +03:00
if (!IsActive)
2023-11-23 22:07:49 +03:00
return;
OnUpdate();
2023-11-23 22:07:49 +03:00
}
2024-01-28 14:56:44 +03:00
protected virtual void OnPreDrawPreActiveCheck() { }
protected virtual void OnPreDraw() { }
private void PreDraw(IBehaviourController _)
2023-11-23 22:07:49 +03:00
{
2024-01-28 14:56:44 +03:00
OnPreDrawPreActiveCheck();
2023-11-23 22:07:49 +03:00
if (!StateEnable.Enabled)
return;
OnPreDraw();
2023-11-23 22:07:49 +03:00
}
}