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