chore: Added Initial Engine Code
This commit is contained in:
138
Engine.Core/BehaviourController.cs
Normal file
138
Engine.Core/BehaviourController.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public class BehaviourController : IBehaviourController
|
||||
{
|
||||
public Action<IBehaviourController, GameTime>? OnUpdate { get; set; } = null;
|
||||
public Action<IBehaviourController, GameTime>? OnPreDraw { get; set; } = null;
|
||||
|
||||
public Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; } = null;
|
||||
public Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; } = null;
|
||||
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
|
||||
|
||||
|
||||
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
|
||||
|
||||
private IGameObject _gameObject = null!;
|
||||
|
||||
|
||||
public IGameObject GameObject => _gameObject;
|
||||
|
||||
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
|
||||
{
|
||||
InsertBehaviourByPriority(behaviour);
|
||||
|
||||
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));
|
||||
|
||||
public bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour)
|
||||
{
|
||||
foreach (var behaviourItem in behaviours)
|
||||
{
|
||||
if (behaviourItem is not T result)
|
||||
continue;
|
||||
|
||||
behaviour = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
behaviour = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IList<T> GetBehaviours<T>() where T : IBehaviour
|
||||
{
|
||||
IList<T> behaviours = new List<T>();
|
||||
foreach (var behaviourItem in this.behaviours)
|
||||
{
|
||||
if (behaviourItem is not T behaviour)
|
||||
continue;
|
||||
|
||||
behaviours ??= new List<T>();
|
||||
behaviours.Add(behaviour);
|
||||
}
|
||||
|
||||
return behaviours;
|
||||
}
|
||||
|
||||
public void RemoveBehaviour<T>(bool removeAll = false) where T : IBehaviour
|
||||
{
|
||||
for (int i = behaviours.Count; i >= 0; i--)
|
||||
{
|
||||
if (behaviours[i] is not T behaviour)
|
||||
continue;
|
||||
|
||||
behaviour.OnPriorityChanged -= OnPriorityChange;
|
||||
behaviour.Finalize();
|
||||
behaviours.RemoveAt(i);
|
||||
OnBehaviourRemoved?.Invoke(this, behaviour);
|
||||
|
||||
if (!removeAll)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Assign(IGameObject gameObject)
|
||||
{
|
||||
if (_gameObject is not null)
|
||||
return false;
|
||||
|
||||
_gameObject = gameObject;
|
||||
OnGameObjectAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
if (!GameObject.StateEnable.Enabled)
|
||||
return;
|
||||
|
||||
OnUpdate?.Invoke(this, gameTime);
|
||||
}
|
||||
public void UpdatePreDraw(GameTime gameTime)
|
||||
{
|
||||
if (!GameObject.StateEnable.Enabled)
|
||||
return;
|
||||
|
||||
OnPreDraw?.Invoke(this, gameTime);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void OnPriorityChange(IBehaviour behaviour)
|
||||
{
|
||||
behaviours.Remove(behaviour);
|
||||
InsertBehaviourByPriority(behaviour);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user