Skip to content

Commit

Permalink
Merge pull request #3 from BlurOne-GIT/develop
Browse files Browse the repository at this point in the history
Merge develop changes for v2.1.0
  • Loading branch information
BlurOne-GIT authored Apr 7, 2024
2 parents 1195565 + b8b28f0 commit b0b5f65
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
7 changes: 3 additions & 4 deletions MmgEngine/EngineGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ protected override void LoadContent()
/// Also called by <see cref="GameState.OnStateSwitched"/>.
/// </summary>
/// <param name="newGameState">New game state.</param>
protected void SwitchGameState(GameState newGameState)
protected virtual void SwitchGameState(GameState newGameState)
{
if (CurrentGameState is not null)
{
Components.Remove(CurrentGameState);
CurrentGameState.OnStateSwitched -= OnStateSwitched;
CurrentGameState.UnloadContent();
CurrentGameState.Dispose();
}

CurrentGameState = newGameState;

CurrentGameState.Initialize();
CurrentGameState.LoadContent();
Components.Add(CurrentGameState);

CurrentGameState.OnStateSwitched += OnStateSwitched;
}
Expand Down
31 changes: 7 additions & 24 deletions MmgEngine/GameState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;

namespace MmgEngine;
Expand All @@ -10,17 +9,17 @@ public GameState(Game game) : base(game)
{
Game.Window.KeyDown += HandleInput;
Input.ButtonDown += HandleInput;
Components.ComponentAdded += InitializeComponent;
Components.ComponentRemoved += DisposeComponent;
Components.ComponentAdded += OnComponentAdded;
Components.ComponentRemoved += OnComponentRemoved;
}

protected override void Dispose(bool disposing)
{
Game.Window.KeyDown -= HandleInput;
Input.ButtonDown -= HandleInput;
Components.ComponentAdded -= InitializeComponent;
Components.ComponentAdded -= OnComponentAdded;
Components.Clear();
Components.ComponentRemoved -= DisposeComponent;
Components.ComponentRemoved -= OnComponentRemoved;
base.Dispose(disposing);
}

Expand All @@ -38,33 +37,17 @@ public virtual void HandleInput(object sender, InputKeyEventArgs eventArgs) {}

public event EventHandler<GameState> OnStateSwitched;

public new abstract void LoadContent();

public new abstract void UnloadContent();

/// <summary>
/// Change the current game state.
/// </summary>
/// <param name="gameState">New game state.</param>
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);
foreach (DrawableGameComponent gameObject in c)
gameObject.Draw(gameTime);
}

private static void InitializeComponent(object s, GameComponentCollectionEventArgs e) => e.GameComponent.Initialize();
private void OnComponentAdded(object s, GameComponentCollectionEventArgs e) => Game.Components.Add(e.GameComponent);

private static void DisposeComponent(object s, GameComponentCollectionEventArgs e)
private void OnComponentRemoved(object s, GameComponentCollectionEventArgs e)
{
Game.Components.Remove(e.GameComponent);
if (e.GameComponent is IDisposable disposable)
disposable.Dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion MmgEngine/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void UpdateMouseInput(MouseState mouseState)
RightButton = Convert.ToBoolean(mouseState.RightButton);
XButton1 = Convert.ToBoolean(mouseState.XButton1);
XButton2 = Convert.ToBoolean(mouseState.XButton2);
MousePoint = ((mouseState.Position.ToVector2() - EngineStatics.Offset) / EngineStatics.Scale ).ToPoint();
MousePoint = ((mouseState.Position.ToVector2() - EngineStatics.Offset) / EngineStatics.Scale).ToPoint();
}

private static void CheckMouseInput(ref bool button, bool value, [CallerMemberName] string name = null)
Expand Down
2 changes: 1 addition & 1 deletion MmgEngine/MmgEngine.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303">
Expand Down

0 comments on commit b0b5f65

Please sign in to comment.