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.

162 lines
5.6 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 bgScale = 1f / 16;
  61. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  62. float yDiff = (world.Height - camera.Bottom) * bgScale;
  63. float yOffset = Textures.Backgrounds[i].Get.Height - camera.Height - yDiff;
  64. Rectangle bgSource = new Rectangle(
  65. (int) (camera.Left * bgScale), (int) yOffset, camera.Width, camera.Height);
  66. spriteBatch.Draw(Textures.Backgrounds[i].Get, bgTarget, bgSource, Color.White);
  67. bgScale *= 2;
  68. }
  69. spriteBatch.End();
  70. // Draw lines of sight.
  71. basicEffect.Projection = camera.Projection;
  72. world.LinesOfSight.Draw(graphics, basicEffect);
  73. // Set up transformation matrix for drawing world objects.
  74. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  75. spriteBatch.Begin(
  76. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  77. // Draw background tiles.
  78. world.DrawBackground(spriteBatch);
  79. // Draw player.
  80. world.Player.Draw(spriteBatch);
  81. // Draw foreground tiles.
  82. world.DrawForeground(spriteBatch);
  83. spriteBatch.End();
  84. // Draw debug rects & lines on top.
  85. Debug.Draw(graphics, basicEffect);
  86. // Draw in-world UI on top of everything.
  87. spriteBatch.Begin(
  88. SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
  89. for (int i = 0; i < world.Player.MaxHealth; i++) {
  90. Vector2 pos = new Vector2(16 + 15 * i, 8);
  91. if (world.Player.Health > i) {
  92. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(0, 0, 16, 16), Color.White);
  93. } else {
  94. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(16, 0, 16, 16), Color.White);
  95. }
  96. }
  97. if (paused) {
  98. string text = Strings.Paused;
  99. Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
  100. Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
  101. music.Pause();
  102. } else {
  103. music.Play();
  104. }
  105. spriteBatch.End();
  106. // Get ready to draw sceneTarget to screen.
  107. graphics.SetRenderTarget(null);
  108. // Letterbox the scene if needed.
  109. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  110. Rectangle drawRect;
  111. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  112. // Need to letterbox the sides.
  113. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  114. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  115. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  116. } else {
  117. // Need to letterbox the top / bottom.
  118. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  119. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  120. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  121. }
  122. // Actually draw to screen.
  123. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  124. SamplerState.PointClamp, DepthStencilState.Default,
  125. RasterizerState.CullNone);
  126. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  127. // Draw debug toasts.
  128. Debug.DrawToasts(spriteBatch);
  129. spriteBatch.End();
  130. }
  131. }
  132. }