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.

161 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. Debug.Clear();
  58. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  59. if (input[0].Exit) {
  60. Exit();
  61. }
  62. if (input[0].Pause && !input[1].Pause) {
  63. paused = !paused;
  64. }
  65. if (input[0].FullScreen && !input[1].FullScreen) {
  66. fullScreen = !fullScreen;
  67. display.SetFullScreen(fullScreen);
  68. }
  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. List<Rectangle> collisionTargets = world.CollisionTargets();
  76. player.Update(modelTime, input, collisionTargets);
  77. camera.Update(player.Position);
  78. }
  79. base.Update(gameTime);
  80. }
  81. // Called when the game should draw itself.
  82. protected override void Draw(GameTime gameTime) {
  83. // We need to update the FPS counter in Draw() since Update() might get called more
  84. // frequently, especially when gameTime.IsRunningSlowly.
  85. fpsCounter.Update();
  86. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  87. $"{fpsCounter.Fps} FPS";
  88. if (paused) {
  89. fpsText += " (paused)";
  90. }
  91. Debug.SetFpsText(fpsText);
  92. // Draw scene to RenderTarget.
  93. GraphicsDevice.SetRenderTarget(renderTarget);
  94. GraphicsDevice.Clear(Color.CornflowerBlue);
  95. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  96. // Draw background.
  97. Rectangle bgSource = new Rectangle(
  98. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  99. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  100. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  101. bgSource = new Rectangle(
  102. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  103. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  104. // Draw player.
  105. player.Draw(spriteBatch, camera);
  106. // Draw foreground tiles.
  107. world.Draw(spriteBatch, camera);
  108. // Draw debug rects & lines.
  109. Debug.Draw(spriteBatch, camera);
  110. // Aaaaand we're done.
  111. spriteBatch.End();
  112. // Draw RenderTarget to screen.
  113. GraphicsDevice.SetRenderTarget(null);
  114. GraphicsDevice.Clear(Color.Black);
  115. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  116. SamplerState.PointClamp, DepthStencilState.Default,
  117. RasterizerState.CullNone);
  118. Rectangle drawRect = new Rectangle(
  119. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  120. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  121. // Draw debug toasts.
  122. Debug.DrawToasts(spriteBatch, font);
  123. spriteBatch.End();
  124. base.Draw(gameTime);
  125. }
  126. }
  127. }