2023-11-24 16:58:07 +03:00
|
|
|
using System;
|
2024-01-23 12:06:14 +03:00
|
|
|
using System.Collections;
|
2023-11-24 16:58:07 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
2023-11-24 16:37:09 +03:00
|
|
|
using Syntriax.Engine.Core.Exceptions;
|
2023-11-23 22:07:49 +03:00
|
|
|
using Syntriax.Engine.Core.Factory;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
2024-01-27 21:05:56 +03:00
|
|
|
[System.Diagnostics.DebuggerDisplay("GameObject Count: {_gameObjects.Count}")]
|
2024-01-30 12:08:21 +03:00
|
|
|
public class GameManager : IGameManager
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2023-11-27 13:46:01 +03:00
|
|
|
public Action<GameManager>? OnCameraChanged { get; set; } = null;
|
2024-01-23 12:08:50 +03:00
|
|
|
public Action<GameManager, IGameObject>? OnGameObjectRegistered { get; set; } = null;
|
|
|
|
public Action<GameManager, IGameObject>? OnGameObjectUnRegistered { get; set; } = null;
|
|
|
|
|
2023-11-24 11:52:14 +03:00
|
|
|
public Action<IInitialize>? OnInitialized { get; set; } = null;
|
|
|
|
public Action<IInitialize>? OnFinalized { get; set; } = null;
|
2023-11-24 17:03:21 +03:00
|
|
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
2023-11-24 11:52:14 +03:00
|
|
|
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
|
|
|
|
|
|
|
|
2024-01-30 12:08:21 +03:00
|
|
|
private readonly List<IGameObject> _gameObjects = new(Constants.GAME_OBJECTS_SIZE_INITIAL);
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2023-11-24 11:52:14 +03:00
|
|
|
private IStateEnable _stateEnable = null!;
|
2023-11-23 22:07:49 +03:00
|
|
|
private GameObjectFactory _gameObjectFactory = null!;
|
2023-11-24 11:52:14 +03:00
|
|
|
private bool _initialized = false;
|
2023-11-27 13:46:01 +03:00
|
|
|
private ICamera _camera = null!;
|
2023-11-24 11:52:14 +03:00
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
private GameObjectFactory GameObjectFactory
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_gameObjectFactory is null)
|
|
|
|
_gameObjectFactory = new GameObjectFactory();
|
|
|
|
return _gameObjectFactory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:52:14 +03:00
|
|
|
public bool Initialized => _initialized;
|
2024-01-30 12:08:21 +03:00
|
|
|
public IReadOnlyList<IGameObject> GameObjects => _gameObjects;
|
|
|
|
|
2023-11-27 11:11:58 +03:00
|
|
|
public IStateEnable StateEnable
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_stateEnable is null)
|
|
|
|
{
|
|
|
|
Assign(new StateEnableFactory().Instantiate(this));
|
|
|
|
if (_stateEnable is null)
|
|
|
|
throw NotAssignedException.From(this, _stateEnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _stateEnable;
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2023-11-27 13:46:01 +03:00
|
|
|
public ICamera Camera
|
|
|
|
{
|
|
|
|
get => _camera;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_camera == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_camera = value;
|
|
|
|
OnCameraChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
public void RegisterGameObject(IGameObject gameObject)
|
|
|
|
{
|
|
|
|
if (_gameObjects.Contains(gameObject))
|
2023-11-27 11:06:02 +03:00
|
|
|
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2023-11-24 11:42:51 +03:00
|
|
|
Register(gameObject);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 09:45:10 +03:00
|
|
|
public T InstantiateGameObject<T>(params object?[]? args) where T : class, IGameObject
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2023-11-24 09:45:10 +03:00
|
|
|
T gameObject = GameObjectFactory.Instantiate<T>(args);
|
2023-11-24 11:42:51 +03:00
|
|
|
Register(gameObject);
|
2023-11-23 22:07:49 +03:00
|
|
|
return gameObject;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:42:51 +03:00
|
|
|
public IGameObject RemoveGameObject(IGameObject gameObject)
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
if (!_gameObjects.Contains(gameObject))
|
2023-11-27 11:06:02 +03:00
|
|
|
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2023-11-24 11:42:51 +03:00
|
|
|
Unregister(gameObject);
|
|
|
|
return gameObject;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:52:14 +03:00
|
|
|
public bool Initialize()
|
2023-11-24 11:42:51 +03:00
|
|
|
{
|
2023-11-24 11:52:14 +03:00
|
|
|
if (Initialized)
|
|
|
|
return false;
|
|
|
|
|
2023-11-24 16:37:09 +03:00
|
|
|
NotAssignedException.Check(this, StateEnable);
|
|
|
|
|
2023-11-24 11:42:51 +03:00
|
|
|
foreach (var gameObject in GameObjects)
|
|
|
|
gameObject.Initialize();
|
2023-11-24 11:52:14 +03:00
|
|
|
|
2024-01-30 12:08:21 +03:00
|
|
|
_initialized = true;
|
2023-11-24 11:52:14 +03:00
|
|
|
OnInitialized?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Finalize()
|
|
|
|
{
|
|
|
|
if (!Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = GameObjects.Count; i >= 0; i--)
|
|
|
|
GameObjects[i].Finalize();
|
|
|
|
|
|
|
|
OnFinalized?.Invoke(this);
|
2024-01-30 12:08:21 +03:00
|
|
|
_initialized = false;
|
2023-11-24 11:52:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IStateEnable stateEnable)
|
|
|
|
{
|
2023-11-24 16:37:09 +03:00
|
|
|
if (Initialized)
|
2023-11-24 11:52:14 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_stateEnable = stateEnable;
|
|
|
|
OnStateEnableAssigned?.Invoke(this);
|
|
|
|
return true;
|
2023-11-24 11:42:51 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 17:03:21 +03:00
|
|
|
public bool Unassign()
|
|
|
|
{
|
|
|
|
if (Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_stateEnable = null!;
|
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-23 12:06:21 +03:00
|
|
|
public void Update(EngineTime time)
|
2023-11-24 11:42:51 +03:00
|
|
|
{
|
2024-01-23 12:06:21 +03:00
|
|
|
Time.SetTime(time);
|
2023-11-24 11:42:51 +03:00
|
|
|
foreach (var gameObject in GameObjects)
|
2024-01-22 22:45:40 +03:00
|
|
|
gameObject.BehaviourController.Update();
|
2023-11-24 11:42:51 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public void PreDraw()
|
2023-11-24 11:42:51 +03:00
|
|
|
{
|
|
|
|
foreach (var gameObject in GameObjects)
|
2024-01-22 22:45:40 +03:00
|
|
|
gameObject.BehaviourController.UpdatePreDraw();
|
2023-11-24 11:42:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
2024-01-23 12:08:50 +03:00
|
|
|
private void Register(IGameObject gameObject)
|
2023-11-24 11:42:51 +03:00
|
|
|
{
|
2024-01-23 12:08:50 +03:00
|
|
|
gameObject.OnFinalized += OnGameObjectFinalize;
|
2023-11-24 11:42:51 +03:00
|
|
|
|
2024-01-23 12:08:50 +03:00
|
|
|
_gameObjects.Add(gameObject);
|
|
|
|
OnGameObjectRegistered?.Invoke(this, gameObject);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
2023-11-24 11:42:51 +03:00
|
|
|
|
2024-01-23 12:08:50 +03:00
|
|
|
private void Unregister(IGameObject gameObject)
|
2023-11-24 11:42:51 +03:00
|
|
|
{
|
2024-01-23 12:08:50 +03:00
|
|
|
gameObject.OnFinalized -= OnGameObjectFinalize;
|
2023-11-24 11:42:51 +03:00
|
|
|
|
2024-01-23 12:08:50 +03:00
|
|
|
_gameObjects.Remove(gameObject);
|
|
|
|
OnGameObjectUnRegistered?.Invoke(this, gameObject);
|
2023-11-24 11:42:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnGameObjectFinalize(IInitialize initialize)
|
|
|
|
{
|
|
|
|
if (initialize is IGameObject gameObject)
|
|
|
|
Unregister(gameObject);
|
|
|
|
}
|
2024-01-23 12:06:14 +03:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public IEnumerator<IGameObject> GetEnumerator() => _gameObjects.GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => _gameObjects.GetEnumerator();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|