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.

172 lines
5.2 KiB

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