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.

160 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 = false
  25. };
  26. IsFixedTimeStep = true;
  27. TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60);
  28. IsMouseVisible = true;
  29. Content.RootDirectory = "Content";
  30. }
  31. // Performs initialization that's needed before starting to run.
  32. protected override void Initialize() {
  33. display = (IDisplay) Services.GetService(typeof(IDisplay));
  34. display.Initialize(Window, graphics);
  35. display.SetFullScreen(fullScreen);
  36. Debug.Initialize(GraphicsDevice);
  37. renderTarget = new RenderTarget2D(
  38. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  39. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  40. base.Initialize();
  41. }
  42. // Called once per game. Loads all game content.
  43. protected override void LoadContent() {
  44. spriteBatch = new SpriteBatch(GraphicsDevice);
  45. font = Content.Load<SpriteFont>("font");
  46. player = new Player(Content.Load<Texture2D>("player_1x"));
  47. world = new World(Content.Load<Texture2D>("grassland"));
  48. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  49. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  50. }
  51. // Called once per game. Unloads all game content.
  52. protected override void UnloadContent() {
  53. }
  54. // Updates the game world.
  55. protected override void Update(GameTime gameTime) {
  56. Debug.Clear();
  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. if (input[0].Debug && !input[1].Debug) {
  69. Debug.Enabled = !Debug.Enabled;
  70. }
  71. if (!paused) {
  72. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  73. Clock.AddModelTime(modelTime);
  74. List<Rectangle> collisionTargets = world.CollisionTargets();
  75. player.Update(modelTime, input, collisionTargets);
  76. camera.Update(player.Position);
  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. }