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(); - } -} diff --git a/Engine.Physics2D/Collider2DBehaviourBase.cs b/Engine.Physics2D/Collider2DBehaviourBase.cs index 64a69cd..c567f76 100644 --- a/Engine.Physics2D/Collider2DBehaviourBase.cs +++ b/Engine.Physics2D/Collider2DBehaviourBase.cs @@ -4,7 +4,7 @@ using Syntriax.Engine.Physics2D.Abstract; namespace Syntriax.Engine.Physics2D; -public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D +public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D { public event ICollider2D.OnCollisionDetectedDelegate? OnCollisionDetected = null; public event ICollider2D.OnCollisionResolvedDelegate? OnCollisionResolved = null; diff --git a/Engine.Physics2D/RigidBody2D.cs b/Engine.Physics2D/RigidBody2D.cs index 87f7409..08a6b02 100644 --- a/Engine.Physics2D/RigidBody2D.cs +++ b/Engine.Physics2D/RigidBody2D.cs @@ -4,7 +4,7 @@ using Syntriax.Engine.Physics2D.Abstract; namespace Syntriax.Engine.Physics2D; -public class RigidBody2D : BehaviourOverride, IRigidBody2D +public class RigidBody2D : Behaviour, IRigidBody2D { event IAssignableTransform.OnTransformAssignedDelegate? IAssignableTransform.OnTransformAssigned { add => GameObject.OnTransformAssigned += value; remove => GameObject.OnTransformAssigned -= value; }