2023-11-24 16:58:07 +03:00
|
|
|
using System;
|
|
|
|
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;
|
|
|
|
|
2023-11-24 17:04:19 +03:00
|
|
|
public class GameManager : IEntity
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2023-11-27 13:46:01 +03:00
|
|
|
public Action<GameManager>? OnCameraChanged { 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;
|
|
|
|
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
private IList<IGameObject> _gameObjects = new List<IGameObject>(Constants.GAME_OBJECTS_SIZE_INITIAL);
|
|
|
|
|
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;
|
2023-11-23 22:07:49 +03:00
|
|
|
public IList<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
|
|
|
|
|
|
|
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);
|
|
|
|
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-22 22:45:40 +03:00
|
|
|
public void Update()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
private void Unregister(IGameObject gameObject)
|
|
|
|
{
|
|
|
|
gameObject.OnFinalized -= OnGameObjectFinalize;
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
_gameObjects.Remove(gameObject);
|
|
|
|
}
|
2023-11-24 11:42:51 +03:00
|
|
|
|
|
|
|
private void Register(IGameObject gameObject)
|
|
|
|
{
|
|
|
|
gameObject.OnFinalized += OnGameObjectFinalize;
|
|
|
|
|
|
|
|
_gameObjects.Add(gameObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnGameObjectFinalize(IInitialize initialize)
|
|
|
|
{
|
|
|
|
if (initialize is IGameObject gameObject)
|
|
|
|
Unregister(gameObject);
|
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|