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.

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