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.

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