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.

152 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 SemiColinGames {
  7. public class SneakGame : Game {
  8. GraphicsDeviceManager graphics;
  9. RenderTarget2D renderTarget;
  10. SpriteBatch spriteBatch;
  11. SpriteFont font;
  12. bool fullScreen = false;
  13. IDisplay display;
  14. History<KeyboardState> keyboard = new History<KeyboardState>(2);
  15. History<GamePadState> gamePad = new History<GamePadState>(2);
  16. FpsCounter fpsCounter = new FpsCounter();
  17. Texture2D grasslandBg1;
  18. Texture2D grasslandBg2;
  19. Player player;
  20. World world;
  21. Camera camera = new Camera();
  22. public SneakGame() {
  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. Debug.Initialize(GraphicsDevice);
  33. renderTarget = new RenderTarget2D(
  34. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  35. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  36. base.Initialize();
  37. }
  38. // Called once per game. Loads all game content.
  39. protected override void LoadContent() {
  40. spriteBatch = new SpriteBatch(GraphicsDevice);
  41. font = Content.Load<SpriteFont>("font");
  42. // TODO: decouple things like Player and World from their textures.
  43. player = new Player(Content.Load<Texture2D>("player_1x"));
  44. world = new World(Content.Load<Texture2D>("grassland"));
  45. // TODO: move backgrounds into World.
  46. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  47. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  48. }
  49. // Called once per game. Unloads all game content.
  50. protected override void UnloadContent() {
  51. // TODO: Unload any non ContentManager content here.
  52. }
  53. // Updates the game world.
  54. protected override void Update(GameTime gameTime) {
  55. Debug.Clear();
  56. gamePad.Add(GamePad.GetState(PlayerIndex.One));
  57. keyboard.Add(Keyboard.GetState());
  58. if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
  59. Exit();
  60. }
  61. if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
  62. keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
  63. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  64. fullScreen = !fullScreen;
  65. display.SetFullScreen(fullScreen);
  66. }
  67. if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
  68. keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
  69. Debug.Enabled = !Debug.Enabled;
  70. }
  71. List<Rectangle> collisionTargets = world.CollisionTargets();
  72. player.Update(gameTime, gamePad, keyboard, collisionTargets);
  73. camera.Update(gameTime, player.Position);
  74. base.Update(gameTime);
  75. }
  76. // Called when the game should draw itself.
  77. protected override void Draw(GameTime gameTime) {
  78. // We need to update the FPS counter in Draw() since Update() might get called more
  79. // frequently, especially when gameTime.IsRunningSlowly.
  80. fpsCounter.Update();
  81. // Draw scene to RenderTarget.
  82. GraphicsDevice.SetRenderTarget(renderTarget);
  83. GraphicsDevice.Clear(Color.CornflowerBlue);
  84. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  85. // Draw background.
  86. Rectangle bgSource = new Rectangle(
  87. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  88. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  89. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  90. bgSource = new Rectangle(
  91. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  92. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  93. // Draw player.
  94. player.Draw(spriteBatch, camera, gameTime);
  95. // Draw foreground tiles.
  96. world.Draw(spriteBatch, camera);
  97. // Draw debug rects.
  98. Debug.Draw(spriteBatch, camera);
  99. // Aaaaand we're done.
  100. spriteBatch.End();
  101. // Draw RenderTarget to screen.
  102. GraphicsDevice.SetRenderTarget(null);
  103. GraphicsDevice.Clear(Color.Black);
  104. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  105. SamplerState.PointClamp, DepthStencilState.Default,
  106. RasterizerState.CullNone);
  107. Rectangle drawRect = new Rectangle(
  108. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  109. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  110. if (Debug.Enabled) {
  111. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  112. $"{fpsCounter.Fps} FPS";
  113. spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
  114. Debug.DrawToast(spriteBatch, font);
  115. }
  116. spriteBatch.End();
  117. base.Draw(gameTime);
  118. }
  119. }
  120. }