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.

163 lines
5.9 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 SneakScene : 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 MusicPlayer musicPlayer;
  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 SneakScene(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. musicPlayer = new MusicPlayer();
  32. musicPlayer.Load(SoundEffects.IntroMusic);
  33. }
  34. ~SneakScene() {
  35. Dispose();
  36. }
  37. public void Dispose() {
  38. musicPlayer.Dispose();
  39. sceneTarget.Dispose();
  40. basicEffect.Dispose();
  41. spriteBatch.Dispose();
  42. GC.SuppressFinalize(this);
  43. }
  44. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  45. SneakWorld world = (SneakWorld) iworld;
  46. graphics.SetRenderTarget(null);
  47. graphics.Clear(backgroundColor);
  48. // Enable the scene after we've gotten enough non-slow frames.
  49. if (framesToSuppress > 0 && !isRunningSlowly) {
  50. framesToSuppress--;
  51. return;
  52. }
  53. // Draw scene to sceneTarget.
  54. graphics.SetRenderTarget(sceneTarget);
  55. graphics.Clear(backgroundColor);
  56. // Draw parallax backgrounds.
  57. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  58. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  59. float xScale = 1f / 16; // Changes with each layer (the layers further back scroll less).
  60. float yScale = 1f / 4; // Constant across all layers.
  61. for (int i = 0; i < Textures.Backgrounds.Length; i++) {
  62. Texture2D background = Textures.Backgrounds[i].Get;
  63. float yDiff = (world.Height - camera.Bottom) * yScale;
  64. float yOffset = background.Height - camera.Height - yDiff;
  65. Rectangle bgSource = new Rectangle(
  66. (int) (camera.Left * xScale), (int) yOffset, camera.Width, camera.Height);
  67. spriteBatch.Draw(background, bgTarget, bgSource, Color.White);
  68. xScale *= 2;
  69. }
  70. spriteBatch.End();
  71. // Draw lines of sight.
  72. basicEffect.Projection = camera.Projection;
  73. world.LinesOfSight.Draw(graphics, basicEffect);
  74. // Set up transformation matrix for drawing world objects.
  75. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  76. spriteBatch.Begin(
  77. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  78. // Draw background tiles.
  79. world.DrawBackground(spriteBatch);
  80. // Draw player.
  81. world.Player.Draw(spriteBatch);
  82. // Draw foreground tiles.
  83. world.DrawForeground(spriteBatch);
  84. spriteBatch.End();
  85. // Draw debug rects & lines on top.
  86. Debug.Draw(graphics, basicEffect);
  87. // Draw in-world UI on top of everything.
  88. spriteBatch.Begin(
  89. SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null,
  90. Matrix.Identity);
  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. musicPlayer.Pause();
  104. } else {
  105. musicPlayer.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. }