Skip to content

Commit

Permalink
refactor: GameState
Browse files Browse the repository at this point in the history
  • Loading branch information
BlurOne-GIT committed Mar 26, 2024
1 parent e6e8317 commit f958033
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions MmgEngine/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public GameState(Game game) : base(game)
Input.KeyDown += HandleInput;
Input.ButtonDown += HandleInput;
}

public new virtual void Dispose()
{
Input.KeyDown -= HandleInput;
Expand All @@ -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<GameState> 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);
Expand Down

0 comments on commit f958033

Please sign in to comment.