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.

223 lines
7.6 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. const int TARGET_FPS = 60;
  9. const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
  10. readonly GraphicsDeviceManager graphics;
  11. RenderTarget2D sceneTarget;
  12. RenderTarget2D lightingTarget;
  13. BasicEffect lightingEffect;
  14. SpriteBatch spriteBatch;
  15. SpriteFont font;
  16. bool fullScreen = false;
  17. bool paused = false;
  18. IDisplay display;
  19. readonly History<Input> input = new History<Input>(2);
  20. readonly FpsCounter fpsCounter = new FpsCounter();
  21. readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
  22. readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
  23. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  24. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  25. // frames can be really slow to draw.
  26. int framesToSuppress = 2;
  27. Texture2D grasslandBg1;
  28. Texture2D grasslandBg2;
  29. Texture2D whiteTexture;
  30. Player player;
  31. World world;
  32. LinesOfSight linesOfSight;
  33. readonly Camera camera = new Camera();
  34. public SneakGame() {
  35. graphics = new GraphicsDeviceManager(this) {
  36. SynchronizeWithVerticalRetrace = true,
  37. GraphicsProfile = GraphicsProfile.HiDef
  38. };
  39. IsFixedTimeStep = true;
  40. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  41. IsMouseVisible = true;
  42. Content.RootDirectory = "Content";
  43. }
  44. // Performs initialization that's needed before starting to run.
  45. protected override void Initialize() {
  46. display = (IDisplay) Services.GetService(typeof(IDisplay));
  47. display.Initialize(Window, graphics);
  48. display.SetFullScreen(fullScreen);
  49. sceneTarget = new RenderTarget2D(
  50. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  51. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  52. lightingTarget = new RenderTarget2D(
  53. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  54. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  55. lightingEffect = new BasicEffect(GraphicsDevice);
  56. lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
  57. lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
  58. lightingEffect.VertexColorEnabled = true;
  59. RasterizerState rasterizerState = new RasterizerState() {
  60. CullMode = CullMode.None
  61. };
  62. GraphicsDevice.RasterizerState = rasterizerState;
  63. base.Initialize();
  64. }
  65. // Called once per game. Loads all game content.
  66. protected override void LoadContent() {
  67. spriteBatch = new SpriteBatch(GraphicsDevice);
  68. font = Content.Load<SpriteFont>("font");
  69. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  70. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  71. linesOfSight = new LinesOfSight(GraphicsDevice);
  72. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  73. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  74. whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
  75. whiteTexture.SetData(new Color[] { Color.White });
  76. Debug.Initialize(GraphicsDevice, whiteTexture);
  77. }
  78. // Called once per game. Unloads all game content.
  79. protected override void UnloadContent() {
  80. whiteTexture.Dispose();
  81. updateTimer.DumpStats();
  82. drawTimer.DumpStats();
  83. base.UnloadContent();
  84. }
  85. // Updates the game world.
  86. protected override void Update(GameTime gameTime) {
  87. updateTimer.Start();
  88. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  89. if (input[0].Exit) {
  90. Exit();
  91. }
  92. if (input[0].Pause && !input[1].Pause) {
  93. paused = !paused;
  94. }
  95. if (input[0].FullScreen && !input[1].FullScreen) {
  96. fullScreen = !fullScreen;
  97. display.SetFullScreen(fullScreen);
  98. }
  99. Debug.Clear(paused);
  100. if (input[0].Debug && !input[1].Debug) {
  101. Debug.Enabled = !Debug.Enabled;
  102. }
  103. if (!paused) {
  104. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  105. Clock.AddModelTime(modelTime);
  106. player.Update(modelTime, input, world.CollisionTargets);
  107. linesOfSight.Update(player, world.CollisionTargets);
  108. camera.Update(player.Position, world.Width);
  109. }
  110. base.Update(gameTime);
  111. updateTimer.Stop();
  112. }
  113. // Called when the game should draw itself.
  114. protected override void Draw(GameTime gameTime) {
  115. drawTimer.Start();
  116. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  117. framesToSuppress--;
  118. }
  119. // We need to update the FPS counter in Draw() since Update() might get called more
  120. // frequently, especially when gameTime.IsRunningSlowly.
  121. fpsCounter.Update();
  122. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  123. $"{fpsCounter.Fps} FPS";
  124. if (paused) {
  125. fpsText += " (paused)";
  126. }
  127. Debug.SetFpsText(fpsText);
  128. // Draw scene to sceneTarget.
  129. GraphicsDevice.SetRenderTarget(sceneTarget);
  130. GraphicsDevice.Clear(Color.CornflowerBlue);
  131. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  132. // Draw background.
  133. Rectangle bgSource = new Rectangle(
  134. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  135. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  136. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  137. bgSource = new Rectangle(
  138. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  139. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  140. spriteBatch.End();
  141. // Set up transformation matrix for drawing world objects.
  142. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  143. spriteBatch.Begin(
  144. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  145. // Draw player.
  146. player.Draw(spriteBatch);
  147. // Draw foreground tiles.
  148. world.Draw(spriteBatch);
  149. // Aaaaand we're done.
  150. spriteBatch.End();
  151. // Draw lighting to lightingTarget.
  152. GraphicsDevice.SetRenderTarget(lightingTarget);
  153. GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
  154. lightingEffect.Projection = camera.Projection;
  155. linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
  156. // Draw debug rects & lines on top.
  157. spriteBatch.Begin(
  158. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  159. Debug.Draw(spriteBatch, GraphicsDevice, lightingEffect);
  160. spriteBatch.End();
  161. // Draw sceneTarget to screen.
  162. GraphicsDevice.SetRenderTarget(null);
  163. GraphicsDevice.Clear(Color.CornflowerBlue);
  164. if (framesToSuppress == 0) {
  165. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  166. SamplerState.PointClamp, DepthStencilState.Default,
  167. RasterizerState.CullNone);
  168. Rectangle drawRect = new Rectangle(
  169. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  170. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  171. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  172. // Draw debug toasts.
  173. Debug.DrawToasts(spriteBatch, font);
  174. spriteBatch.End();
  175. }
  176. base.Draw(gameTime);
  177. drawTimer.Stop();
  178. }
  179. }
  180. }