Syntriax.Engine/Engine.Core/GameManager.cs

195 lines
6.5 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-10-25 23:10:20 +03:00
public event IGameManager.OnUpdateDelegate? OnUpdate = null;
public event IGameManager.OnPreDawDelegate? OnPreDraw = null;
2024-07-15 01:13:39 +03:00
public event IGameManager.OnGameObjectRegisteredDelegate? OnGameObjectRegistered = null;
public event IGameManager.OnGameObjectUnRegisteredDelegate? OnGameObjectUnRegistered = null;
public event IGameManager.OnHierarchyObjectRegisteredDelegate? OnHierarchyObjectRegistered = null;
public event IGameManager.OnHierarchyObjectUnRegisteredDelegate? OnHierarchyObjectUnRegistered = null;
2024-01-30 12:08:21 +03:00
private readonly List<IGameObject> _gameObjects = new(Constants.GAME_OBJECTS_SIZE_INITIAL);
private readonly List<IHierarchyObject> _hierarchyObjects = 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;
public IReadOnlyList<IHierarchyObject> HierarchyObjects => _hierarchyObjects;
2024-01-30 12:08:21 +03:00
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 Register(IHierarchyObject hierarchyObject)
2023-11-23 22:07:49 +03:00
{
if (_hierarchyObjects.Contains(hierarchyObject))
throw new Exception($"{nameof(IHierarchyObject)} named {hierarchyObject.Name} is already registered to the {nameof(GameManager)}.");
2023-11-23 22:07:49 +03:00
if (hierarchyObject is IGameObject gameObject)
Register(gameObject);
else
{
if (!hierarchyObject.Initialize())
throw new Exception($"{nameof(hierarchyObject)} can't be initialized");
_hierarchyObjects.Add(hierarchyObject);
hierarchyObject.EnterHierarchy(this);
OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject);
}
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 void Remove(IHierarchyObject hierarchyObject)
2023-11-23 22:07:49 +03:00
{
if (!_hierarchyObjects.Contains(hierarchyObject))
throw new Exception($"{nameof(IHierarchyObject)} named {hierarchyObject.Name} is not registered to the {nameof(GameManager)}.");
2023-11-23 22:07:49 +03:00
if (hierarchyObject is IGameObject gameObject)
Unregister(gameObject);
else
{
_hierarchyObjects.Remove(hierarchyObject);
hierarchyObject.ExitHierarchy();
if (!hierarchyObject.Finalize())
throw new Exception($"{nameof(hierarchyObject)} can't be finalized");
OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject);
}
}
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);
for (int i = 0; i < GameObjects.Count; i++)
GameObjects[i].BehaviourController.Update();
2024-10-25 23:10:20 +03:00
OnUpdate?.Invoke(this, time);
}
public void PreDraw()
{
for (int i = 0; i < GameObjects.Count; i++)
GameObjects[i].BehaviourController.UpdatePreDraw();
2024-10-25 23:10:20 +03:00
OnPreDraw?.Invoke(this);
}
/////////////////////////////////////////////////////////////////
private void Register(IGameObject gameObject)
{
if (_gameObjects.Contains(gameObject))
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
gameObject.OnFinalized += OnGameObjectFinalize;
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
if (!gameObject.Initialize())
throw new Exception($"{nameof(gameObject)} can't be initialized");
foreach (ITransform child in gameObject.Transform.Children)
Register(child.GameObject);
_gameObjects.Add(gameObject);
_hierarchyObjects.Add(gameObject);
if (!gameObject.EnterHierarchy(this))
throw new Exception($"{nameof(gameObject)} can't enter the hierarchy");
OnHierarchyObjectRegistered?.Invoke(this, gameObject);
OnGameObjectRegistered?.Invoke(this, gameObject);
2023-11-23 22:07:49 +03:00
}
private void Unregister(IGameObject gameObject)
{
if (!_gameObjects.Contains(gameObject))
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
gameObject.OnFinalized -= OnGameObjectFinalize;
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
foreach (ITransform child in gameObject.Transform.Children)
Unregister(child.GameObject);
_gameObjects.Remove(gameObject);
_hierarchyObjects.Remove(gameObject);
if (!gameObject.ExitHierarchy())
throw new Exception($"{nameof(gameObject)} can't exit the hierarchy");
if (!gameObject.Finalize())
throw new Exception($"{nameof(gameObject)} can't be finalized");
OnHierarchyObjectUnRegistered?.Invoke(this, gameObject);
OnGameObjectUnRegistered?.Invoke(this, gameObject);
}
private void OnGameObjectFinalize(IInitialize initialize)
{
if (initialize is IGameObject gameObject)
Unregister(gameObject);
}
private void OnGameObjectExitedHierarchy(IHierarchyObject sender, IGameManager gameManager)
{
if (sender is IGameObject gameObject)
Unregister(gameObject);
}
2023-11-23 22:07:49 +03:00
}