experiments-monogame/UPWG/Elite/Engine.cs

81 lines
2.2 KiB
C#
Executable File

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Elite
{
public class Engine : Game
{
public static Engine Instance;
private GraphicsDeviceManager _graphicsDeviceManager;
private int _width;
private int _height;
private Color _clearColor;
private Scene _currentScene;
private Scene _nextScene;
private float _elapsed;
public static Scene Scene
{
get => Instance._currentScene;
set
{
if (Instance._currentScene != null)
Instance._nextScene = value;
else
Instance._currentScene = value;
}
}
public static float Elapsed => Instance._elapsed;
public Engine(int width, int height, Color clearColor)
{
Instance = this;
_graphicsDeviceManager = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = false;
_clearColor = clearColor;
_width = width;
_height = height;
}
protected override void Initialize()
{
base.Initialize();
_graphicsDeviceManager.PreferredBackBufferWidth = _width;
_graphicsDeviceManager.PreferredBackBufferHeight = _height;
_graphicsDeviceManager.ApplyChanges();
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
_elapsed = (float) gameTime.ElapsedGameTime.TotalSeconds;
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_currentScene?.Update(_elapsed);
if (_nextScene != null)
{
_currentScene = _nextScene;
_nextScene = null;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(_clearColor);
_currentScene.Draw(_elapsed);
}
}
}