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.

102 lines
3.6 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. lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
  25. lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
  26. lightingEffect.VertexColorEnabled = true;
  27. spriteBatch = new SpriteBatch(graphics);
  28. }
  29. public void Draw(World world, Player player, LinesOfSight linesOfSight) {
  30. graphics.SetRenderTarget(null);
  31. graphics.Clear(backgroundColor);
  32. if (!Enabled) {
  33. return;
  34. }
  35. graphics.SetRenderTarget(sceneTarget);
  36. graphics.Clear(backgroundColor);
  37. // Draw scene to sceneTarget.
  38. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  39. // Draw background.
  40. Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.5f));
  41. Rectangle bgSource = new Rectangle(
  42. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  43. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  44. spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend);
  45. bgSource = new Rectangle(
  46. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  47. spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend);
  48. spriteBatch.End();
  49. // Set up transformation matrix for drawing world objects.
  50. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  51. spriteBatch.Begin(
  52. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  53. // Draw player.
  54. player.Draw(spriteBatch);
  55. // Draw foreground tiles.
  56. world.Draw(spriteBatch);
  57. // Aaaaand we're done.
  58. spriteBatch.End();
  59. // Draw lighting to lightingTarget.
  60. graphics.SetRenderTarget(lightingTarget);
  61. graphics.Clear(new Color(0, 0, 0, 0f));
  62. lightingEffect.Projection = camera.Projection;
  63. linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect);
  64. // Draw debug rects & lines on top.
  65. Debug.Draw(graphics, lightingEffect);
  66. // Draw sceneTarget to screen.
  67. graphics.SetRenderTarget(null);
  68. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  69. SamplerState.PointClamp, DepthStencilState.Default,
  70. RasterizerState.CullNone);
  71. Rectangle drawRect = new Rectangle(
  72. 0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
  73. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  74. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  75. // Draw debug toasts.
  76. Debug.DrawToasts(spriteBatch);
  77. spriteBatch.End();
  78. }
  79. }
  80. }