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.

179 lines
5.7 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  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. RenderTarget2D renderTarget;
  12. SpriteBatch spriteBatch;
  13. SpriteFont font;
  14. bool fullScreen = false;
  15. bool paused = false;
  16. IDisplay display;
  17. readonly History<Input> input = new History<Input>(2);
  18. readonly FpsCounter fpsCounter = new FpsCounter();
  19. readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
  20. readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
  21. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  22. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  23. // frames can be really slow to draw.
  24. int framesToSuppress = 2;
  25. Texture2D grasslandBg1;
  26. Texture2D grasslandBg2;
  27. Player player;
  28. World world;
  29. readonly Camera camera = new Camera();
  30. public SneakGame() {
  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. renderTarget = new RenderTarget2D(
  47. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  48. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  49. base.Initialize();
  50. }
  51. // Called once per game. Loads all game content.
  52. protected override void LoadContent() {
  53. spriteBatch = new SpriteBatch(GraphicsDevice);
  54. font = Content.Load<SpriteFont>("font");
  55. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  56. world = new World(Content.Load<Texture2D>("grassland"));
  57. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  58. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  59. }
  60. // Called once per game. Unloads all game content.
  61. protected override void UnloadContent() {
  62. }
  63. // Updates the game world.
  64. protected override void Update(GameTime gameTime) {
  65. updateTimer.Start();
  66. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  67. if (input[0].Exit) {
  68. Exit();
  69. }
  70. if (input[0].Pause && !input[1].Pause) {
  71. paused = !paused;
  72. }
  73. if (input[0].FullScreen && !input[1].FullScreen) {
  74. fullScreen = !fullScreen;
  75. display.SetFullScreen(fullScreen);
  76. }
  77. Debug.Clear(paused);
  78. if (input[0].Debug && !input[1].Debug) {
  79. Debug.Enabled = !Debug.Enabled;
  80. }
  81. if (!paused) {
  82. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  83. Clock.AddModelTime(modelTime);
  84. player.Update(modelTime, input, world.CollisionTargets);
  85. camera.Update(player.Position, world.Width);
  86. }
  87. base.Update(gameTime);
  88. updateTimer.Stop();
  89. }
  90. // Called when the game should draw itself.
  91. protected override void Draw(GameTime gameTime) {
  92. drawTimer.Start();
  93. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  94. framesToSuppress--;
  95. }
  96. // We need to update the FPS counter in Draw() since Update() might get called more
  97. // frequently, especially when gameTime.IsRunningSlowly.
  98. fpsCounter.Update();
  99. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  100. $"{fpsCounter.Fps} FPS";
  101. if (paused) {
  102. fpsText += " (paused)";
  103. }
  104. Debug.SetFpsText(fpsText);
  105. // Draw scene to RenderTarget.
  106. GraphicsDevice.SetRenderTarget(renderTarget);
  107. GraphicsDevice.Clear(Color.CornflowerBlue);
  108. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  109. // Draw background.
  110. Rectangle bgSource = new Rectangle(
  111. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  112. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  113. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  114. bgSource = new Rectangle(
  115. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  116. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  117. // Draw player.
  118. player.Draw(spriteBatch, camera);
  119. // Draw foreground tiles.
  120. world.Draw(spriteBatch, camera);
  121. // Draw debug rects & lines.
  122. Debug.Draw(spriteBatch, camera);
  123. // Aaaaand we're done.
  124. spriteBatch.End();
  125. // Draw RenderTarget to screen.
  126. GraphicsDevice.SetRenderTarget(null);
  127. GraphicsDevice.Clear(Color.CornflowerBlue);
  128. if (framesToSuppress == 0) {
  129. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  130. SamplerState.PointClamp, DepthStencilState.Default,
  131. RasterizerState.CullNone);
  132. Rectangle drawRect = new Rectangle(
  133. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  134. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  135. // Draw debug toasts.
  136. Debug.DrawToasts(spriteBatch, font);
  137. spriteBatch.End();
  138. }
  139. base.Draw(gameTime);
  140. drawTimer.Stop();
  141. }
  142. }
  143. }