Syntriax.Engine/Engine.Core/Behaviour.cs

98 lines
2.7 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 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();
}
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();
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() { }
private void EnteredHierarchy(IHierarchyObject sender) => OnEnteredHierarchy();
protected virtual void OnExitedHierarchy() { }
private void ExitedHierarchy(IHierarchyObject sender) => OnExitedHierarchy();
2023-11-23 22:07:49 +03:00
}