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.

110 lines
3.9 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. Color backgroundColor = Color.CornflowerBlue;
  7. readonly GraphicsDevice graphics;
  8. readonly Camera camera;
  9. readonly RenderTarget2D sceneTarget;
  10. readonly RenderTarget2D lightingTarget;
  11. readonly BasicEffect lightingEffect;
  12. readonly SpriteBatch spriteBatch;
  13. readonly SpriteFont font;
  14. readonly Texture2D grasslandBg1;
  15. readonly Texture2D grasslandBg2;
  16. public Scene(GraphicsDevice graphics, Camera camera, ContentManager content) {
  17. Enabled = false;
  18. this.graphics = graphics;
  19. this.camera = camera;
  20. sceneTarget = new RenderTarget2D(
  21. graphics, camera.Width, camera.Height, false /* mipmap */,
  22. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  23. lightingTarget = new RenderTarget2D(
  24. graphics, camera.Width, camera.Height, false /* mipmap */,
  25. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  26. lightingEffect = new BasicEffect(graphics);
  27. lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
  28. lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
  29. lightingEffect.VertexColorEnabled = true;
  30. // TODO: handle unloading of resources when the level is done.
  31. spriteBatch = new SpriteBatch(graphics);
  32. font = content.Load<SpriteFont>("font");
  33. grasslandBg1 = content.Load<Texture2D>("grassland_bg1");
  34. grasslandBg2 = content.Load<Texture2D>("grassland_bg2");
  35. }
  36. public bool Enabled { get; set; }
  37. public void Draw(World world, Player player, LinesOfSight linesOfSight) {
  38. graphics.SetRenderTarget(null);
  39. graphics.Clear(backgroundColor);
  40. if (!Enabled) {
  41. return;
  42. }
  43. graphics.SetRenderTarget(sceneTarget);
  44. graphics.Clear(backgroundColor);
  45. // Draw scene to sceneTarget.
  46. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  47. // Draw background.
  48. Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.5f));
  49. Rectangle bgSource = new Rectangle(
  50. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  51. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  52. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, bgBlend);
  53. bgSource = new Rectangle(
  54. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  55. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, bgBlend);
  56. spriteBatch.End();
  57. // Set up transformation matrix for drawing world objects.
  58. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  59. spriteBatch.Begin(
  60. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  61. // Draw foreground tiles.
  62. world.Draw(spriteBatch);
  63. // Draw player.
  64. player.Draw(spriteBatch);
  65. // Aaaaand we're done.
  66. spriteBatch.End();
  67. // Draw lighting to lightingTarget.
  68. graphics.SetRenderTarget(lightingTarget);
  69. graphics.Clear(new Color(0, 0, 0, 0f));
  70. lightingEffect.Projection = camera.Projection;
  71. linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect);
  72. // Draw debug rects & lines on top.
  73. Debug.Draw(graphics, lightingEffect);
  74. // Draw sceneTarget to screen.
  75. graphics.SetRenderTarget(null);
  76. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  77. SamplerState.PointClamp, DepthStencilState.Default,
  78. RasterizerState.CullNone);
  79. Rectangle drawRect = new Rectangle(
  80. 0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
  81. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  82. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  83. // Draw debug toasts.
  84. Debug.DrawToasts(spriteBatch, font);
  85. spriteBatch.End();
  86. }
  87. }
  88. }