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.

151 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. Console.WriteLine("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. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  69. fullScreen = !fullScreen;
  70. display.SetFullScreen(fullScreen);
  71. }
  72. if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
  73. gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
  74. Debug.Enabled = !Debug.Enabled;
  75. }
  76. List<Rectangle> collisionTargets = world.CollisionTargets();
  77. player.Update(gameTime, gamePad, collisionTargets);
  78. base.Update(gameTime);
  79. }
  80. // Called when the game should draw itself.
  81. protected override void Draw(GameTime gameTime) {
  82. // We need to update the FPS counter in Draw() since Update() might get called more
  83. // frequently, especially when gameTime.IsRunningSlowly.
  84. fpsCounter.Update();
  85. // Draw scene to RenderTarget.
  86. RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
  87. renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
  88. GraphicsDevice.SetRenderTarget(renderTarget);
  89. GraphicsDevice.Clear(Color.CornflowerBlue);
  90. spriteBatch.Begin();
  91. // Draw background.
  92. Rectangle bgSource = new Rectangle(
  93. 0, grasslandBg1.Height - Camera.Height, Camera.Width, Camera.Height);
  94. Rectangle bgTarget = new Rectangle(0, 0, Camera.Width, Camera.Height);
  95. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  96. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  97. // Draw player.
  98. player.Draw(gameTime, spriteBatch);
  99. // Draw foreground tiles.
  100. world.Draw(spriteBatch);
  101. // Draw debug rects.
  102. Debug.Draw(spriteBatch);
  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. 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. spriteBatch.End();
  118. base.Draw(gameTime);
  119. }
  120. }
  121. }