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.

282 lines
9.9 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. readonly Camera camera = new Camera();
  32. public SneakGame() {
  33. graphics = new GraphicsDeviceManager(this) {
  34. SynchronizeWithVerticalRetrace = true,
  35. GraphicsProfile = GraphicsProfile.HiDef
  36. };
  37. IsFixedTimeStep = true;
  38. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  39. IsMouseVisible = true;
  40. Content.RootDirectory = "Content";
  41. }
  42. // Performs initialization that's needed before starting to run.
  43. protected override void Initialize() {
  44. display = (IDisplay) Services.GetService(typeof(IDisplay));
  45. display.Initialize(Window, graphics);
  46. display.SetFullScreen(fullScreen);
  47. Debug.Initialize(GraphicsDevice);
  48. sceneTarget = new RenderTarget2D(
  49. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  50. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  51. lightingTarget = new RenderTarget2D(
  52. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  53. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  54. lightingEffect = new BasicEffect(GraphicsDevice);
  55. lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
  56. lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
  57. lightingEffect.VertexColorEnabled = true;
  58. RasterizerState rasterizerState = new RasterizerState() {
  59. CullMode = CullMode.None
  60. };
  61. GraphicsDevice.RasterizerState = rasterizerState;
  62. base.Initialize();
  63. }
  64. // Called once per game. Loads all game content.
  65. protected override void LoadContent() {
  66. spriteBatch = new SpriteBatch(GraphicsDevice);
  67. font = Content.Load<SpriteFont>("font");
  68. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  69. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  70. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  71. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  72. }
  73. // Called once per game. Unloads all game content.
  74. protected override void UnloadContent() {
  75. updateTimer.DumpStats();
  76. drawTimer.DumpStats();
  77. }
  78. // Updates the game world.
  79. protected override void Update(GameTime gameTime) {
  80. updateTimer.Start();
  81. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  82. if (input[0].Exit) {
  83. Exit();
  84. }
  85. if (input[0].Pause && !input[1].Pause) {
  86. paused = !paused;
  87. }
  88. if (input[0].FullScreen && !input[1].FullScreen) {
  89. fullScreen = !fullScreen;
  90. display.SetFullScreen(fullScreen);
  91. }
  92. Debug.Clear(paused);
  93. if (input[0].Debug && !input[1].Debug) {
  94. Debug.Enabled = !Debug.Enabled;
  95. }
  96. if (!paused) {
  97. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  98. Clock.AddModelTime(modelTime);
  99. player.Update(modelTime, input, world.CollisionTargets);
  100. camera.Update(player.Position, world.Width);
  101. }
  102. base.Update(gameTime);
  103. updateTimer.Stop();
  104. }
  105. // Called when the game should draw itself.
  106. protected override void Draw(GameTime gameTime) {
  107. drawTimer.Start();
  108. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  109. framesToSuppress--;
  110. }
  111. // We need to update the FPS counter in Draw() since Update() might get called more
  112. // frequently, especially when gameTime.IsRunningSlowly.
  113. fpsCounter.Update();
  114. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  115. $"{fpsCounter.Fps} FPS";
  116. if (paused) {
  117. fpsText += " (paused)";
  118. }
  119. Debug.SetFpsText(fpsText);
  120. // Draw scene to sceneTarget.
  121. GraphicsDevice.SetRenderTarget(sceneTarget);
  122. GraphicsDevice.Clear(Color.CornflowerBlue);
  123. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  124. // Draw background.
  125. Rectangle bgSource = new Rectangle(
  126. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  127. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  128. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  129. bgSource = new Rectangle(
  130. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  131. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  132. spriteBatch.End();
  133. // Set up transformation matrix for drawing world objects.
  134. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  135. spriteBatch.Begin(
  136. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  137. // Draw player.
  138. player.Draw(spriteBatch);
  139. // Draw foreground tiles.
  140. world.Draw(spriteBatch);
  141. // Draw debug rects & lines.
  142. Debug.Draw(spriteBatch);
  143. // Aaaaand we're done.
  144. spriteBatch.End();
  145. // Draw lighting to lightingTarget.
  146. GraphicsDevice.SetRenderTarget(lightingTarget);
  147. GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
  148. lightingEffect.Projection = camera.Projection;
  149. DrawFov();
  150. // Draw sceneTarget to screen.
  151. GraphicsDevice.SetRenderTarget(null);
  152. GraphicsDevice.Clear(Color.CornflowerBlue);
  153. if (framesToSuppress == 0) {
  154. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  155. SamplerState.PointClamp, DepthStencilState.Default,
  156. RasterizerState.CullNone);
  157. Rectangle drawRect = new Rectangle(
  158. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  159. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  160. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  161. // Draw debug toasts.
  162. Debug.DrawToasts(spriteBatch, font);
  163. spriteBatch.End();
  164. }
  165. base.Draw(gameTime);
  166. drawTimer.Stop();
  167. }
  168. private void DrawFov() {
  169. // TODO: DrawIndexedPrimitives
  170. Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
  171. Vector2 eyePos = player.EyePosition;
  172. int numConePoints = 60;
  173. // TODO: don't new[] every frame.
  174. VertexPositionColor[] conePoints = new VertexPositionColor[numConePoints];
  175. float visionRange = 150;
  176. float visionRangeSq = visionRange * visionRange;
  177. float fov = FMath.DegToRad(120);
  178. float fovStep = fov / (numConePoints - 1);
  179. Vector2 ray = new Vector2(visionRange * player.GetFacing, 0);
  180. if (player.GetPose == Player.Pose.Stretching) {
  181. ray = ray.Rotate(player.GetFacing * FMath.DegToRad(-30));
  182. }
  183. if (player.GetPose == Player.Pose.Crouching) {
  184. ray = ray.Rotate(player.GetFacing * FMath.DegToRad(30));
  185. }
  186. for (int i = 0; i < conePoints.Length; i++) {
  187. float angle = -fov / 2 + fovStep * i;
  188. Vector2 rotated = ray.Rotate(angle);
  189. Vector2 closestHit = Vector2.Add(eyePos, rotated);
  190. float hitTime = 1f;
  191. Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
  192. for (int j = 0; j < world.CollisionTargets.Length; j++) {
  193. AABB box = world.CollisionTargets[j];
  194. if (Math.Abs(box.Position.X - player.Position.X) > visionRange + halfTileSize.X) {
  195. continue;
  196. }
  197. Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
  198. if (delta.LengthSquared() > visionRangeSq) {
  199. continue;
  200. }
  201. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  202. if (maybeHit != null) {
  203. Hit hit = maybeHit.Value;
  204. if (hit.Time < hitTime) {
  205. hitTime = hit.Time;
  206. closestHit = hit.Position;
  207. }
  208. }
  209. }
  210. float tint = 0.6f - hitTime / 2;
  211. Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
  212. conePoints[i] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
  213. }
  214. // TODO: don't new[] every frame.
  215. VertexPositionColor[] vertices = new VertexPositionColor[numConePoints * 3];
  216. VertexPositionColor eyeVertex = new VertexPositionColor(new Vector3(eyePos, 0), color);
  217. for (int i = 0; i < numConePoints - 1; i++) {
  218. vertices[i * 3] = eyeVertex;
  219. vertices[i * 3 + 1] = conePoints[i];
  220. vertices[i * 3 + 2] = conePoints[i + 1];
  221. }
  222. VertexBuffer vertexBuffer = new VertexBuffer(
  223. GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
  224. vertexBuffer.SetData<VertexPositionColor>(vertices);
  225. GraphicsDevice.SetVertexBuffer(vertexBuffer);
  226. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  227. pass.Apply();
  228. GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
  229. }
  230. }
  231. }
  232. }