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.

129 lines
4.8 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 TreeScene : IScene {
  7. const int MAX_SEGMENTS = 1000;
  8. const int MAX_VERTICES = MAX_SEGMENTS * 6; // 2 triangles per segment
  9. private readonly Color backgroundColor = Color.SkyBlue;
  10. private readonly GraphicsDevice graphics;
  11. private readonly BasicEffect basicEffect;
  12. private VertexPositionColor[] vertices;
  13. private VertexBuffer vertexBuffer;
  14. public TreeScene(GraphicsDevice graphics) {
  15. this.graphics = graphics;
  16. basicEffect = new BasicEffect(graphics) {
  17. World = Matrix.CreateTranslation(0, 0, 0),
  18. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  19. VertexColorEnabled = true,
  20. Projection = Matrix.CreateOrthographicOffCenter(-1920 / 2, 1920 / 2, -1080 / 4, 1080 * 3 / 4, -1, 1)
  21. };
  22. vertices = new VertexPositionColor[MAX_VERTICES];
  23. vertexBuffer = new VertexBuffer(
  24. graphics, typeof(VertexPositionColor), MAX_VERTICES, BufferUsage.WriteOnly);
  25. }
  26. ~TreeScene() {
  27. Dispose();
  28. }
  29. public void Dispose() {
  30. vertexBuffer.Dispose();
  31. GC.SuppressFinalize(this);
  32. }
  33. public struct Trapezoid {
  34. public Vector2 p1, p2, p3, p4;
  35. }
  36. public class TreeNode {
  37. public float Orientation; // relative to parent
  38. public float Length;
  39. public float InWidth;
  40. public float OutWidth;
  41. public readonly List<TreeNode> Children;
  42. public Vector2 Position;
  43. public TreeNode(float orientation, float length, float inWidth, float outWidth) :
  44. this(orientation, length, inWidth, outWidth, new List<TreeNode>()) {}
  45. public TreeNode(float orientation, float length, float inWidth, float outWidth, List<TreeNode> children) {
  46. Orientation = orientation;
  47. Length = length;
  48. InWidth = inWidth;
  49. OutWidth = outWidth;
  50. Children = children;
  51. Position = Vector2.Zero;
  52. }
  53. }
  54. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  55. var tree =
  56. new TreeNode(0.0f, 100, 10, 6, new List<TreeNode>() {
  57. new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
  58. new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
  59. new TreeNode(-0.3f, 100, 6, 4, new List<TreeNode>() {
  60. new TreeNode(-0.1f, 100, 6, 4, new List<TreeNode>() {
  61. new TreeNode(-0.3f, 150, 4, 2),
  62. new TreeNode(0.2f, 200, 4, 2),
  63. new TreeNode(0.5f, 100, 4, 2)
  64. })}),
  65. new TreeNode(0.5f, 100, 6, 4, new List<TreeNode>() {
  66. new TreeNode(-0.1f, 100, 6, 4, new List<TreeNode>() {
  67. new TreeNode(-0.1f, 150, 4, 2),
  68. new TreeNode(0.2f, 200, 4, 2)
  69. })}),
  70. })})});
  71. graphics.Clear(backgroundColor);
  72. var segments = new List<Trapezoid>();
  73. LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
  74. queue.AddLast(tree);
  75. while (queue.Count > 0) {
  76. TreeNode parent = queue.First.Value;
  77. queue.RemoveFirst();
  78. Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation);
  79. Vector2 outPosition = Vector2.Add(parent.Position, outVector);
  80. Trapezoid t = new Trapezoid();
  81. t.p1 = new Vector2(parent.Position.X - parent.InWidth, parent.Position.Y);
  82. t.p2 = new Vector2(parent.Position.X + parent.InWidth, parent.Position.Y);
  83. t.p3 = new Vector2(outPosition.X - parent.OutWidth, outPosition.Y);
  84. t.p4 = new Vector2(outPosition.X + parent.OutWidth, outPosition.Y);
  85. segments.Add(t);
  86. foreach (TreeNode child in parent.Children) {
  87. child.Position = outPosition;
  88. child.Orientation += parent.Orientation;
  89. queue.AddLast(child);
  90. }
  91. }
  92. Color color = Color.SaddleBrown;
  93. for (int i = 0; i < segments.Count; i++) {
  94. Trapezoid t = segments[i];
  95. vertices[i * 6] = new VertexPositionColor(new Vector3(t.p1.X, t.p1.Y, 0), color);
  96. vertices[i * 6 + 1] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  97. vertices[i * 6 + 2] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  98. vertices[i * 6 + 3] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  99. vertices[i * 6 + 4] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  100. vertices[i * 6 + 5] = new VertexPositionColor(new Vector3(t.p4.X, t.p4.Y, 0), color);
  101. }
  102. graphics.SetVertexBuffer(vertexBuffer);
  103. vertexBuffer.SetData(vertices);
  104. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
  105. pass.Apply();
  106. graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, segments.Count * 2);
  107. }
  108. }
  109. }
  110. }