2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2020-02-18 18:57:02 +00:00
|
|
|
using MonoGame.Framework.Utilities;
|
2020-01-18 03:41:45 +00:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
public class SneakGame : Game {
|
2020-02-03 23:17:28 +00:00
|
|
|
const int TARGET_FPS = 60;
|
|
|
|
const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
|
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly GraphicsDeviceManager graphics;
|
2020-02-11 22:06:17 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
bool fullScreen = false;
|
2020-01-24 22:49:29 +00:00
|
|
|
bool paused = false;
|
2020-01-18 03:41:45 +00:00
|
|
|
IDisplay display;
|
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly History<Input> input = new History<Input>(2);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly FpsCounter fpsCounter = new FpsCounter();
|
2020-02-03 23:17:28 +00:00
|
|
|
readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
|
|
|
|
readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-18 02:08:01 +00:00
|
|
|
Scene scene;
|
2020-01-18 03:41:45 +00:00
|
|
|
World world;
|
2020-02-17 00:21:41 +00:00
|
|
|
Camera camera = new Camera();
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public SneakGame() {
|
2020-02-18 18:57:02 +00:00
|
|
|
Debug.WriteLine("MonoGame platform: " + PlatformInfo.MonoGamePlatform +
|
|
|
|
" w/ graphics backend: " + PlatformInfo.GraphicsBackend);
|
2020-01-25 02:09:09 +00:00
|
|
|
graphics = new GraphicsDeviceManager(this) {
|
2020-01-27 03:50:14 +00:00
|
|
|
SynchronizeWithVerticalRetrace = true,
|
|
|
|
GraphicsProfile = GraphicsProfile.HiDef
|
2020-01-25 02:09:09 +00:00
|
|
|
};
|
2020-01-25 00:57:07 +00:00
|
|
|
IsFixedTimeStep = true;
|
2020-02-03 23:17:28 +00:00
|
|
|
TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
|
2020-01-18 03:41:45 +00:00
|
|
|
IsMouseVisible = true;
|
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs initialization that's needed before starting to run.
|
|
|
|
protected override void Initialize() {
|
|
|
|
display = (IDisplay) Services.GetService(typeof(IDisplay));
|
|
|
|
display.Initialize(Window, graphics);
|
|
|
|
display.SetFullScreen(fullScreen);
|
|
|
|
|
2020-02-15 21:31:59 +00:00
|
|
|
Debug.Initialize(GraphicsDevice);
|
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
RasterizerState rasterizerState = new RasterizerState() {
|
|
|
|
CullMode = CullMode.None
|
|
|
|
};
|
|
|
|
GraphicsDevice.RasterizerState = rasterizerState;
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
base.Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Loads all game content.
|
|
|
|
protected override void LoadContent() {
|
2020-02-15 21:31:59 +00:00
|
|
|
base.LoadContent();
|
2020-02-28 01:23:05 +00:00
|
|
|
SoundEffects.Load(Content);
|
2020-02-19 16:19:23 +00:00
|
|
|
Textures.Load(Content);
|
2020-03-03 22:14:05 +00:00
|
|
|
Sprites.Load(Content);
|
2020-02-17 00:21:41 +00:00
|
|
|
LoadLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadLevel() {
|
|
|
|
camera = new Camera();
|
2020-03-09 16:19:19 +00:00
|
|
|
world?.Dispose();
|
|
|
|
world = new World(GraphicsDevice, Content.LoadString("levels/demo.json"));
|
2020-02-19 21:30:34 +00:00
|
|
|
scene?.Dispose();
|
2020-02-19 16:19:23 +00:00
|
|
|
scene = new Scene(GraphicsDevice, camera);
|
2020-02-19 21:30:34 +00:00
|
|
|
|
|
|
|
GC.Collect();
|
|
|
|
GC.WaitForPendingFinalizers();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Unloads all game content.
|
|
|
|
protected override void UnloadContent() {
|
2020-02-15 21:31:59 +00:00
|
|
|
base.UnloadContent();
|
2020-02-04 14:55:07 +00:00
|
|
|
updateTimer.DumpStats();
|
|
|
|
drawTimer.DumpStats();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the game world.
|
|
|
|
protected override void Update(GameTime gameTime) {
|
2020-02-03 23:17:28 +00:00
|
|
|
updateTimer.Start();
|
2020-01-18 03:41:45 +00:00
|
|
|
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
|
|
|
|
|
|
|
if (input[0].Exit) {
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (input[0].Pause && !input[1].Pause) {
|
|
|
|
paused = !paused;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (input[0].FullScreen && !input[1].FullScreen) {
|
|
|
|
fullScreen = !fullScreen;
|
|
|
|
display.SetFullScreen(fullScreen);
|
|
|
|
}
|
|
|
|
|
2020-02-17 00:21:41 +00:00
|
|
|
if (input[0].Restart && !input[1].Restart) {
|
|
|
|
LoadLevel();
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:33:55 +00:00
|
|
|
Debug.Clear(paused);
|
2020-01-18 03:41:45 +00:00
|
|
|
if (input[0].Debug && !input[1].Debug) {
|
|
|
|
Debug.Enabled = !Debug.Enabled;
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (!paused) {
|
2020-01-24 23:36:55 +00:00
|
|
|
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
2020-01-25 01:42:27 +00:00
|
|
|
Clock.AddModelTime(modelTime);
|
2020-03-03 18:04:29 +00:00
|
|
|
world.Update(modelTime, input);
|
|
|
|
camera.Update(world.Player.Position, world.Width);
|
2020-01-24 22:49:29 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
base.Update(gameTime);
|
2020-02-03 23:17:28 +00:00
|
|
|
updateTimer.Stop();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game should draw itself.
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
2020-02-03 23:17:28 +00:00
|
|
|
drawTimer.Start();
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
|
|
fpsCounter.Update();
|
2020-01-23 17:19:16 +00:00
|
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
|
|
$"{fpsCounter.Fps} FPS";
|
2020-01-24 22:49:29 +00:00
|
|
|
if (paused) {
|
2020-01-25 02:09:09 +00:00
|
|
|
fpsText += " (paused)";
|
2020-01-24 22:49:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
Debug.SetFpsText(fpsText);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-09 16:19:19 +00:00
|
|
|
scene.Draw(gameTime.IsRunningSlowly, world, paused);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
base.Draw(gameTime);
|
2020-02-03 23:17:28 +00:00
|
|
|
drawTimer.Stop();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|