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.4 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 BasicEffect basicEffect;
  12. readonly SpriteBatch spriteBatch;
  13. public Scene(GraphicsDevice graphics, Camera camera) {
  14. this.graphics = graphics;
  15. this.camera = camera;
  16. sceneTarget = new RenderTarget2D(
  17. graphics, camera.Width, camera.Height, false /* mipmap */,
  18. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  19. basicEffect = new BasicEffect(graphics) {
  20. World = Matrix.CreateTranslation(0, 0, 0),
  21. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  22. VertexColorEnabled = true
  23. };
  24. spriteBatch = new SpriteBatch(graphics);
  25. }
  26. ~Scene() {
  27. Dispose();
  28. }
  29. public void Dispose() {
  30. sceneTarget.Dispose();
  31. basicEffect.Dispose();
  32. spriteBatch.Dispose();
  33. GC.SuppressFinalize(this);
  34. }
  35. public void Draw(World world, Player player, LinesOfSight linesOfSight) {
  36. graphics.SetRenderTarget(null);
  37. graphics.Clear(backgroundColor);
  38. if (!Enabled) {
  39. return;
  40. }
  41. // Draw scene to sceneTarget.
  42. graphics.SetRenderTarget(sceneTarget);
  43. graphics.Clear(backgroundColor);
  44. // Draw parallax backgrounds.
  45. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  46. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  47. float xScale = 1f / 16;
  48. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  49. int yOffset = Textures.Backgrounds[i].Height - camera.Height - 24;
  50. Rectangle bgSource = new Rectangle(
  51. (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
  52. spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White);
  53. xScale *= 2;
  54. }
  55. spriteBatch.End();
  56. // Draw lines of sight.
  57. basicEffect.Projection = camera.Projection;
  58. if (Debug.Enabled) {
  59. linesOfSight.Draw(graphics, basicEffect);
  60. }
  61. // Set up transformation matrix for drawing world objects.
  62. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  63. // Draw player.
  64. spriteBatch.Begin(
  65. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  66. player.Draw(spriteBatch);
  67. spriteBatch.End();
  68. // Draw foreground tiles.
  69. spriteBatch.Begin(
  70. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  71. world.Draw(spriteBatch);
  72. spriteBatch.End();
  73. // Draw debug rects & lines on top.
  74. Debug.Draw(graphics, basicEffect);
  75. // Draw sceneTarget to screen.
  76. graphics.SetRenderTarget(null);
  77. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  78. SamplerState.PointClamp, DepthStencilState.Default,
  79. RasterizerState.CullNone);
  80. Rectangle drawRect = new Rectangle(
  81. 0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
  82. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  83. // Draw debug toasts.
  84. Debug.DrawToasts(spriteBatch);
  85. spriteBatch.End();
  86. }
  87. }
  88. }