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.5 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. Player player;
  30. World world;
  31. LinesOfSight linesOfSight;
  32. Camera camera = new Camera();
  33. public SneakGame() {
  34. graphics = new GraphicsDeviceManager(this) {
  35. SynchronizeWithVerticalRetrace = true,
  36. GraphicsProfile = GraphicsProfile.HiDef
  37. };
  38. IsFixedTimeStep = true;
  39. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  40. IsMouseVisible = true;
  41. Content.RootDirectory = "Content";
  42. }
  43. // Performs initialization that's needed before starting to run.
  44. protected override void Initialize() {
  45. display = (IDisplay) Services.GetService(typeof(IDisplay));
  46. display.Initialize(Window, graphics);
  47. display.SetFullScreen(fullScreen);
  48. Debug.Initialize(GraphicsDevice);
  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. base.LoadContent();
  68. spriteBatch = new SpriteBatch(GraphicsDevice);
  69. font = Content.Load<SpriteFont>("font");
  70. linesOfSight = new LinesOfSight(GraphicsDevice);
  71. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  72. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  73. LoadLevel();
  74. }
  75. private void LoadLevel() {
  76. camera = new Camera();
  77. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  78. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  79. }
  80. // Called once per game. Unloads all game content.
  81. protected override void UnloadContent() {
  82. base.UnloadContent();
  83. updateTimer.DumpStats();
  84. drawTimer.DumpStats();
  85. }
  86. // Updates the game world.
  87. protected override void Update(GameTime gameTime) {
  88. updateTimer.Start();
  89. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  90. if (input[0].Exit) {
  91. Exit();
  92. }
  93. if (input[0].Pause && !input[1].Pause) {
  94. paused = !paused;
  95. }
  96. if (input[0].FullScreen && !input[1].FullScreen) {
  97. fullScreen = !fullScreen;
  98. display.SetFullScreen(fullScreen);
  99. }
  100. if (input[0].Restart && !input[1].Restart) {
  101. LoadLevel();
  102. }
  103. Debug.Clear(paused);
  104. if (input[0].Debug && !input[1].Debug) {
  105. Debug.Enabled = !Debug.Enabled;
  106. }
  107. if (!paused) {
  108. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  109. Clock.AddModelTime(modelTime);
  110. player.Update(modelTime, input, world.CollisionTargets);
  111. linesOfSight.Update(player, world.CollisionTargets);
  112. camera.Update(player.Position, world.Width);
  113. }
  114. base.Update(gameTime);
  115. updateTimer.Stop();
  116. }
  117. // Called when the game should draw itself.
  118. protected override void Draw(GameTime gameTime) {
  119. drawTimer.Start();
  120. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  121. framesToSuppress--;
  122. }
  123. // We need to update the FPS counter in Draw() since Update() might get called more
  124. // frequently, especially when gameTime.IsRunningSlowly.
  125. fpsCounter.Update();
  126. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  127. $"{fpsCounter.Fps} FPS";
  128. if (paused) {
  129. fpsText += " (paused)";
  130. }
  131. Debug.SetFpsText(fpsText);
  132. // Draw scene to sceneTarget.
  133. GraphicsDevice.SetRenderTarget(sceneTarget);
  134. GraphicsDevice.Clear(Color.CornflowerBlue);
  135. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  136. // Draw background.
  137. Rectangle bgSource = new Rectangle(
  138. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  139. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  140. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  141. bgSource = new Rectangle(
  142. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  143. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  144. spriteBatch.End();
  145. // Set up transformation matrix for drawing world objects.
  146. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  147. spriteBatch.Begin(
  148. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  149. // Draw player.
  150. player.Draw(spriteBatch);
  151. // Draw foreground tiles.
  152. world.Draw(spriteBatch);
  153. // Aaaaand we're done.
  154. spriteBatch.End();
  155. // Draw lighting to lightingTarget.
  156. GraphicsDevice.SetRenderTarget(lightingTarget);
  157. GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
  158. lightingEffect.Projection = camera.Projection;
  159. linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
  160. // Draw debug rects & lines on top.
  161. Debug.Draw(GraphicsDevice, lightingEffect);
  162. // Draw sceneTarget to screen.
  163. GraphicsDevice.SetRenderTarget(null);
  164. GraphicsDevice.Clear(Color.CornflowerBlue);
  165. if (framesToSuppress == 0) {
  166. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  167. SamplerState.PointClamp, DepthStencilState.Default,
  168. RasterizerState.CullNone);
  169. Rectangle drawRect = new Rectangle(
  170. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  171. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  172. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  173. // Draw debug toasts.
  174. Debug.DrawToasts(spriteBatch, font);
  175. spriteBatch.End();
  176. }
  177. base.Draw(gameTime);
  178. drawTimer.Stop();
  179. }
  180. }
  181. }