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.

159 lines
5.0 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. readonly GraphicsDeviceManager graphics;
  9. RenderTarget2D renderTarget;
  10. SpriteBatch spriteBatch;
  11. SpriteFont font;
  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. Texture2D grasslandBg1;
  18. Texture2D grasslandBg2;
  19. Player player;
  20. World world;
  21. readonly Camera camera = new Camera();
  22. public SneakGame() {
  23. graphics = new GraphicsDeviceManager(this) {
  24. SynchronizeWithVerticalRetrace = true,
  25. GraphicsProfile = GraphicsProfile.HiDef
  26. };
  27. IsFixedTimeStep = true;
  28. TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60);
  29. IsMouseVisible = true;
  30. Content.RootDirectory = "Content";
  31. }
  32. // Performs initialization that's needed before starting to run.
  33. protected override void Initialize() {
  34. display = (IDisplay) Services.GetService(typeof(IDisplay));
  35. display.Initialize(Window, graphics);
  36. display.SetFullScreen(fullScreen);
  37. Debug.Initialize(GraphicsDevice);
  38. renderTarget = new RenderTarget2D(
  39. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  40. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  41. base.Initialize();
  42. }
  43. // Called once per game. Loads all game content.
  44. protected override void LoadContent() {
  45. spriteBatch = new SpriteBatch(GraphicsDevice);
  46. font = Content.Load<SpriteFont>("font");
  47. player = new Player(Content.Load<Texture2D>("player_1x"));
  48. world = new World(Content.Load<Texture2D>("grassland"));
  49. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  50. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  51. }
  52. // Called once per game. Unloads all game content.
  53. protected override void UnloadContent() {
  54. }
  55. // Updates the game world.
  56. protected override void Update(GameTime gameTime) {
  57. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  58. if (input[0].Exit) {
  59. Exit();
  60. }
  61. if (input[0].Pause && !input[1].Pause) {
  62. paused = !paused;
  63. }
  64. if (input[0].FullScreen && !input[1].FullScreen) {
  65. fullScreen = !fullScreen;
  66. display.SetFullScreen(fullScreen);
  67. }
  68. Debug.Clear(paused);
  69. if (input[0].Debug && !input[1].Debug) {
  70. Debug.Enabled = !Debug.Enabled;
  71. }
  72. if (!paused) {
  73. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  74. Clock.AddModelTime(modelTime);
  75. player.Update(modelTime, input, world.CollisionTargets);
  76. camera.Update(player.Position, world.Width);
  77. }
  78. base.Update(gameTime);
  79. }
  80. // Called when the game should draw itself.
  81. protected override void Draw(GameTime gameTime) {
  82. // We need to update the FPS counter in Draw() since Update() might get called more
  83. // frequently, especially when gameTime.IsRunningSlowly.
  84. fpsCounter.Update();
  85. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  86. $"{fpsCounter.Fps} FPS";
  87. if (paused) {
  88. fpsText += " (paused)";
  89. }
  90. Debug.SetFpsText(fpsText);
  91. // Draw scene to RenderTarget.
  92. GraphicsDevice.SetRenderTarget(renderTarget);
  93. GraphicsDevice.Clear(Color.CornflowerBlue);
  94. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  95. // Draw background.
  96. Rectangle bgSource = new Rectangle(
  97. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  98. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  99. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  100. bgSource = new Rectangle(
  101. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  102. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  103. // Draw player.
  104. player.Draw(spriteBatch, camera);
  105. // Draw foreground tiles.
  106. world.Draw(spriteBatch, camera);
  107. // Draw debug rects & lines.
  108. Debug.Draw(spriteBatch, camera);
  109. // Aaaaand we're done.
  110. spriteBatch.End();
  111. // Draw RenderTarget to screen.
  112. GraphicsDevice.SetRenderTarget(null);
  113. GraphicsDevice.Clear(Color.Black);
  114. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  115. SamplerState.PointClamp, DepthStencilState.Default,
  116. RasterizerState.CullNone);
  117. Rectangle drawRect = new Rectangle(
  118. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  119. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  120. // Draw debug toasts.
  121. Debug.DrawToasts(spriteBatch, font);
  122. spriteBatch.End();
  123. base.Draw(gameTime);
  124. }
  125. }
  126. }