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.

103 lines
3.5 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Content;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace SemiColinGames {
  5. class Scene {
  6. public bool Enabled = false;
  7. Color backgroundColor = Color.CornflowerBlue;
  8. readonly GraphicsDevice graphics;
  9. readonly Camera camera;
  10. readonly RenderTarget2D sceneTarget;
  11. readonly RenderTarget2D lightingTarget;
  12. readonly BasicEffect lightingEffect;
  13. readonly SpriteBatch spriteBatch;
  14. public Scene(GraphicsDevice graphics, Camera camera) {
  15. this.graphics = graphics;
  16. this.camera = camera;
  17. sceneTarget = new RenderTarget2D(
  18. graphics, camera.Width, camera.Height, false /* mipmap */,
  19. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  20. lightingTarget = new RenderTarget2D(
  21. graphics, camera.Width, camera.Height, false /* mipmap */,
  22. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  23. lightingEffect = new BasicEffect(graphics) {
  24. World = Matrix.CreateTranslation(0, 0, 0),
  25. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  26. VertexColorEnabled = true
  27. };
  28. spriteBatch = new SpriteBatch(graphics);
  29. }
  30. public void Draw(World world, Player player, LinesOfSight linesOfSight) {
  31. graphics.SetRenderTarget(null);
  32. graphics.Clear(backgroundColor);
  33. if (!Enabled) {
  34. return;
  35. }
  36. graphics.SetRenderTarget(sceneTarget);
  37. graphics.Clear(backgroundColor);
  38. // Draw scene to sceneTarget.
  39. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  40. // Draw background.
  41. Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.5f));
  42. Rectangle bgSource = new Rectangle(
  43. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  44. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  45. spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend);
  46. bgSource = new Rectangle(
  47. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  48. spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend);
  49. spriteBatch.End();
  50. // Set up transformation matrix for drawing world objects.
  51. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  52. spriteBatch.Begin(
  53. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  54. // Draw player.
  55. player.Draw(spriteBatch);
  56. // Draw foreground tiles.
  57. world.Draw(spriteBatch);
  58. // Aaaaand we're done.
  59. spriteBatch.End();
  60. // Draw lighting to lightingTarget.
  61. graphics.SetRenderTarget(lightingTarget);
  62. graphics.Clear(new Color(0, 0, 0, 0f));
  63. lightingEffect.Projection = camera.Projection;
  64. linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect);
  65. // Draw debug rects & lines on top.
  66. Debug.Draw(graphics, lightingEffect);
  67. // Draw sceneTarget to screen.
  68. graphics.SetRenderTarget(null);
  69. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  70. SamplerState.PointClamp, DepthStencilState.Default,
  71. RasterizerState.CullNone);
  72. Rectangle drawRect = new Rectangle(
  73. 0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
  74. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  75. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  76. // Draw debug toasts.
  77. Debug.DrawToasts(spriteBatch);
  78. spriteBatch.End();
  79. }
  80. }
  81. }