refactor: IBehaviourController is now an IEntity as well
This commit is contained in:
parent
0184d1758c
commit
d92d16cfad
@ -5,7 +5,7 @@ namespace Syntriax.Engine.Core;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a controller for managing <see cref="IBehaviour"/>s and notify them accordingly about the engine's updates. Connected to an <see cref="IUniverseObject"/>.
|
/// Represents a controller for managing <see cref="IBehaviour"/>s and notify them accordingly about the engine's updates. Connected to an <see cref="IUniverseObject"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IBehaviourController : IInitializable, IHasUniverseObject, IEnumerable<IBehaviour>
|
public interface IBehaviourController : IEntity, IHasUniverseObject, IEnumerable<IBehaviour>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event triggered before the update of <see cref="IBehaviour"/>s.
|
/// Event triggered before the update of <see cref="IBehaviour"/>s.
|
||||||
|
@ -6,7 +6,7 @@ using System.Linq;
|
|||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
|
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
|
||||||
public class BehaviourController : IBehaviourController
|
public class BehaviourController : BaseEntity, IBehaviourController
|
||||||
{
|
{
|
||||||
public event IBehaviourController.PreUpdateEventHandler? OnPreUpdate = null;
|
public event IBehaviourController.PreUpdateEventHandler? OnPreUpdate = null;
|
||||||
public event IBehaviourController.UpdateEventHandler? OnUpdate = null;
|
public event IBehaviourController.UpdateEventHandler? OnUpdate = null;
|
||||||
@ -16,34 +16,12 @@ public class BehaviourController : IBehaviourController
|
|||||||
public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
|
public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
|
||||||
public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
|
public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
|
||||||
|
|
||||||
public event IInitializable.InitializedEventHandler? OnInitialized = null;
|
|
||||||
public event IInitializable.FinalizedEventHandler? OnFinalized = null;
|
|
||||||
|
|
||||||
public event IAssignable.UnassignEventHandler? OnUnassigned = null;
|
|
||||||
|
|
||||||
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
|
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
|
||||||
|
|
||||||
private IUniverseObject _universeObject = null!;
|
private IUniverseObject _universeObject = null!;
|
||||||
private bool _initialized = false;
|
|
||||||
|
|
||||||
public IUniverseObject UniverseObject => _universeObject;
|
public IUniverseObject UniverseObject => _universeObject;
|
||||||
|
|
||||||
public bool IsInitialized
|
|
||||||
{
|
|
||||||
get => _initialized;
|
|
||||||
private set
|
|
||||||
{
|
|
||||||
if (value == _initialized)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_initialized = value;
|
|
||||||
if (value)
|
|
||||||
OnInitialized?.InvokeSafe(this);
|
|
||||||
else
|
|
||||||
OnFinalized?.InvokeSafe(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
|
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
|
||||||
{
|
{
|
||||||
InsertBehaviourByPriority(behaviour);
|
InsertBehaviourByPriority(behaviour);
|
||||||
@ -133,40 +111,18 @@ public class BehaviourController : IBehaviourController
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Initialize()
|
protected override void InitializeInternal()
|
||||||
{
|
{
|
||||||
if (IsInitialized)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Debug.AssertHelpers.AssertUniverseObjectAssigned(this);
|
Debug.AssertHelpers.AssertUniverseObjectAssigned(this);
|
||||||
|
|
||||||
foreach (IBehaviour behaviour in behaviours)
|
foreach (IBehaviour behaviour in behaviours)
|
||||||
behaviour.Initialize();
|
behaviour.Initialize();
|
||||||
|
|
||||||
IsInitialized = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Finalize()
|
protected override void FinalizeInternal()
|
||||||
{
|
{
|
||||||
if (!IsInitialized)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
foreach (IBehaviour behaviour in behaviours)
|
foreach (IBehaviour behaviour in behaviours)
|
||||||
behaviour.Finalize();
|
behaviour.Finalize();
|
||||||
|
|
||||||
IsInitialized = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Unassign()
|
|
||||||
{
|
|
||||||
if (IsInitialized)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
_universeObject = null!;
|
|
||||||
OnUnassigned?.InvokeSafe(this);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user