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.

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