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.

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