From 02747c5150d595afca13334c7334d648c03d7a6c Mon Sep 17 00:00:00 2001 From: Syntriax Date: Mon, 30 Sep 2024 12:18:13 +0300 Subject: [PATCH] refactor: Renamed Behaviour to BehaviourBase & BehaviourOverride to Behaviour --- Engine.Core/Behaviour.cs | 108 +++++++++++++++++++------------ Engine.Core/BehaviourBase.cs | 59 +++++++++++++++++ Engine.Core/BehaviourOverride.cs | 87 ------------------------- 3 files changed, 127 insertions(+), 127 deletions(-) create mode 100644 Engine.Core/BehaviourBase.cs delete mode 100644 Engine.Core/BehaviourOverride.cs diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index 4bbe173..07523c8 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -1,59 +1,87 @@ using Syntriax.Engine.Core.Abstract; -using Syntriax.Engine.Core.Exceptions; namespace Syntriax.Engine.Core; -[System.Diagnostics.DebuggerDisplay("{GetType().Name, nq}, Priority: {Priority}, Initialized: {Initialized}")] -public abstract class Behaviour : BaseEntity, IBehaviour +public abstract class Behaviour : BehaviourBase { - public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null; + private bool isInitializedThisFrame = false; - public event IBehaviour.OnPriorityChangedDelegate? OnPriorityChanged = null; + protected IGameObject GameObject => BehaviourController.GameObject; + protected ITransform Transform => BehaviourController.GameObject.Transform; - - private IBehaviourController _behaviourController = null!; - - private int _priority = 0; - - - public IBehaviourController BehaviourController => _behaviourController; - - public override bool IsActive => base.IsActive && BehaviourController.GameObject.StateEnable.Enabled; - - public int Priority + public Behaviour() { - get => _priority; - set - { - if (value == _priority) - return; - - int previousPriority = _priority; - _priority = value; - OnPriorityChanged?.Invoke(this, previousPriority); - } + OnInitialized += OnInitialize; + OnFinalized += OnFinalize; + OnUnassigned += OnUnassign; } - public bool Assign(IBehaviourController behaviourController) - { - if (Initialized) - return false; + protected virtual void OnUnassign() { } + private void OnUnassign(IAssignable assignable) => OnUnassign(); - _behaviourController = behaviourController; - OnBehaviourControllerAssigned?.Invoke(this); - return true; + protected virtual void OnInitialize() { } + private void OnInitialize(IInitialize _) + { + isInitializedThisFrame = true; + + BehaviourController.OnPreUpdate += PreUpdate; + BehaviourController.OnPreDraw += PreDraw; + BehaviourController.OnUpdate += Update; + OnInitialize(); } - protected override void UnassignInternal() + protected virtual void OnFinalize() { } + private void OnFinalize(IInitialize _) { - base.UnassignInternal(); - _behaviourController = null!; + BehaviourController.OnPreUpdate -= PreUpdate; + BehaviourController.OnPreDraw -= PreDraw; + BehaviourController.OnUpdate -= Update; + OnFinalize(); } - protected override void InitializeInternal() + protected virtual void OnPreUpdatePreActiveCheck() { } + protected virtual void OnPreUpdate() { } + private void PreUpdate(IBehaviourController _) { - base.InitializeInternal(); - NotAssignedException.Check(this, _behaviourController); - NotAssignedException.Check(this, StateEnable); + OnPreUpdatePreActiveCheck(); + + if (!IsActive) + return; + + if (isInitializedThisFrame) + FirstActiveFrame(); + + OnPreUpdate(); + } + + protected virtual void OnFirstActiveFrame() { } + private void FirstActiveFrame() + { + OnFirstActiveFrame(); + isInitializedThisFrame = false; + } + + protected virtual void OnUpdatePreActiveCheck() { } + protected virtual void OnUpdate() { } + private void Update(IBehaviourController _) + { + OnUpdatePreActiveCheck(); + + if (!IsActive) + return; + + OnUpdate(); + } + + protected virtual void OnPreDrawPreActiveCheck() { } + protected virtual void OnPreDraw() { } + private void PreDraw(IBehaviourController _) + { + OnPreDrawPreActiveCheck(); + + if (!StateEnable.Enabled) + return; + + OnPreDraw(); } } diff --git a/Engine.Core/BehaviourBase.cs b/Engine.Core/BehaviourBase.cs new file mode 100644 index 0000000..04736b0 --- /dev/null +++ b/Engine.Core/BehaviourBase.cs @@ -0,0 +1,59 @@ +using Syntriax.Engine.Core.Abstract; +using Syntriax.Engine.Core.Exceptions; + +namespace Syntriax.Engine.Core; + +[System.Diagnostics.DebuggerDisplay("{GetType().Name, nq}, Priority: {Priority}, Initialized: {Initialized}")] +public abstract class BehaviourBase : BaseEntity, IBehaviour +{ + public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null; + + public event IBehaviour.OnPriorityChangedDelegate? OnPriorityChanged = null; + + + private IBehaviourController _behaviourController = null!; + + private int _priority = 0; + + + public IBehaviourController BehaviourController => _behaviourController; + + public override bool IsActive => base.IsActive && BehaviourController.GameObject.StateEnable.Enabled; + + public int Priority + { + get => _priority; + set + { + if (value == _priority) + return; + + int previousPriority = _priority; + _priority = value; + OnPriorityChanged?.Invoke(this, previousPriority); + } + } + + public bool Assign(IBehaviourController behaviourController) + { + if (Initialized) + return false; + + _behaviourController = behaviourController; + OnBehaviourControllerAssigned?.Invoke(this); + return true; + } + + protected override void UnassignInternal() + { + base.UnassignInternal(); + _behaviourController = null!; + } + + protected override void InitializeInternal() + { + base.InitializeInternal(); + NotAssignedException.Check(this, _behaviourController); + NotAssignedException.Check(this, StateEnable); + } +} diff --git a/Engine.Core/BehaviourOverride.cs b/Engine.Core/BehaviourOverride.cs deleted file mode 100644 index 6e8dcc9..0000000 --- a/Engine.Core/BehaviourOverride.cs +++ /dev/null @@ -1,87 +0,0 @@ -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 OnPreUpdatePreActiveCheck() { } - protected virtual void OnPreUpdate() { } - private void PreUpdate(IBehaviourController _) - { - OnPreUpdatePreActiveCheck(); - - if (!IsActive) - return; - - if (isInitializedThisFrame) - FirstActiveFrame(); - - OnPreUpdate(); - } - - protected virtual void OnFirstActiveFrame() { } - private void FirstActiveFrame() - { - OnFirstActiveFrame(); - isInitializedThisFrame = false; - } - - protected virtual void OnUpdatePreActiveCheck() { } - protected virtual void OnUpdate() { } - private void Update(IBehaviourController _) - { - OnUpdatePreActiveCheck(); - - if (!IsActive) - return; - - OnUpdate(); - } - - protected virtual void OnPreDrawPreActiveCheck() { } - protected virtual void OnPreDraw() { } - private void PreDraw(IBehaviourController _) - { - OnPreDrawPreActiveCheck(); - - if (!StateEnable.Enabled) - return; - - OnPreDraw(); - } -}