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
5.2 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. public JumpyGame() {
  26. graphics = new GraphicsDeviceManager(this);
  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. for (int i = 0; i < renderTargets.Length; i++) {
  37. renderTargets[i] = new RenderTarget2D(
  38. GraphicsDevice, Camera.Width, Camera.Height, false /* mipmap */,
  39. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  40. }
  41. base.Initialize();
  42. }
  43. // Called once per game. Loads all game content.
  44. protected override void LoadContent() {
  45. spriteBatch = new SpriteBatch(GraphicsDevice);
  46. font = Content.Load<SpriteFont>("font");
  47. // TODO: decouple things like Player and World from their textures.
  48. player = new Player(Content.Load<Texture2D>("player_1x"));
  49. world = new World(Content.Load<Texture2D>("grassland"));
  50. // TODO: move backgrounds into World.
  51. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  52. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  53. }
  54. // Called once per game. Unloads all game content.
  55. protected override void UnloadContent() {
  56. // TODO: Unload any non ContentManager content here.
  57. }
  58. // Updates the game world.
  59. protected override void Update(GameTime gameTime) {
  60. Debug.Clear();
  61. gamePad.Add(GamePad.GetState(PlayerIndex.One));
  62. keyboard.Add(Keyboard.GetState());
  63. if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
  64. Exit();
  65. }
  66. if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
  67. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  68. fullScreen = !fullScreen;
  69. display.SetFullScreen(fullScreen);
  70. }
  71. if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
  72. gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
  73. Debug.Enabled = !Debug.Enabled;
  74. }
  75. List<Rectangle> collisionTargets = world.CollisionTargets();
  76. player.Update(gameTime, gamePad, collisionTargets);
  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. // Draw scene to RenderTarget.
  85. RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
  86. renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
  87. GraphicsDevice.SetRenderTarget(renderTarget);
  88. GraphicsDevice.Clear(Color.CornflowerBlue);
  89. spriteBatch.Begin();
  90. // Draw background.
  91. Rectangle bgSource = new Rectangle(
  92. 0, grasslandBg1.Height - Camera.Height, Camera.Width, Camera.Height);
  93. Rectangle bgTarget = new Rectangle(0, 0, Camera.Width, Camera.Height);
  94. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  95. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  96. // Draw player.
  97. player.Draw(gameTime, spriteBatch);
  98. // Draw foreground tiles.
  99. world.Draw(spriteBatch);
  100. // Draw debug rects.
  101. Debug.Draw(spriteBatch);
  102. // Aaaaand we're done.
  103. spriteBatch.End();
  104. // Draw RenderTarget to screen.
  105. GraphicsDevice.SetRenderTarget(null);
  106. GraphicsDevice.Clear(Color.Black);
  107. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  108. SamplerState.PointClamp, DepthStencilState.Default,
  109. RasterizerState.CullNone);
  110. Rectangle drawRect = new Rectangle(
  111. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  112. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  113. if (Debug.Enabled) {
  114. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  115. $"{fpsCounter.Fps} FPS";
  116. spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.White);
  117. }
  118. spriteBatch.End();
  119. base.Draw(gameTime);
  120. }
  121. }
  122. }