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.

165 lines
5.8 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 : IScene {
  7. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  8. private readonly Color backgroundColor = Color.CornflowerBlue;
  9. private readonly GraphicsDevice graphics;
  10. private readonly Camera camera;
  11. private readonly RenderTarget2D sceneTarget;
  12. private readonly BasicEffect basicEffect;
  13. private readonly SpriteBatch spriteBatch;
  14. private 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. private 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, IWorld iworld, bool paused) {
  47. World world = (World) iworld;
  48. graphics.SetRenderTarget(null);
  49. graphics.Clear(backgroundColor);
  50. // Enable the scene after we've gotten enough non-slow frames.
  51. if (framesToSuppress > 0 && !isRunningSlowly) {
  52. framesToSuppress--;
  53. return;
  54. }
  55. // Draw scene to sceneTarget.
  56. graphics.SetRenderTarget(sceneTarget);
  57. graphics.Clear(backgroundColor);
  58. // Draw parallax backgrounds.
  59. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  60. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  61. float xScale = 1f / 16; // Changes with each layer (the layers further back scroll less).
  62. float yScale = 1f / 4; // Constant across all layers.
  63. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  64. Texture2D background = Textures.Backgrounds[i].Get;
  65. float yDiff = (world.Height - camera.Bottom) * yScale;
  66. float yOffset = background.Height - camera.Height - yDiff;
  67. Rectangle bgSource = new Rectangle(
  68. (int) (camera.Left * xScale), (int) yOffset, camera.Width, camera.Height);
  69. spriteBatch.Draw(background, bgTarget, bgSource, Color.White);
  70. xScale *= 2;
  71. }
  72. spriteBatch.End();
  73. // Draw lines of sight.
  74. basicEffect.Projection = camera.Projection;
  75. world.LinesOfSight.Draw(graphics, basicEffect);
  76. // Set up transformation matrix for drawing world objects.
  77. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  78. spriteBatch.Begin(
  79. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  80. // Draw background tiles.
  81. world.DrawBackground(spriteBatch);
  82. // Draw player.
  83. world.Player.Draw(spriteBatch);
  84. // Draw foreground tiles.
  85. world.DrawForeground(spriteBatch);
  86. spriteBatch.End();
  87. // Draw debug rects & lines on top.
  88. Debug.Draw(graphics, basicEffect);
  89. // Draw in-world UI on top of everything.
  90. spriteBatch.Begin(
  91. SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
  92. for (int i = 0; i < world.Player.MaxHealth; i++) {
  93. Vector2 pos = new Vector2(16 + 15 * i, 8);
  94. if (world.Player.Health > i) {
  95. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(0, 0, 16, 16), Color.White);
  96. } else {
  97. spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(16, 0, 16, 16), Color.White);
  98. }
  99. }
  100. if (paused) {
  101. string text = Strings.Paused;
  102. Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
  103. Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
  104. music.Pause();
  105. } else {
  106. music.Play();
  107. }
  108. spriteBatch.End();
  109. // Get ready to draw sceneTarget to screen.
  110. graphics.SetRenderTarget(null);
  111. // Letterbox the scene if needed.
  112. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  113. Rectangle drawRect;
  114. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  115. // Need to letterbox the sides.
  116. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  117. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  118. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  119. } else {
  120. // Need to letterbox the top / bottom.
  121. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  122. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  123. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  124. }
  125. // Actually draw to screen.
  126. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  127. SamplerState.PointClamp, DepthStencilState.Default,
  128. RasterizerState.CullNone);
  129. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  130. // Draw debug toasts.
  131. Debug.DrawToasts(spriteBatch);
  132. spriteBatch.End();
  133. }
  134. }
  135. }