2023-11-23 22:07:49 +03:00
|
|
|
using System;
|
2024-01-30 18:54:12 +03:00
|
|
|
using System.Collections;
|
2023-11-23 22:07:49 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-01-24 18:40:12 +03:00
|
|
|
using System.Linq;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
2024-11-10 20:21:53 +03:00
|
|
|
using Syntriax.Engine.Core.Exceptions;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
2024-01-27 21:05:56 +03:00
|
|
|
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
|
2023-11-23 22:07:49 +03:00
|
|
|
public class BehaviourController : IBehaviourController
|
|
|
|
{
|
2024-07-15 01:13:39 +03:00
|
|
|
public event IBehaviourController.OnPreUpdateDelegate? OnPreUpdate = null;
|
|
|
|
public event IBehaviourController.OnUpdateDelegate? OnUpdate = null;
|
|
|
|
public event IBehaviourController.OnPreDrawDelegate? OnPreDraw = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
public event IBehaviourController.OnBehaviourAddedDelegate? OnBehaviourAdded = null;
|
|
|
|
public event IBehaviourController.OnBehaviourRemovedDelegate? OnBehaviourRemoved = null;
|
|
|
|
public event IAssignableGameObject.OnGameObjectAssignedDelegate? OnGameObjectAssigned = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-11-10 20:21:53 +03:00
|
|
|
public event IInitialize.OnInitializedDelegate? OnInitialized = null;
|
|
|
|
public event IInitialize.OnFinalizedDelegate? OnFinalized = null;
|
|
|
|
|
|
|
|
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
|
|
|
|
|
|
|
|
private IGameObject _gameObject = null!;
|
2024-11-10 20:21:53 +03:00
|
|
|
private bool _initialized = false;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
public IGameObject GameObject => _gameObject;
|
|
|
|
|
2024-11-10 20:21:53 +03:00
|
|
|
|
|
|
|
public bool IsInitialized
|
|
|
|
{
|
|
|
|
get => _initialized;
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (value == _initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_initialized = value;
|
|
|
|
if (value)
|
|
|
|
OnInitialized?.Invoke(this);
|
|
|
|
else
|
|
|
|
OnFinalized?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
|
|
|
|
{
|
|
|
|
InsertBehaviourByPriority(behaviour);
|
|
|
|
|
2024-09-30 19:55:14 +03:00
|
|
|
behaviour.Assign(this);
|
|
|
|
behaviour.Assign(GameObject.StateEnable);
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
behaviour.Initialize();
|
|
|
|
behaviour.OnPriorityChanged += OnPriorityChange;
|
|
|
|
OnBehaviourAdded?.Invoke(this, behaviour);
|
|
|
|
return behaviour;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T AddBehaviour<T>(params object?[]? args) where T : class, IBehaviour
|
|
|
|
=> AddBehaviour(new Factory.BehaviourFactory().Instantiate<T>(_gameObject, args));
|
|
|
|
|
2024-01-31 18:32:53 +03:00
|
|
|
public T? GetBehaviour<T>()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
foreach (var behaviourItem in behaviours)
|
2024-01-31 18:32:53 +03:00
|
|
|
if (behaviourItem is T result)
|
|
|
|
return result;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-01-31 18:32:53 +03:00
|
|
|
return default;
|
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-01-31 18:32:53 +03:00
|
|
|
public bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour)
|
|
|
|
{
|
|
|
|
behaviour = GetBehaviour<T>();
|
|
|
|
return behaviour is not null;
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2023-11-30 17:54:32 +03:00
|
|
|
public IList<T> GetBehaviours<T>()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-01-24 18:40:12 +03:00
|
|
|
List<T>? behaviours = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
foreach (var behaviourItem in this.behaviours)
|
|
|
|
{
|
|
|
|
if (behaviourItem is not T behaviour)
|
|
|
|
continue;
|
|
|
|
|
2024-01-24 18:40:12 +03:00
|
|
|
behaviours ??= [];
|
2023-11-23 22:07:49 +03:00
|
|
|
behaviours.Add(behaviour);
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:40:12 +03:00
|
|
|
return behaviours ?? Enumerable.Empty<T>().ToList();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 12:31:51 +03:00
|
|
|
public void GetBehaviours<T>(List<T> behaviors)
|
|
|
|
{
|
|
|
|
behaviors.Clear();
|
|
|
|
foreach (var behaviourItem in behaviours)
|
|
|
|
{
|
|
|
|
if (behaviourItem is not T _)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
behaviours.Add(behaviourItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 17:54:32 +03:00
|
|
|
public void RemoveBehaviour<T>(bool removeAll = false) where T : class, IBehaviour
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
for (int i = behaviours.Count; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (behaviours[i] is not T behaviour)
|
|
|
|
continue;
|
|
|
|
|
2023-11-30 17:54:32 +03:00
|
|
|
RemoveBehaviour(behaviour);
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
if (!removeAll)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 17:54:32 +03:00
|
|
|
public void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour
|
|
|
|
{
|
|
|
|
if (!behaviours.Contains(behaviour))
|
|
|
|
throw new Exception($"{behaviour.GetType().Name} does not exist in {GameObject.Name}'s {nameof(IBehaviourController)}.");
|
|
|
|
|
|
|
|
behaviour.OnPriorityChanged -= OnPriorityChange;
|
|
|
|
behaviour.Finalize();
|
|
|
|
behaviours.Remove(behaviour);
|
|
|
|
OnBehaviourRemoved?.Invoke(this, behaviour);
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
public bool Assign(IGameObject gameObject)
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (GameObject is not null && GameObject.IsInitialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_gameObject = gameObject;
|
|
|
|
OnGameObjectAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-10 20:21:53 +03:00
|
|
|
|
|
|
|
public bool Initialize()
|
|
|
|
{
|
|
|
|
if (IsInitialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NotAssignedException.Check(this, _gameObject);
|
|
|
|
|
|
|
|
foreach (IBehaviour behaviour in behaviours)
|
|
|
|
behaviour.Initialize();
|
|
|
|
|
|
|
|
IsInitialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Finalize()
|
|
|
|
{
|
|
|
|
if (!IsInitialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach (IBehaviour behaviour in behaviours)
|
|
|
|
behaviour.Finalize();
|
|
|
|
|
|
|
|
IsInitialized = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-24 17:03:21 +03:00
|
|
|
public bool Unassign()
|
|
|
|
{
|
2024-11-10 20:21:53 +03:00
|
|
|
if (IsInitialized)
|
2023-11-24 17:03:21 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_gameObject = null!;
|
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public void Update()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
if (!GameObject.StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreUpdate?.Invoke(this);
|
|
|
|
OnUpdate?.Invoke(this);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
2023-11-30 10:39:40 +03:00
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public void UpdatePreDraw()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
if (!GameObject.StateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnPreDraw?.Invoke(this);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public BehaviourController() { }
|
|
|
|
public BehaviourController(IGameObject gameObject) => Assign(gameObject);
|
|
|
|
|
|
|
|
private void InsertBehaviourByPriority<T>(T behaviour) where T : class, IBehaviour
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < behaviours.Count; i++)
|
|
|
|
{
|
|
|
|
if (behaviours[i].Priority > behaviour.Priority)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
behaviours.Insert(i, behaviour);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0 || i == behaviours.Count)
|
|
|
|
behaviours.Add(behaviour);
|
|
|
|
}
|
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
|
|
|
|
private void OnPriorityChange(IBehaviour sender, int previousPriority)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-07-15 01:13:39 +03:00
|
|
|
behaviours.Remove(sender);
|
|
|
|
InsertBehaviourByPriority(sender);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
2024-01-30 18:54:12 +03:00
|
|
|
|
|
|
|
public IEnumerator<IBehaviour> GetEnumerator() => behaviours.GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => behaviours.GetEnumerator();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|