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.

167 lines
5.0 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. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  19. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  20. // frames can be really slow to draw.
  21. int framesToSuppress;
  22. int levelIdx = -1;
  23. Scene scene;
  24. Player player;
  25. World world;
  26. LinesOfSight linesOfSight;
  27. Camera camera = new Camera();
  28. public SneakGame() {
  29. Debug.WriteLine("MonoGame platform: " + PlatformInfo.MonoGamePlatform +
  30. " w/ graphics backend: " + PlatformInfo.GraphicsBackend);
  31. graphics = new GraphicsDeviceManager(this) {
  32. SynchronizeWithVerticalRetrace = true,
  33. GraphicsProfile = GraphicsProfile.HiDef
  34. };
  35. IsFixedTimeStep = true;
  36. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  37. IsMouseVisible = true;
  38. Content.RootDirectory = "Content";
  39. }
  40. // Performs initialization that's needed before starting to run.
  41. protected override void Initialize() {
  42. display = (IDisplay) Services.GetService(typeof(IDisplay));
  43. display.Initialize(Window, graphics);
  44. display.SetFullScreen(fullScreen);
  45. Debug.Initialize(GraphicsDevice);
  46. RasterizerState rasterizerState = new RasterizerState() {
  47. CullMode = CullMode.None
  48. };
  49. GraphicsDevice.RasterizerState = rasterizerState;
  50. base.Initialize();
  51. }
  52. // Called once per game. Loads all game content.
  53. protected override void LoadContent() {
  54. base.LoadContent();
  55. SoundEffects.Load(Content);
  56. Textures.Load(Content);
  57. linesOfSight = new LinesOfSight(GraphicsDevice);
  58. LoadLevel();
  59. }
  60. private void LoadLevel() {
  61. framesToSuppress = 2;
  62. camera = new Camera();
  63. player = new Player();
  64. levelIdx++;
  65. world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
  66. scene?.Dispose();
  67. scene = new Scene(GraphicsDevice, camera);
  68. GC.Collect();
  69. GC.WaitForPendingFinalizers();
  70. }
  71. // Called once per game. Unloads all game content.
  72. protected override void UnloadContent() {
  73. base.UnloadContent();
  74. updateTimer.DumpStats();
  75. drawTimer.DumpStats();
  76. }
  77. // Updates the game world.
  78. protected override void Update(GameTime gameTime) {
  79. updateTimer.Start();
  80. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  81. if (input[0].Exit) {
  82. Exit();
  83. }
  84. if (input[0].Pause && !input[1].Pause) {
  85. paused = !paused;
  86. }
  87. if (input[0].FullScreen && !input[1].FullScreen) {
  88. fullScreen = !fullScreen;
  89. display.SetFullScreen(fullScreen);
  90. }
  91. if (input[0].Restart && !input[1].Restart) {
  92. LoadLevel();
  93. }
  94. Debug.Clear(paused);
  95. if (input[0].Debug && !input[1].Debug) {
  96. Debug.Enabled = !Debug.Enabled;
  97. }
  98. if (!paused) {
  99. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  100. Clock.AddModelTime(modelTime);
  101. player.Update(modelTime, input, world.CollisionTargets);
  102. world.Update(modelTime);
  103. linesOfSight.Update(player, world.CollisionTargets);
  104. camera.Update(player.Position, world.Width);
  105. if (player.Health <= 0) {
  106. world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
  107. player = new Player();
  108. }
  109. }
  110. base.Update(gameTime);
  111. updateTimer.Stop();
  112. }
  113. // Called when the game should draw itself.
  114. protected override void Draw(GameTime gameTime) {
  115. drawTimer.Start();
  116. // Enable the scene after we've gotten enough non-slow frames.
  117. // TODO: the Scene should handle this, really.
  118. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  119. framesToSuppress--;
  120. if (framesToSuppress == 0) {
  121. scene.Enabled = true;
  122. }
  123. }
  124. // We need to update the FPS counter in Draw() since Update() might get called more
  125. // frequently, especially when gameTime.IsRunningSlowly.
  126. fpsCounter.Update();
  127. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  128. $"{fpsCounter.Fps} FPS";
  129. if (paused) {
  130. fpsText += " (paused)";
  131. }
  132. Debug.SetFpsText(fpsText);
  133. scene.Draw(world, player, linesOfSight, paused);
  134. base.Draw(gameTime);
  135. drawTimer.Stop();
  136. }
  137. }
  138. }