From 5f2c551fa40acf7518a87b0173ee18ecb2cc434a Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 24 Nov 2023 11:52:14 +0300 Subject: [PATCH] feat: Implemented IInitialize on GameManager --- Engine.Core/Engine.Core.sln | 25 ++++++++++++++++++++++ Engine.Core/GameManager.cs | 42 +++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 Engine.Core/Engine.Core.sln diff --git a/Engine.Core/Engine.Core.sln b/Engine.Core/Engine.Core.sln new file mode 100644 index 0000000..5133cda --- /dev/null +++ b/Engine.Core/Engine.Core.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Engine.Core", "Engine.Core.csproj", "{6C1AF4B1-60B0-4225-9A96-F597BC04E9D0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6C1AF4B1-60B0-4225-9A96-F597BC04E9D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C1AF4B1-60B0-4225-9A96-F597BC04E9D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C1AF4B1-60B0-4225-9A96-F597BC04E9D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C1AF4B1-60B0-4225-9A96-F597BC04E9D0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CC449885-B99C-4C71-842D-3C19A35E786F} + EndGlobalSection +EndGlobal diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index 8fafe00..83075c4 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -6,12 +6,20 @@ using Syntriax.Engine.Core.Factory; namespace Syntriax.Engine.Core; -public class GameManager +public class GameManager : IInitialize { + public Action? OnInitialized { get; set; } = null; + public Action? OnFinalized { get; set; } = null; + public Action? OnStateEnableAssigned { get; set; } = null; + + private IList _gameObjects = new List(Constants.GAME_OBJECTS_SIZE_INITIAL); private IList _drawables = new List(Constants.DRAWABLE_OBJECTS_SIZE_INITIAL); + private IStateEnable _stateEnable = null!; private GameObjectFactory _gameObjectFactory = null!; + private bool _initialized = false; + private GameObjectFactory GameObjectFactory { get @@ -22,7 +30,9 @@ public class GameManager } } + public bool Initialized => _initialized; public IList GameObjects => _gameObjects; + public IStateEnable StateEnable { get { if (_stateEnable is null) Assign(new StateEnableFactory().Instantiate(this)); return _stateEnable; } } public void RegisterGameObject(IGameObject gameObject) { @@ -48,10 +58,38 @@ public class GameManager return gameObject; } - public void Initialize() + public bool Initialize() { + if (Initialized) + return false; + foreach (var gameObject in GameObjects) gameObject.Initialize(); + + 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) + { + if (_stateEnable is not null) + return false; + + _stateEnable = stateEnable; + OnStateEnableAssigned?.Invoke(this); + return true; } public void Update(GameTime time)