refactor: Renamed Behaviour to BehaviourBase & BehaviourOverride to Behaviour
This commit is contained in:
parent
c51eda49bf
commit
e7c80871fe
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue