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.

161 lines
4.8 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 = 0;
  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. Textures.Load(Content);
  56. linesOfSight = new LinesOfSight(GraphicsDevice);
  57. LoadLevel();
  58. }
  59. private void LoadLevel() {
  60. framesToSuppress = 2;
  61. camera = new Camera();
  62. player = new Player();
  63. world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
  64. scene?.Dispose();
  65. scene = new Scene(GraphicsDevice, camera);
  66. levelIdx++;
  67. GC.Collect();
  68. GC.WaitForPendingFinalizers();
  69. }
  70. // Called once per game. Unloads all game content.
  71. protected override void UnloadContent() {
  72. base.UnloadContent();
  73. updateTimer.DumpStats();
  74. drawTimer.DumpStats();
  75. }
  76. // Updates the game world.
  77. protected override void Update(GameTime gameTime) {
  78. updateTimer.Start();
  79. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  80. if (input[0].Exit) {
  81. Exit();
  82. }
  83. if (input[0].Pause && !input[1].Pause) {
  84. paused = !paused;
  85. }
  86. if (input[0].FullScreen && !input[1].FullScreen) {
  87. fullScreen = !fullScreen;
  88. display.SetFullScreen(fullScreen);
  89. }
  90. if (input[0].Restart && !input[1].Restart) {
  91. LoadLevel();
  92. }
  93. Debug.Clear(paused);
  94. if (input[0].Debug && !input[1].Debug) {
  95. Debug.Enabled = !Debug.Enabled;
  96. }
  97. if (!paused) {
  98. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  99. Clock.AddModelTime(modelTime);
  100. player.Update(modelTime, input, world.CollisionTargets);
  101. linesOfSight.Update(player, world.CollisionTargets);
  102. camera.Update(player.Position, world.Width);
  103. }
  104. base.Update(gameTime);
  105. updateTimer.Stop();
  106. }
  107. // Called when the game should draw itself.
  108. protected override void Draw(GameTime gameTime) {
  109. drawTimer.Start();
  110. // Enable the scene after we've gotten enough non-slow frames.
  111. // TODO: the Scene should handle this, really.
  112. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  113. framesToSuppress--;
  114. if (framesToSuppress == 0) {
  115. scene.Enabled = true;
  116. }
  117. }
  118. // We need to update the FPS counter in Draw() since Update() might get called more
  119. // frequently, especially when gameTime.IsRunningSlowly.
  120. fpsCounter.Update();
  121. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  122. $"{fpsCounter.Fps} FPS";
  123. if (paused) {
  124. fpsText += " (paused)";
  125. }
  126. Debug.SetFpsText(fpsText);
  127. scene.Draw(world, player, linesOfSight, paused);
  128. base.Draw(gameTime);
  129. drawTimer.Stop();
  130. }
  131. }
  132. }