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.

128 lines
4.4 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. Player player;
  22. Texture2D grassland;
  23. public JumpyGame() {
  24. graphics = new GraphicsDeviceManager(this);
  25. IsMouseVisible = true;
  26. Content.RootDirectory = "Content";
  27. }
  28. // Performs initialization that's needed before starting to run.
  29. protected override void Initialize() {
  30. display = (IDisplay) Services.GetService(typeof(IDisplay));
  31. display.Initialize(Window, graphics);
  32. display.SetFullScreen(fullScreen);
  33. for (int i = 0; i < renderTargets.Length; i++) {
  34. renderTargets[i] = new RenderTarget2D(
  35. GraphicsDevice, Camera.Width, Camera.Height, false /* mipmap */,
  36. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  37. }
  38. base.Initialize();
  39. }
  40. // Called once per game. Loads all game content.
  41. protected override void LoadContent() {
  42. spriteBatch = new SpriteBatch(GraphicsDevice);
  43. font = Content.Load<SpriteFont>("font");
  44. player = new Player(Content.Load<Texture2D>("player_1x"));
  45. grassland = Content.Load<Texture2D>("grassland");
  46. }
  47. // Called once per game. Unloads all game content.
  48. protected override void UnloadContent() {
  49. // TODO: Unload any non ContentManager content here.
  50. }
  51. // Updates the game world.
  52. protected override void Update(GameTime gameTime) {
  53. gamePad.Add(GamePad.GetState(PlayerIndex.One));
  54. keyboard.Add(Keyboard.GetState());
  55. if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
  56. Exit();
  57. }
  58. if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
  59. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  60. fullScreen = !fullScreen;
  61. display.SetFullScreen(fullScreen);
  62. }
  63. player.Update(gameTime, gamePad);
  64. base.Update(gameTime);
  65. }
  66. // Called when the game should draw itself.
  67. protected override void Draw(GameTime gameTime) {
  68. // We need to update the FPS counter in Draw() since Update() might get called more
  69. // frequently, especially when gameTime.IsRunningSlowly.
  70. fpsCounter.Update();
  71. // Draw scene to RenderTarget.
  72. RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
  73. renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
  74. GraphicsDevice.SetRenderTarget(renderTarget);
  75. GraphicsDevice.Clear(Color.CornflowerBlue);
  76. spriteBatch.Begin();
  77. // Draw player.
  78. player.Draw(gameTime, spriteBatch);
  79. // Draw foreground tiles.
  80. int size = 16;
  81. Rectangle textureSource = new Rectangle(3 * size, 0 * size, size, size);
  82. for (int i = 0; i < Camera.Width / size; i++) {
  83. Vector2 drawPos = new Vector2(i * size, Camera.Height - size);
  84. spriteBatch.Draw(grassland, drawPos, textureSource, Color.White);
  85. }
  86. // Aaaaand we're done.
  87. spriteBatch.End();
  88. // Draw RenderTarget to screen.
  89. GraphicsDevice.SetRenderTarget(null);
  90. GraphicsDevice.Clear(Color.Black);
  91. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  92. SamplerState.PointClamp, DepthStencilState.Default,
  93. RasterizerState.CullNone);
  94. Rectangle drawRect = new Rectangle(
  95. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  96. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  97. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  98. $"{fpsCounter.Fps} FPS";
  99. spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.White);
  100. spriteBatch.End();
  101. base.Draw(gameTime);
  102. }
  103. }
  104. }