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.

96 lines
3.1 KiB

3 years ago
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. public sealed class SpiderScene : IScene {
  7. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  8. private readonly Color letterboxColor = Color.Blue;
  9. private readonly Color backgroundColor = Color.Black;
  10. private readonly GraphicsDevice graphics;
  11. private readonly RenderTarget2D sceneTarget;
  12. private readonly SpriteBatch spriteBatch;
  13. private readonly BasicEffect basicEffect;
  14. public SpiderScene(GraphicsDevice graphics, Point worldSize) {
  15. this.graphics = graphics;
  16. sceneTarget = new RenderTarget2D(
  17. graphics, worldSize.X, worldSize.Y, false /* mipmap */,
  18. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  19. spriteBatch = new SpriteBatch(graphics);
  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. }
  26. ~SpiderScene() {
  27. Dispose();
  28. }
  29. public void Dispose() {
  30. sceneTarget.Dispose();
  31. spriteBatch.Dispose();
  32. basicEffect.Dispose();
  33. GC.SuppressFinalize(this);
  34. }
  35. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  36. SpiderWorld world = (SpiderWorld) iworld;
  37. // Draw scene to sceneTarget.
  38. graphics.SetRenderTarget(sceneTarget);
  39. graphics.Clear(backgroundColor);
  40. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  41. SamplerState.PointClamp, DepthStencilState.Default,
  42. RasterizerState.CullNone);
  43. foreach (SpiderWorld.Anchor anchor in world.Anchors) {
  44. anchor.Draw(spriteBatch);
  45. }
  46. world.Player.Draw(spriteBatch);
  47. // Finish drawing sprites.
  48. spriteBatch.End();
  49. // Get ready to draw sceneTarget to screen.
  50. graphics.SetRenderTarget(null);
  51. graphics.Clear(letterboxColor);
  52. // Letterbox the scene if needed.
  53. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  54. Rectangle drawRect;
  55. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  56. // Need to letterbox the sides.
  57. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  58. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  59. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  60. } else {
  61. // Need to letterbox the top / bottom.
  62. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  63. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  64. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  65. }
  66. // Actually draw to screen.
  67. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  68. SamplerState.PointClamp, DepthStencilState.Default,
  69. RasterizerState.CullNone);
  70. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  71. // Draw debug toasts.
  72. Debug.DrawToasts(spriteBatch);
  73. spriteBatch.End();
  74. }
  75. }
  76. }