feat: PreUpdate and FirstActiveFrame to Behaviour

This commit is contained in:
Syntriax 2023-11-30 10:39:40 +03:00
parent d2881e94df
commit bb6990a80c
3 changed files with 35 additions and 1 deletions

View File

@ -11,6 +11,10 @@ namespace Syntriax.Engine.Core.Abstract;
/// </summary>
public interface IBehaviourController : IAssignableGameObject
{
/// <summary>
/// Callback triggered when the <see cref="Update(GameTime)"/> is called but right before the <see cref="OnUpdate"/> action is triggered.
/// </summary>
Action<IBehaviourController, GameTime>? OnPreUpdate { get; set; }
/// <summary>
/// Callback triggered when the <see cref="Update(GameTime)"/> is called.
/// </summary>

View File

@ -10,6 +10,7 @@ namespace Syntriax.Engine.Core;
public class BehaviourController : IBehaviourController
{
public Action<IBehaviourController, GameTime>? OnPreUpdate { get; set; }
public Action<IBehaviourController, GameTime>? OnUpdate { get; set; } = null;
public Action<IBehaviourController, GameTime>? OnPreDraw { get; set; } = null;
@ -111,8 +112,10 @@ public class BehaviourController : IBehaviourController
if (!GameObject.StateEnable.Enabled)
return;
OnPreUpdate?.Invoke(this, gameTime);
OnUpdate?.Invoke(this, gameTime);
}
public void UpdatePreDraw(GameTime gameTime)
{
if (!GameObject.StateEnable.Enabled)

View File

@ -1,4 +1,3 @@
using System;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
@ -7,6 +6,8 @@ namespace Syntriax.Engine.Core;
public abstract class BehaviourOverride : Behaviour
{
private bool isInitializedThisFrame = false;
protected IGameObject GameObject => BehaviourController.GameObject;
protected ITransform Transform => BehaviourController.GameObject.Transform;
@ -23,6 +24,9 @@ public abstract class BehaviourOverride : Behaviour
protected virtual void OnInitialize() { }
private void OnInitialize(IInitialize _)
{
isInitializedThisFrame = true;
BehaviourController.OnPreUpdate += PreUpdate;
BehaviourController.OnPreDraw += PreDraw;
BehaviourController.OnUpdate += Update;
OnInitialize();
@ -31,11 +35,34 @@ public abstract class BehaviourOverride : Behaviour
protected virtual void OnFinalize() { }
private void OnFinalize(IInitialize _)
{
BehaviourController.OnPreUpdate -= PreUpdate;
BehaviourController.OnPreDraw -= PreDraw;
BehaviourController.OnUpdate -= Update;
OnFinalize();
}
protected virtual void OnPreUpdatePreEnabledCheck(GameTime time) { }
protected virtual void OnPreUpdate(GameTime time) { }
private void PreUpdate(IBehaviourController _, GameTime time)
{
OnPreUpdatePreEnabledCheck(time);
if (!StateEnable.Enabled)
return;
if (isInitializedThisFrame)
FirstActiveFrame(time);
OnPreUpdate(time);
}
protected virtual void OnFirstActiveFrame(GameTime time) { }
private void FirstActiveFrame(GameTime time)
{
OnFirstActiveFrame(time);
isInitializedThisFrame = false;
}
protected virtual void OnUpdatePreEnabledCheck(GameTime time) { }
protected virtual void OnUpdate(GameTime time) { }
private void Update(IBehaviourController _, GameTime time)