139 lines
5.2 KiB
C#
139 lines
5.2 KiB
C#
using System;
|
|
using MonoGameBlank2dStartKit.Core.Localization;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using MonoGameBlank2dStartKit.Core.Content;
|
|
using MonoGameBlank2dStartKit.Core.Systems;
|
|
using MoonTools.ECS;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace MonoGameBlank2dStartKit.Core
|
|
{
|
|
/// <summary>
|
|
/// The main class for the game, responsible for managing game components, settings,
|
|
/// and platform-specific configurations.
|
|
/// </summary>
|
|
public class MonoGameBlank2dStartKitGame : Game
|
|
{
|
|
private TextureDrawSystem TextureDrawSystem { get; set; }
|
|
private VelocitySystem VelocitySystem { get; set; }
|
|
private RotationSystem RotationSystem { get; set; }
|
|
|
|
// Resources for drawing.
|
|
private readonly GraphicsDeviceManager graphicsDeviceManager;
|
|
private SpriteBatch _spriteBatch;
|
|
private Assets _assets;
|
|
|
|
private World World { get; init; }
|
|
|
|
/// <summary>
|
|
/// Indicates if the game is running on a mobile platform.
|
|
/// </summary>
|
|
public static readonly bool IsMobile = OperatingSystem.IsAndroid() || OperatingSystem.IsIOS();
|
|
|
|
/// <summary>
|
|
/// Indicates if the game is running on a desktop platform.
|
|
/// </summary>
|
|
public static readonly bool IsDesktop =
|
|
OperatingSystem.IsMacOS() || OperatingSystem.IsLinux() || OperatingSystem.IsWindows();
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the game. Configures platform-specific settings,
|
|
/// initializes services like settings and leaderboard managers, and sets up the
|
|
/// screen manager for screen transitions.
|
|
/// </summary>
|
|
public MonoGameBlank2dStartKitGame()
|
|
{
|
|
graphicsDeviceManager = new GraphicsDeviceManager(this);
|
|
|
|
// Share GraphicsDeviceManager as a service.
|
|
Services.AddService(typeof(GraphicsDeviceManager), graphicsDeviceManager);
|
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
// Configure screen orientations.
|
|
graphicsDeviceManager.SupportedOrientations =
|
|
DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
|
|
|
|
World = new World();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes the game, including setting up localization and adding the
|
|
/// initial screens to the ScreenManager.
|
|
/// </summary>
|
|
protected override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
// Load supported languages and set the default language.
|
|
List<CultureInfo> cultures = LocalizationManager.GetSupportedCultures();
|
|
var languages = new List<CultureInfo>();
|
|
for (int i = 0; i < cultures.Count; i++)
|
|
{
|
|
languages.Add(cultures[i]);
|
|
}
|
|
|
|
// TODO You should load this from a settings file or similar,
|
|
// based on what the user or operating system selected.
|
|
var selectedLanguage = LocalizationManager.DEFAULT_CULTURE_CODE;
|
|
LocalizationManager.SetCulture(selectedLanguage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads game content, such as textures and particle systems.
|
|
/// </summary>
|
|
protected override void LoadContent()
|
|
{
|
|
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
_assets = new Assets(Content);
|
|
|
|
TextureDrawSystem = new TextureDrawSystem(World, _assets, _spriteBatch);
|
|
VelocitySystem = new VelocitySystem(World);
|
|
RotationSystem = new RotationSystem(World);
|
|
Scenarios.Basic(World, _assets);
|
|
base.LoadContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the game's logic, called once per frame.
|
|
/// </summary>
|
|
/// <param name="gameTime">
|
|
/// Provides a snapshot of timing values used for game updates.
|
|
/// </param>
|
|
protected override void Update(GameTime gameTime)
|
|
{
|
|
// Exit the game if the Back button (GamePad) or Escape key (Keyboard) is pressed.
|
|
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
|
|
|| Keyboard.GetState().IsKeyDown(Keys.Escape))
|
|
Exit();
|
|
|
|
var elapsedGameTime = gameTime.ElapsedGameTime;
|
|
VelocitySystem.Update(elapsedGameTime);
|
|
RotationSystem.Update(elapsedGameTime);
|
|
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws the game's graphics, called once per frame.
|
|
/// </summary>
|
|
/// <param name="gameTime">
|
|
/// Provides a snapshot of timing values used for rendering.
|
|
/// </param>
|
|
protected override void Draw(GameTime gameTime)
|
|
{
|
|
var elapsedGameTime = gameTime.ElapsedGameTime;
|
|
// Clears the screen with the MonoGame orange color before drawing.
|
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
|
_spriteBatch.Begin();
|
|
TextureDrawSystem.Update(elapsedGameTime);
|
|
_spriteBatch.End();
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
}
|
|
} |