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.

164 lines
5.7 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using System;
  5. namespace SemiColinGames {
  6. public sealed class Scene : IDisposable {
  7. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  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. readonly SoundEffectInstance music;
  15. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  16. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  17. // frames can be really slow to draw.
  18. int framesToSuppress = 2;
  19. public Scene(GraphicsDevice graphics, Camera camera) {
  20. this.graphics = graphics;
  21. this.camera = camera;
  22. sceneTarget = new RenderTarget2D(
  23. graphics, camera.Width, camera.Height, false /* mipmap */,
  24. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  25. basicEffect = new BasicEffect(graphics) {
  26. World = Matrix.CreateTranslation(0, 0, 0),
  27. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  28. VertexColorEnabled = true
  29. };
  30. spriteBatch = new SpriteBatch(graphics);
  31. music = SoundEffects.IntroMusic.CreateInstance();
  32. music.IsLooped = true;
  33. music.Volume = 0.1f;
  34. }
  35. ~Scene() {
  36. Dispose();
  37. }
  38. public void Dispose() {
  39. music.Stop();
  40. music.Dispose();
  41. sceneTarget.Dispose();
  42. basicEffect.Dispose();
  43. spriteBatch.Dispose();
  44. GC.SuppressFinalize(this);
  45. }
  46. public void Draw(bool isRunningSlowly, World world, bool paused) {
  47. graphics.SetRenderTarget(null);
  48. graphics.Clear(backgroundColor);
  49. // Enable the scene after we've gotten enough non-slow frames.
  50. if (framesToSuppress > 0 && !isRunningSlowly) {
  51. framesToSuppress--;
  52. return;
  53. }
  54. // Draw scene to sceneTarget.
  55. graphics.SetRenderTarget(sceneTarget);
  56. graphics.Clear(backgroundColor);
  57. // Draw parallax backgrounds.
  58. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  59. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  60. float xScale = 1f / 16; // Changes with each layer (the layers further back scroll less).
  61. float yScale = 1f / 4; // Constant across all layers.
  62. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  63. Texture2D background = Textures.Backgrounds[i].Get;
  64. float yDiff = (world.Height - camera.Bottom) * yScale;
  65. float yOffset = background.Height - camera.Height - yDiff;
  66. Rectangle bgSource = new Rectangle(
  67. (int) (camera.Left * xScale), (int) yOffset, camera.Width, camera.Height);
  68. spriteBatch.Draw(background, bgTarget, bgSource, Color.White);
  69. xScale *= 2;
  70. }
  71. spriteBatch.End();
  72. // Draw lines of sight.
  73. basicEffect.Projection = camera.Projection;
  74. world.LinesOfSight.Draw(graphics, basicEffect);
  75. // Set up transformation matrix for drawing world objects.
  76. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  77. spriteBatch.Begin(
  78. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  79. // Draw background tiles.
  80. world.DrawBackground(spriteBatch);
  81. // Draw player.
  82. world.Player.Draw(spriteBatch);
  83. // Draw foreground tiles.
  84. world.DrawForeground(spriteBatch);
  85. spriteBatch.End();
  86. // Draw debug rects & lines on top.
  87. Debug.Draw(graphics, basicEffect);
  88. // Draw in-world UI on top of everything.
  89. spriteBatch.Begin(
  90. SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
  91. for (int i = 0; i < world.Player.MaxHealth; i++) {
  92. Vector2 pos = new Vector2(16 + 15 * i, 8);
  93. if (world.Player.Health > i) {
  94. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(0, 0, 16, 16), Color.White);
  95. } else {
  96. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(16, 0, 16, 16), Color.White);
  97. }
  98. }
  99. if (paused) {
  100. string text = Strings.Paused;
  101. Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
  102. Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
  103. music.Pause();
  104. } else {
  105. music.Play();
  106. }
  107. spriteBatch.End();
  108. // Get ready to draw sceneTarget to screen.
  109. graphics.SetRenderTarget(null);
  110. // Letterbox the scene if needed.
  111. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  112. Rectangle drawRect;
  113. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  114. // Need to letterbox the sides.
  115. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  116. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  117. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  118. } else {
  119. // Need to letterbox the top / bottom.
  120. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  121. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  122. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  123. }
  124. // Actually draw to screen.
  125. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  126. SamplerState.PointClamp, DepthStencilState.Default,
  127. RasterizerState.CullNone);
  128. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  129. // Draw debug toasts.
  130. Debug.DrawToasts(spriteBatch);
  131. spriteBatch.End();
  132. }
  133. }
  134. }