From f9580333671821911ef2310c96aef62f3a7f0fea Mon Sep 17 00:00:00 2001 From: BlurOne! <61806094+BlurOne-GIT@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:11:29 -0300 Subject: [PATCH] refactor: GameState --- MmgEngine/GameState.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/MmgEngine/GameState.cs b/MmgEngine/GameState.cs index 2f26e25..4a64f0c 100644 --- a/MmgEngine/GameState.cs +++ b/MmgEngine/GameState.cs @@ -11,6 +11,7 @@ public GameState(Game game) : base(game) Input.KeyDown += HandleInput; Input.ButtonDown += HandleInput; } + public new virtual void Dispose() { Input.KeyDown -= HandleInput; @@ -20,21 +21,27 @@ public GameState(Game game) : base(game) base.Dispose(); } + protected readonly GameComponentCollection Components = new(); + public virtual void HandleInput(object s, ButtonEventArgs e) {} + public virtual void HandleInput(object s, InputKeyEventArgs e) {} + public event EventHandler OnStateSwitched; + public new abstract void LoadContent(); + public new abstract void UnloadContent(); - protected void SwitchState(GameState gameState) - { - OnStateSwitched?.Invoke(this, gameState); - } + + protected void SwitchState(GameState gameState) => OnStateSwitched?.Invoke(this, gameState); + public override void Update(GameTime gameTime) { foreach (GameComponent gameObject in Components.OrderBy(a => (a as GameComponent)!.UpdateOrder)) if (gameObject.Enabled) gameObject.Update(gameTime); } + public override void Draw(GameTime gameTime) { var c = Components.Where(a => a is DrawableGameComponent { Visible: true }).OrderBy(a => (a as DrawableGameComponent)!.DrawOrder);