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.

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