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.

116 lines
3.7 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace SemiColinGames {
  5. class Scene : IDisposable {
  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. ~Scene() {
  31. Dispose();
  32. }
  33. public void Dispose() {
  34. sceneTarget.Dispose();
  35. lightingTarget.Dispose();
  36. lightingEffect.Dispose();
  37. spriteBatch.Dispose();
  38. }
  39. public void Draw(World world, Player player, LinesOfSight linesOfSight) {
  40. graphics.SetRenderTarget(null);
  41. graphics.Clear(backgroundColor);
  42. if (!Enabled) {
  43. return;
  44. }
  45. // Draw scene to sceneTarget.
  46. graphics.SetRenderTarget(sceneTarget);
  47. graphics.Clear(backgroundColor);
  48. // Draw background.
  49. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  50. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  51. float xScale = 1f / 16;
  52. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  53. int yOffset = Textures.Backgrounds[i].Height - camera.Height - 24;
  54. Rectangle bgSource = new Rectangle(
  55. (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
  56. spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White);
  57. xScale *= 2;
  58. }
  59. spriteBatch.End();
  60. // Set up transformation matrix for drawing world objects.
  61. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  62. spriteBatch.Begin(
  63. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  64. // Draw player.
  65. player.Draw(spriteBatch);
  66. // Draw foreground tiles.
  67. world.Draw(spriteBatch);
  68. // Aaaaand we're done.
  69. spriteBatch.End();
  70. // Draw lighting to lightingTarget.
  71. graphics.SetRenderTarget(lightingTarget);
  72. graphics.Clear(new Color(0, 0, 0, 0f));
  73. lightingEffect.Projection = camera.Projection;
  74. if (Debug.Enabled) {
  75. linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect);
  76. }
  77. // Draw debug rects & lines on top.
  78. Debug.Draw(graphics, lightingEffect);
  79. // Draw sceneTarget to screen.
  80. graphics.SetRenderTarget(null);
  81. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  82. SamplerState.PointClamp, DepthStencilState.Default,
  83. RasterizerState.CullNone);
  84. Rectangle drawRect = new Rectangle(
  85. 0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
  86. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  87. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  88. // Draw debug toasts.
  89. Debug.DrawToasts(spriteBatch);
  90. spriteBatch.End();
  91. }
  92. }
  93. }