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.5 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. int levelIdx = -1;
  19. Scene scene;
  20. Player player;
  21. World world;
  22. LinesOfSight linesOfSight;
  23. Camera camera = new Camera();
  24. public SneakGame() {
  25. Debug.WriteLine("MonoGame platform: " + PlatformInfo.MonoGamePlatform +
  26. " w/ graphics backend: " + PlatformInfo.GraphicsBackend);
  27. graphics = new GraphicsDeviceManager(this) {
  28. SynchronizeWithVerticalRetrace = true,
  29. GraphicsProfile = GraphicsProfile.HiDef
  30. };
  31. IsFixedTimeStep = true;
  32. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  33. IsMouseVisible = true;
  34. Content.RootDirectory = "Content";
  35. }
  36. // Performs initialization that's needed before starting to run.
  37. protected override void Initialize() {
  38. display = (IDisplay) Services.GetService(typeof(IDisplay));
  39. display.Initialize(Window, graphics);
  40. display.SetFullScreen(fullScreen);
  41. Debug.Initialize(GraphicsDevice);
  42. RasterizerState rasterizerState = new RasterizerState() {
  43. CullMode = CullMode.None
  44. };
  45. GraphicsDevice.RasterizerState = rasterizerState;
  46. base.Initialize();
  47. }
  48. // Called once per game. Loads all game content.
  49. protected override void LoadContent() {
  50. base.LoadContent();
  51. SoundEffects.Load(Content);
  52. Textures.Load(Content);
  53. linesOfSight = new LinesOfSight(GraphicsDevice);
  54. LoadLevel();
  55. }
  56. private void LoadLevel() {
  57. camera = new Camera();
  58. player = new Player();
  59. levelIdx++;
  60. world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
  61. scene?.Dispose();
  62. scene = new Scene(GraphicsDevice, camera);
  63. GC.Collect();
  64. GC.WaitForPendingFinalizers();
  65. }
  66. // Called once per game. Unloads all game content.
  67. protected override void UnloadContent() {
  68. base.UnloadContent();
  69. updateTimer.DumpStats();
  70. drawTimer.DumpStats();
  71. }
  72. // Updates the game world.
  73. protected override void Update(GameTime gameTime) {
  74. updateTimer.Start();
  75. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  76. if (input[0].Exit) {
  77. Exit();
  78. }
  79. if (input[0].Pause && !input[1].Pause) {
  80. paused = !paused;
  81. }
  82. if (input[0].FullScreen && !input[1].FullScreen) {
  83. fullScreen = !fullScreen;
  84. display.SetFullScreen(fullScreen);
  85. }
  86. if (input[0].Restart && !input[1].Restart) {
  87. LoadLevel();
  88. }
  89. Debug.Clear(paused);
  90. if (input[0].Debug && !input[1].Debug) {
  91. Debug.Enabled = !Debug.Enabled;
  92. }
  93. if (!paused) {
  94. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  95. Clock.AddModelTime(modelTime);
  96. player.Update(modelTime, input, world.CollisionTargets);
  97. world.Update(modelTime);
  98. linesOfSight.Update(player, world.CollisionTargets);
  99. camera.Update(player.Position, world.Width);
  100. if (player.Health <= 0) {
  101. world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
  102. player = new Player();
  103. }
  104. }
  105. base.Update(gameTime);
  106. updateTimer.Stop();
  107. }
  108. // Called when the game should draw itself.
  109. protected override void Draw(GameTime gameTime) {
  110. drawTimer.Start();
  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(gameTime.IsRunningSlowly, world, player, linesOfSight, paused);
  121. base.Draw(gameTime);
  122. drawTimer.Stop();
  123. }
  124. }
  125. }