Compare commits
3 Commits
4000e761a7
...
07666359f2
Author | SHA1 | Date | |
---|---|---|---|
07666359f2 | |||
514e5b5762 | |||
1438b19e35 |
26
Engine.Core/Abstract/Assignable/IAssignableGameManager.cs
Normal file
26
Engine.Core/Abstract/Assignable/IAssignableGameManager.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="ITransform"/> field.
|
||||
/// </summary>
|
||||
public interface IAssignableGameManager : IAssignable
|
||||
{
|
||||
/// <summary>
|
||||
/// Callback triggered when the <see cref="IGameManager"/> value has has been assigned a new value.
|
||||
/// </summary>
|
||||
Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IGameManager" />
|
||||
IGameManager GameManager { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Assign a value to the <see cref="IGameManager"/> field of this object
|
||||
/// </summary>
|
||||
/// <param name="gameManager">New <see cref="IGameManager"/> to assign.</param>
|
||||
/// <returns>
|
||||
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
|
||||
/// </returns>
|
||||
bool Assign(IGameManager gameManager);
|
||||
}
|
@@ -65,6 +65,10 @@ public interface IBehaviourController : IAssignableGameObject
|
||||
/// <returns>Returns a list of all the matching <see cref="IBehaviour"/>s found in the <see cref="IBehaviourController"/>.</returns>
|
||||
IList<T> GetBehaviours<T>();
|
||||
|
||||
/// <typeparam name="T">An implemented class or <see cref="interface"/>.</typeparam>
|
||||
/// <returns>Fills the provided list parameter all the matching <see cref="IBehaviour"/>s found in the <see cref="IBehaviourController"/>.</returns>
|
||||
void GetBehaviours<T>(List<T> behaviours);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the <see cref="IBehaviour"/> found in the <see cref="IBehaviourController"/>.
|
||||
/// </summary>
|
||||
|
@@ -2,7 +2,7 @@ using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
public interface IGameObject : IEntity, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
|
||||
public interface IGameObject : IEntity, IAssignableGameManager, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
|
||||
{
|
||||
Action<IGameObject>? OnUpdated { get; set; }
|
||||
|
||||
|
@@ -70,6 +70,18 @@ public class BehaviourController : IBehaviourController
|
||||
return behaviours ?? Enumerable.Empty<T>().ToList();
|
||||
}
|
||||
|
||||
public void GetBehaviours<T>(List<T> behaviors)
|
||||
{
|
||||
behaviors.Clear();
|
||||
foreach (var behaviourItem in behaviours)
|
||||
{
|
||||
if (behaviourItem is not T _)
|
||||
continue;
|
||||
|
||||
behaviours.Add(behaviourItem);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveBehaviour<T>(bool removeAll = false) where T : class, IBehaviour
|
||||
{
|
||||
for (int i = behaviours.Count; i >= 0; i--)
|
||||
|
32
Engine.Core/Extensions/BehaviourExtensions.cs
Normal file
32
Engine.Core/Extensions/BehaviourExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public static class BehaviourExtensions
|
||||
{
|
||||
public static bool TryFindBehaviour<T>(this IEnumerable<IGameObject> gameObjects, [NotNullWhen(returnValue: true)] out T? behaviour)
|
||||
{
|
||||
behaviour = default;
|
||||
|
||||
foreach (IGameObject gameObject in gameObjects)
|
||||
if (gameObject.BehaviourController.TryGetBehaviour(out behaviour))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void FindBehaviours<T>(this IEnumerable<IGameObject> gameObjects, List<T> behaviours)
|
||||
{
|
||||
behaviours.Clear();
|
||||
List<T> cache = [];
|
||||
|
||||
foreach (IGameObject gameObject in gameObjects)
|
||||
{
|
||||
gameObject.BehaviourController.GetBehaviours(cache);
|
||||
behaviours.AddRange(cache);
|
||||
}
|
||||
}
|
||||
}
|
@@ -143,6 +143,8 @@ public class GameManager : IGameManager
|
||||
|
||||
private void Register(IGameObject gameObject)
|
||||
{
|
||||
gameObject.Assign(this);
|
||||
|
||||
gameObject.OnFinalized += OnGameObjectFinalize;
|
||||
|
||||
_gameObjects.Add(gameObject);
|
||||
|
@@ -12,6 +12,7 @@ public class GameObject : IGameObject
|
||||
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
|
||||
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
||||
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
||||
|
||||
public Action<IEntity>? OnNameChanged { get; set; } = null;
|
||||
|
||||
@@ -24,6 +25,7 @@ public class GameObject : IGameObject
|
||||
private ITransform _transform = null!;
|
||||
private IBehaviourController _behaviourController = null!;
|
||||
private IStateEnable _stateEnable = null!;
|
||||
private IGameManager _gameManager = null!;
|
||||
|
||||
private string _name = nameof(GameObject);
|
||||
private bool _initialized = false;
|
||||
@@ -31,6 +33,7 @@ public class GameObject : IGameObject
|
||||
public ITransform Transform => _transform;
|
||||
public IBehaviourController BehaviourController => _behaviourController;
|
||||
public IStateEnable StateEnable => _stateEnable;
|
||||
public IGameManager GameManager => _gameManager;
|
||||
|
||||
public bool Initialized
|
||||
{
|
||||
@@ -68,6 +71,7 @@ public class GameObject : IGameObject
|
||||
NotAssignedException.Check(this, _transform);
|
||||
NotAssignedException.Check(this, _behaviourController);
|
||||
NotAssignedException.Check(this, _stateEnable);
|
||||
NotAssignedException.Check(this, _gameManager);
|
||||
|
||||
Initialized = true;
|
||||
return true;
|
||||
@@ -126,6 +130,16 @@ public class GameObject : IGameObject
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Assign(IGameManager gameManager)
|
||||
{
|
||||
if (Initialized)
|
||||
return false;
|
||||
|
||||
_gameManager = gameManager;
|
||||
OnGameManagerAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Unassign()
|
||||
{
|
||||
if (Initialized)
|
||||
@@ -134,6 +148,7 @@ public class GameObject : IGameObject
|
||||
_stateEnable = null!;
|
||||
_transform = null!;
|
||||
_behaviourController = null!;
|
||||
_gameManager = null!;
|
||||
|
||||
OnUnassigned?.Invoke(this);
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user