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.

91 lines
3.3 KiB

  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 ShmupScene : IScene {
  7. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  8. private readonly Color letterboxColor = Color.DarkSlateBlue;
  9. private readonly Color backgroundColor = Color.Black;
  10. private readonly GraphicsDevice graphics;
  11. private readonly RenderTarget2D sceneTarget;
  12. private readonly SpriteBatch spriteBatch;
  13. public ShmupScene(GraphicsDevice graphics, Point worldSize) {
  14. this.graphics = graphics;
  15. sceneTarget = new RenderTarget2D(
  16. graphics, worldSize.X, worldSize.Y, false /* mipmap */,
  17. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  18. spriteBatch = new SpriteBatch(graphics);
  19. }
  20. ~ShmupScene() {
  21. Dispose();
  22. }
  23. public void Dispose() {
  24. sceneTarget.Dispose();
  25. spriteBatch.Dispose();
  26. GC.SuppressFinalize(this);
  27. }
  28. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  29. ShmupWorld world = (ShmupWorld) iworld;
  30. // Draw scene to sceneTarget.
  31. graphics.SetRenderTarget(sceneTarget);
  32. graphics.Clear(backgroundColor);
  33. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  34. SamplerState.PointClamp, DepthStencilState.Default,
  35. RasterizerState.CullNone);
  36. // Draw player.
  37. Texture2D playerTexture = world.Player.Texture.Get;
  38. Vector2 spriteCenter = new Vector2(playerTexture.Width / 2, playerTexture.Height / 2);
  39. Vector2 drawPos = Vector2.Floor(Vector2.Subtract(world.Player.Position, spriteCenter));
  40. spriteBatch.Draw(playerTexture, drawPos, Color.White);
  41. // Draw shots.
  42. foreach (ShmupWorld.Shot s in world.Shots) {
  43. Texture2D texture = s.Texture.Get;
  44. Vector2 center = new Vector2(texture.Width / 2, texture.Height / 2);
  45. spriteBatch.Draw(texture, Vector2.Floor(Vector2.Subtract(s.Position, center)), Color.White);
  46. }
  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. spriteBatch.End();
  72. }
  73. }
  74. }