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.

158 lines
4.9 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. graphics.SynchronizeWithVerticalRetrace = false;
  25. IsFixedTimeStep = true;
  26. TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 120);
  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. List<Rectangle> collisionTargets = world.CollisionTargets();
  73. player.Update(modelTime, input, collisionTargets);
  74. camera.Update(player.Position);
  75. }
  76. base.Update(gameTime);
  77. }
  78. // Called when the game should draw itself.
  79. protected override void Draw(GameTime gameTime) {
  80. // We need to update the FPS counter in Draw() since Update() might get called more
  81. // frequently, especially when gameTime.IsRunningSlowly.
  82. fpsCounter.Update();
  83. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  84. $"{fpsCounter.Fps} FPS";
  85. if (paused) {
  86. fpsText = fpsText + " (paused)";
  87. }
  88. Debug.SetFpsText(fpsText);
  89. // Draw scene to RenderTarget.
  90. GraphicsDevice.SetRenderTarget(renderTarget);
  91. GraphicsDevice.Clear(Color.CornflowerBlue);
  92. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  93. // Draw background.
  94. Rectangle bgSource = new Rectangle(
  95. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  96. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  97. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  98. bgSource = new Rectangle(
  99. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  100. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  101. // Draw player.
  102. player.Draw(spriteBatch, camera);
  103. // Draw foreground tiles.
  104. world.Draw(spriteBatch, camera);
  105. // Draw debug rects & lines.
  106. Debug.Draw(spriteBatch, camera);
  107. // Aaaaand we're done.
  108. spriteBatch.End();
  109. // Draw RenderTarget to screen.
  110. GraphicsDevice.SetRenderTarget(null);
  111. GraphicsDevice.Clear(Color.Black);
  112. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  113. SamplerState.PointClamp, DepthStencilState.Default,
  114. RasterizerState.CullNone);
  115. Rectangle drawRect = new Rectangle(
  116. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  117. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  118. // Draw debug toasts.
  119. Debug.DrawToasts(spriteBatch, font);
  120. spriteBatch.End();
  121. base.Draw(gameTime);
  122. }
  123. }
  124. }