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.

172 lines
6.4 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. public void Rotate(float angle) {
  36. p1 = p1.Rotate(angle);
  37. p2 = p2.Rotate(angle);
  38. p3 = p3.Rotate(angle);
  39. p4 = p4.Rotate(angle);
  40. }
  41. public void Translate(Vector2 position) {
  42. p1 = Vector2.Add(p1, position);
  43. p2 = Vector2.Add(p2, position);
  44. p3 = Vector2.Add(p3, position);
  45. p4 = Vector2.Add(p4, position);
  46. }
  47. }
  48. public class TreeNode {
  49. public float Orientation; // relative to parent
  50. public float Length;
  51. public float InWidth;
  52. public float OutWidth;
  53. public readonly List<TreeNode> Children;
  54. public Vector2 Position;
  55. public TreeNode(float orientation, float length, float inWidth, float outWidth) :
  56. this(orientation, length, inWidth, outWidth, new List<TreeNode>()) {
  57. }
  58. public TreeNode(float orientation, float length, float inWidth, float outWidth, TreeNode child) :
  59. this(orientation, length, inWidth, outWidth, new List<TreeNode>() { child }) {
  60. }
  61. public TreeNode(float orientation, float length, float inWidth, float outWidth, TreeNode child1, TreeNode child2) :
  62. this(orientation, length, inWidth, outWidth, new List<TreeNode>() { child1, child2 }) {
  63. }
  64. public TreeNode(float orientation, float length, float inWidth, float outWidth, TreeNode child1, TreeNode child2, TreeNode child3) :
  65. this(orientation, length, inWidth, outWidth, new List<TreeNode>() { child1, child2, child3 }) {
  66. }
  67. public TreeNode(float orientation, float length, float inWidth, float outWidth, List<TreeNode> children) {
  68. Orientation = orientation;
  69. Length = length;
  70. InWidth = inWidth;
  71. OutWidth = outWidth;
  72. Children = children;
  73. Position = Vector2.Zero;
  74. }
  75. }
  76. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  77. var tree =
  78. new TreeNode(-0.2f, 100, 10, 6,
  79. new TreeNode(-0.2f, 100, 10, 6,
  80. new TreeNode(-0.2f, 100, 10, 6,
  81. new TreeNode(-0.3f, 100, 6, 4,
  82. new TreeNode(-0.1f, 100, 6, 4,
  83. new TreeNode(-0.3f, 150, 4, 2),
  84. new TreeNode(0.2f, 200, 4, 2),
  85. new TreeNode(0.5f, 100, 4, 2))),
  86. new TreeNode(0.5f, 100, 6, 4,
  87. new TreeNode(-0.1f, 100, 6, 4,
  88. new TreeNode(-0.1f, 150, 4, 2),
  89. new TreeNode(0.2f, 200, 4, 2))))));
  90. graphics.Clear(backgroundColor);
  91. var segments = new List<Trapezoid>();
  92. LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
  93. queue.AddLast(tree);
  94. while (queue.Count > 0) {
  95. TreeNode parent = queue.First.Value;
  96. queue.RemoveFirst();
  97. Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation);
  98. Vector2 outPosition = Vector2.Add(parent.Position, outVector);
  99. // We want a trapezoid with 4 points. A is the in position, B is the out position.
  100. // The TreeNode.Length is the distance from A to B.
  101. //
  102. // We come up with the points relative to A being the origin, then rotate the trapezoid
  103. // by its orientation, and translate the result to A's actual position.
  104. //
  105. // 3---B---4 <-- length = outWidth
  106. // / | \
  107. // / | \
  108. // 1------A------2 <-- length = inWidth
  109. // This fudge factor lengthens the sides a bit longer to prevent small discontinuities
  110. // in the rendered result.
  111. // TODO: remove this sideLengthFudge.
  112. float sideLengthFudge = 1.05f;
  113. Trapezoid t = new Trapezoid();
  114. t.p1 = new Vector2(-parent.InWidth, 0);
  115. t.p2 = new Vector2(parent.InWidth, 0);
  116. t.p3 = new Vector2(-parent.OutWidth, parent.Length * sideLengthFudge);
  117. t.p4 = new Vector2(parent.OutWidth, parent.Length * sideLengthFudge);
  118. t.Rotate(parent.Orientation);
  119. t.Translate(parent.Position);
  120. segments.Add(t);
  121. foreach (TreeNode child in parent.Children) {
  122. child.Position = outPosition;
  123. child.Orientation += parent.Orientation;
  124. queue.AddLast(child);
  125. }
  126. }
  127. Color color = Color.SaddleBrown;
  128. for (int i = 0; i < segments.Count; i++) {
  129. Trapezoid t = segments[i];
  130. vertices[i * 6] = new VertexPositionColor(new Vector3(t.p1.X, t.p1.Y, 0), color);
  131. vertices[i * 6 + 1] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  132. vertices[i * 6 + 2] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  133. vertices[i * 6 + 3] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  134. vertices[i * 6 + 4] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  135. vertices[i * 6 + 5] = new VertexPositionColor(new Vector3(t.p4.X, t.p4.Y, 0), color);
  136. }
  137. graphics.SetVertexBuffer(vertexBuffer);
  138. vertexBuffer.SetData(vertices);
  139. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
  140. pass.Apply();
  141. graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, segments.Count * 2);
  142. }
  143. }
  144. }
  145. }