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.

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