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.

161 lines
5.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 Jumpy {
  7. public class JumpyGame : Game {
  8. GraphicsDeviceManager graphics;
  9. // TODO: use a History<RenderTarget2D> but implement functions that let us re-use the
  10. // RenderTargets instead of re-creating them every frame?
  11. const int numRenderTargets = 1;
  12. RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
  13. int renderTargetIdx = 0;
  14. SpriteBatch spriteBatch;
  15. SpriteFont font;
  16. bool fullScreen = false;
  17. IDisplay display;
  18. History<KeyboardState> keyboard = new History<KeyboardState>(2);
  19. History<GamePadState> gamePad = new History<GamePadState>(2);
  20. FpsCounter fpsCounter = new FpsCounter();
  21. Texture2D grasslandBg1;
  22. Texture2D grasslandBg2;
  23. Player player;
  24. World world;
  25. Camera camera = new Camera();
  26. public JumpyGame() {
  27. graphics = new GraphicsDeviceManager(this);
  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. for (int i = 0; i < renderTargets.Length; i++) {
  38. renderTargets[i] = new RenderTarget2D(
  39. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  40. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  41. }
  42. base.Initialize();
  43. }
  44. // Called once per game. Loads all game content.
  45. protected override void LoadContent() {
  46. spriteBatch = new SpriteBatch(GraphicsDevice);
  47. font = Content.Load<SpriteFont>("font");
  48. // TODO: decouple things like Player and World from their textures.
  49. player = new Player(Content.Load<Texture2D>("player_1x"));
  50. world = new World(Content.Load<Texture2D>("grassland"));
  51. // TODO: move backgrounds into World.
  52. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  53. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  54. }
  55. // Called once per game. Unloads all game content.
  56. protected override void UnloadContent() {
  57. // TODO: Unload any non ContentManager content here.
  58. }
  59. // Updates the game world.
  60. protected override void Update(GameTime gameTime) {
  61. Debug.Clear();
  62. gamePad.Add(GamePad.GetState(PlayerIndex.One));
  63. keyboard.Add(Keyboard.GetState());
  64. if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
  65. Exit();
  66. }
  67. if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
  68. keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
  69. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  70. fullScreen = !fullScreen;
  71. display.SetFullScreen(fullScreen);
  72. }
  73. if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
  74. keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
  75. Debug.Enabled = !Debug.Enabled;
  76. }
  77. List<Rectangle> collisionTargets = world.CollisionTargets();
  78. player.Update(gameTime, gamePad, keyboard, collisionTargets);
  79. camera.Update(gameTime, player.Position);
  80. base.Update(gameTime);
  81. }
  82. // Called when the game should draw itself.
  83. protected override void Draw(GameTime gameTime) {
  84. // We need to update the FPS counter in Draw() since Update() might get called more
  85. // frequently, especially when gameTime.IsRunningSlowly.
  86. fpsCounter.Update();
  87. // Draw scene to RenderTarget.
  88. RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
  89. renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
  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, gameTime);
  103. // Draw foreground tiles.
  104. world.Draw(spriteBatch, camera);
  105. // Draw debug rects.
  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. if (Debug.Enabled) {
  119. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  120. $"{fpsCounter.Fps} FPS";
  121. spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
  122. Debug.DrawToast(spriteBatch, font);
  123. }
  124. spriteBatch.End();
  125. base.Draw(gameTime);
  126. }
  127. }
  128. }