Syntriax.Engine/Engine.Core/GameManager.cs

133 lines
3.8 KiB
C#
Raw Normal View History

2023-11-24 16:58:07 +03:00
using System;
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;
using Syntriax.Engine.Core.Exceptions;
2023-11-23 22:07:49 +03:00
using Syntriax.Engine.Core.Factory;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("GameObject Count: {_gameObjects.Count}")]
2024-02-02 12:11:51 +03:00
public class GameManager : BaseEntity, IGameManager
2023-11-23 22:07:49 +03:00
{
2024-01-30 18:52:31 +03:00
public Action<IGameManager, IGameObject>? OnGameObjectRegistered { get; set; } = null;
public Action<IGameManager, IGameObject>? OnGameObjectUnRegistered { 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
private GameObjectFactory _gameObjectFactory = null!;
2023-11-23 22:07:49 +03:00
private GameObjectFactory GameObjectFactory
{
get
{
if (_gameObjectFactory is null)
_gameObjectFactory = new GameObjectFactory();
return _gameObjectFactory;
}
}
2024-01-30 12:08:21 +03:00
public IReadOnlyList<IGameObject> GameObjects => _gameObjects;
2024-02-02 12:11:51 +03:00
public override IStateEnable StateEnable
{
get
{
2024-02-02 12:11:51 +03:00
if (base.StateEnable is null)
{
Assign(new StateEnableFactory().Instantiate(this));
2024-02-02 12:11:51 +03:00
if (base.StateEnable is null)
throw NotAssignedException.From(this, base.StateEnable);
}
2024-02-02 12:11:51 +03:00
return base.StateEnable;
}
}
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
Register(gameObject);
2023-11-23 22:07:49 +03:00
}
public T InstantiateGameObject<T>(params object?[]? args) where T : class, IGameObject
2023-11-23 22:07:49 +03:00
{
T gameObject = GameObjectFactory.Instantiate<T>(args);
Register(gameObject);
2023-11-23 22:07:49 +03:00
return gameObject;
}
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
Unregister(gameObject);
return gameObject;
}
2024-02-02 12:11:51 +03:00
protected override void InitializeInternal()
{
2024-02-02 12:11:51 +03:00
base.InitializeInternal();
NotAssignedException.Check(this, StateEnable);
foreach (var gameObject in GameObjects)
gameObject.Initialize();
}
2024-02-02 12:11:51 +03:00
protected override void FinalizeInternal()
{
2024-02-02 12:11:51 +03:00
base.FinalizeInternal();
for (int i = GameObjects.Count; i >= 0; i--)
GameObjects[i].Finalize();
2023-11-24 17:03:21 +03:00
}
public void Update(EngineTime time)
{
Time.SetTime(time);
foreach (var gameObject in GameObjects)
gameObject.BehaviourController.Update();
}
public void PreDraw()
{
foreach (var gameObject in GameObjects)
gameObject.BehaviourController.UpdatePreDraw();
}
/////////////////////////////////////////////////////////////////
private void Register(IGameObject gameObject)
{
gameObject.Assign(this);
gameObject.OnFinalized += OnGameObjectFinalize;
_gameObjects.Add(gameObject);
OnGameObjectRegistered?.Invoke(this, gameObject);
2023-11-23 22:07:49 +03:00
}
private void Unregister(IGameObject gameObject)
{
gameObject.OnFinalized -= OnGameObjectFinalize;
_gameObjects.Remove(gameObject);
OnGameObjectUnRegistered?.Invoke(this, gameObject);
}
private void OnGameObjectFinalize(IInitialize initialize)
{
if (initialize is IGameObject gameObject)
Unregister(gameObject);
}
/////////////////////////////////////////////////////////////////
public IEnumerator<IGameObject> GetEnumerator() => _gameObjects.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _gameObjects.GetEnumerator();
2023-11-23 22:07:49 +03:00
}