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.

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