A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.2 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using MonoGame.Framework.Utilities;
  5. using System;
  6. namespace SemiColinGames {
  7. public class SneakGame : Game {
  8. const int TARGET_FPS = 60;
  9. const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
  10. readonly GraphicsDeviceManager graphics;
  11. bool fullScreen = false;
  12. bool paused = false;
  13. IDisplay display;
  14. readonly History<Input> input = new History<Input>(2);
  15. readonly FpsCounter fpsCounter = new FpsCounter();
  16. readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
  17. readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
  18. IScene scene;
  19. IWorld world;
  20. public SneakGame() {
  21. Debug.WriteLine("MonoGame platform: " + PlatformInfo.MonoGamePlatform +
  22. " w/ graphics backend: " + PlatformInfo.GraphicsBackend);
  23. graphics = new GraphicsDeviceManager(this) {
  24. SynchronizeWithVerticalRetrace = true,
  25. GraphicsProfile = GraphicsProfile.HiDef
  26. };
  27. IsFixedTimeStep = true;
  28. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  29. IsMouseVisible = true;
  30. Content.RootDirectory = "Content";
  31. }
  32. // Performs initialization that's needed before starting to run.
  33. protected override void Initialize() {
  34. display = (IDisplay) Services.GetService(typeof(IDisplay));
  35. display.Initialize(Window, graphics);
  36. display.SetFullScreen(fullScreen);
  37. Debug.Initialize(GraphicsDevice);
  38. RasterizerState rasterizerState = new RasterizerState() {
  39. CullMode = CullMode.None
  40. };
  41. GraphicsDevice.RasterizerState = rasterizerState;
  42. base.Initialize();
  43. }
  44. // Called once per game. Loads all game content.
  45. protected override void LoadContent() {
  46. base.LoadContent();
  47. SoundEffects.Load(Content);
  48. Textures.Load(Content);
  49. Sprites.Load(Content);
  50. LoadLevel();
  51. }
  52. private void LoadLevel() {
  53. world?.Dispose();
  54. // world = new World(GraphicsDevice, Content.LoadString("levels/demo.json"));
  55. world = new TreeWorld();
  56. scene?.Dispose();
  57. // scene = new Scene(GraphicsDevice, ((World) world).Camera);
  58. scene = new TreeScene(GraphicsDevice);
  59. GC.Collect();
  60. GC.WaitForPendingFinalizers();
  61. }
  62. // Called once per game. Unloads all game content.
  63. protected override void UnloadContent() {
  64. base.UnloadContent();
  65. updateTimer.DumpStats();
  66. drawTimer.DumpStats();
  67. }
  68. // Updates the game world.
  69. protected override void Update(GameTime gameTime) {
  70. updateTimer.Start();
  71. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  72. if (input[0].Exit) {
  73. Exit();
  74. }
  75. if (input[0].Pause && !input[1].Pause) {
  76. paused = !paused;
  77. }
  78. if (input[0].FullScreen && !input[1].FullScreen) {
  79. fullScreen = !fullScreen;
  80. display.SetFullScreen(fullScreen);
  81. }
  82. if (input[0].Restart && !input[1].Restart) {
  83. LoadLevel();
  84. }
  85. Debug.Clear(paused);
  86. if (input[0].Debug && !input[1].Debug) {
  87. Debug.Enabled = !Debug.Enabled;
  88. }
  89. if (!paused) {
  90. double modelTime = gameTime.ElapsedGameTime.TotalSeconds;
  91. // To prevent huge diffs, never update by more than 4 frames' worth of time.
  92. modelTime = Math.Min(modelTime, TARGET_FRAME_TIME * 4.0f);
  93. Clock.AddModelTime(modelTime);
  94. world.Update((float) modelTime, input);
  95. }
  96. base.Update(gameTime);
  97. updateTimer.Stop();
  98. }
  99. // Called when the game should draw itself.
  100. protected override void Draw(GameTime gameTime) {
  101. drawTimer.Start();
  102. // We need to update the FPS counter in Draw() since Update() might get called more
  103. // frequently, especially when gameTime.IsRunningSlowly.
  104. fpsCounter.Update();
  105. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  106. $"{fpsCounter.Fps} FPS";
  107. if (paused) {
  108. fpsText += " (paused)";
  109. }
  110. Debug.SetFpsText(fpsText);
  111. scene.Draw(gameTime.IsRunningSlowly, world, paused);
  112. base.Draw(gameTime);
  113. drawTimer.Stop();
  114. }
  115. }
  116. }