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.

150 lines
5.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 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. player = new Player(Content.Load<Texture2D>("player_1x"));
  43. world = new World(Content.Load<Texture2D>("grassland"));
  44. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  45. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  46. }
  47. // Called once per game. Unloads all game content.
  48. protected override void UnloadContent() {
  49. }
  50. // Updates the game world.
  51. protected override void Update(GameTime gameTime) {
  52. Debug.Clear();
  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. keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
  60. gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
  61. fullScreen = !fullScreen;
  62. display.SetFullScreen(fullScreen);
  63. }
  64. if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
  65. keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
  66. Debug.Enabled = !Debug.Enabled;
  67. }
  68. List<Rectangle> collisionTargets = world.CollisionTargets();
  69. player.Update(gameTime, gamePad, keyboard, collisionTargets);
  70. camera.Update(gameTime, player.Position);
  71. base.Update(gameTime);
  72. }
  73. // Called when the game should draw itself.
  74. protected override void Draw(GameTime gameTime) {
  75. // We need to update the FPS counter in Draw() since Update() might get called more
  76. // frequently, especially when gameTime.IsRunningSlowly.
  77. fpsCounter.Update();
  78. // Draw scene to RenderTarget.
  79. GraphicsDevice.SetRenderTarget(renderTarget);
  80. GraphicsDevice.Clear(Color.CornflowerBlue);
  81. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  82. // Draw background.
  83. Rectangle bgSource = new Rectangle(
  84. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  85. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  86. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  87. bgSource = new Rectangle(
  88. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  89. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  90. // Draw player.
  91. player.Draw(spriteBatch, camera, gameTime);
  92. // Draw foreground tiles.
  93. world.Draw(spriteBatch, camera);
  94. // Draw debug rects.
  95. Debug.Draw(spriteBatch, camera);
  96. // Aaaaand we're done.
  97. spriteBatch.End();
  98. // Draw RenderTarget to screen.
  99. GraphicsDevice.SetRenderTarget(null);
  100. GraphicsDevice.Clear(Color.Black);
  101. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  102. SamplerState.PointClamp, DepthStencilState.Default,
  103. RasterizerState.CullNone);
  104. Rectangle drawRect = new Rectangle(
  105. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  106. spriteBatch.Draw(renderTarget, drawRect, Color.White);
  107. if (Debug.Enabled) {
  108. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  109. $"{fpsCounter.Fps} FPS";
  110. spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
  111. Debug.DrawToast(spriteBatch, font);
  112. }
  113. spriteBatch.End();
  114. base.Draw(gameTime);
  115. }
  116. }
  117. }