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.

149 lines
5.0 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace SemiColinGames {
  5. class Scene : IDisposable {
  6. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  7. public bool Enabled = false;
  8. Color backgroundColor = Color.CornflowerBlue;
  9. readonly GraphicsDevice graphics;
  10. readonly Camera camera;
  11. readonly RenderTarget2D sceneTarget;
  12. readonly BasicEffect basicEffect;
  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. basicEffect = new BasicEffect(graphics) {
  21. World = Matrix.CreateTranslation(0, 0, 0),
  22. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  23. VertexColorEnabled = true
  24. };
  25. spriteBatch = new SpriteBatch(graphics);
  26. }
  27. ~Scene() {
  28. Dispose();
  29. }
  30. public void Dispose() {
  31. sceneTarget.Dispose();
  32. basicEffect.Dispose();
  33. spriteBatch.Dispose();
  34. GC.SuppressFinalize(this);
  35. }
  36. public void Draw(World world, Player player, LinesOfSight linesOfSight, bool paused) {
  37. graphics.SetRenderTarget(null);
  38. graphics.Clear(backgroundColor);
  39. if (!Enabled) {
  40. return;
  41. }
  42. // Draw scene to sceneTarget.
  43. graphics.SetRenderTarget(sceneTarget);
  44. graphics.Clear(backgroundColor);
  45. // Draw parallax backgrounds.
  46. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  47. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  48. float xScale = 1f / 16;
  49. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  50. int yOffset = Textures.Backgrounds[i].Get.Height - camera.Height - 24;
  51. Rectangle bgSource = new Rectangle(
  52. (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
  53. spriteBatch.Draw(Textures.Backgrounds[i].Get, bgTarget, bgSource, Color.White);
  54. xScale *= 2;
  55. }
  56. spriteBatch.End();
  57. // Draw lines of sight.
  58. basicEffect.Projection = camera.Projection;
  59. if (Debug.Enabled) {
  60. linesOfSight.Draw(graphics, basicEffect);
  61. }
  62. // Set up transformation matrix for drawing world objects.
  63. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  64. spriteBatch.Begin(
  65. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  66. // Draw background tiles.
  67. world.DrawBackground(spriteBatch);
  68. // Draw player.
  69. player.Draw(spriteBatch);
  70. // Draw foreground tiles.
  71. world.DrawForeground(spriteBatch);
  72. spriteBatch.End();
  73. // Draw debug rects & lines on top.
  74. Debug.Draw(graphics, basicEffect);
  75. // Draw in-world UI on top of everything.
  76. // TODO: make a Text.cs file and move things like "black outlined text" there.
  77. if (paused) {
  78. string text = "Paused";
  79. Vector2 size = Textures.BannerFont.MeasureString(text);
  80. Vector2 position = new Vector2((camera.Width - size.X) / 2, (camera.Height - size.Y) / 2);
  81. spriteBatch.Begin(
  82. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, null);
  83. for (int i = -1; i <= 1; i++) {
  84. for (int j = -1; j <= 1; j++) {
  85. if (i == 0 && j == 0) {
  86. continue;
  87. }
  88. Vector2 stencilPos = new Vector2(position.X + i, position.Y + j);
  89. spriteBatch.DrawString(Textures.BannerFont, text, stencilPos, Color.Black);
  90. }
  91. }
  92. spriteBatch.DrawString(Textures.BannerFont, text, position, Color.White);
  93. spriteBatch.End();
  94. }
  95. // Get ready to draw sceneTarget to screen.
  96. graphics.SetRenderTarget(null);
  97. // Letterbox the scene if needed.
  98. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  99. Rectangle drawRect;
  100. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  101. // Need to letterbox the sides.
  102. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  103. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  104. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  105. } else {
  106. // Need to letterbox the top / bottom.
  107. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  108. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  109. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  110. }
  111. // Actually draw to screen.
  112. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  113. SamplerState.PointClamp, DepthStencilState.Default,
  114. RasterizerState.CullNone);
  115. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  116. // Draw debug toasts.
  117. Debug.DrawToasts(spriteBatch);
  118. spriteBatch.End();
  119. }
  120. }
  121. }