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.

144 lines
4.8 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace SemiColinGames {
  5. public sealed class LinesOfSight : IDisposable {
  6. // Max number of NPCs whose vision cones will be shown at once.
  7. const int MAX_NPCS = 4;
  8. // Number of edge vertices per vision cone.
  9. const int NUM_EDGE_VERTICES = 30;
  10. readonly VertexBuffer vertexBuffer;
  11. readonly IndexBuffer indexBuffer;
  12. readonly bool[] coneEnabled = new bool[MAX_NPCS];
  13. // coneVertices[i][0] is the eye position; the rest are the edge vertices.
  14. readonly VertexPositionColor[][] coneVertices = new VertexPositionColor[MAX_NPCS][];
  15. // The number of total triangles drawn is one less than the number of edge points.
  16. readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
  17. Color color = Color.FromNonPremultiplied(new Vector4(1, 0, 0, 0.25f));
  18. public LinesOfSight(GraphicsDevice graphics) {
  19. for (int i = 0; i < MAX_NPCS; i++) {
  20. coneVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
  21. }
  22. vertexBuffer = new VertexBuffer(
  23. graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly);
  24. indexBuffer = new IndexBuffer(
  25. graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
  26. }
  27. ~LinesOfSight() {
  28. Dispose();
  29. }
  30. public void Dispose() {
  31. vertexBuffer.Dispose();
  32. indexBuffer.Dispose();
  33. GC.SuppressFinalize(this);
  34. }
  35. public void Update(NPC[] npcs, AABB[] collisionTargets) {
  36. for (int i = 0; i < MAX_NPCS; i++) {
  37. coneEnabled[i] = false;
  38. }
  39. for (int i = 0; i < MAX_NPCS; i++) {
  40. UpdateNpc(i, npcs[i], collisionTargets);
  41. }
  42. }
  43. private void UpdateNpc(int index, NPC npc, AABB[] collisionTargets) {
  44. coneEnabled[index] = true;
  45. Vector2 eyePos = npc.EyePosition;
  46. float visionRange = npc.VisionRange;
  47. Vector2 ray = npc.VisionRay;
  48. float fov = npc.FieldOfView;
  49. float visionRangeSq = visionRange * visionRange;
  50. float fovStep = fov / (NUM_EDGE_VERTICES - 1);
  51. coneVertices[index][0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color);
  52. for (int i = 0; i < NUM_EDGE_VERTICES; i++) {
  53. float angle = -fov / 2 + fovStep * i;
  54. Vector2 rotated = ray.Rotate(angle);
  55. Vector2 closestHit = Vector2.Add(eyePos, rotated);
  56. float hitTime = 1f;
  57. for (int j = 0; j < collisionTargets.Length; j++) {
  58. AABB box = collisionTargets[j];
  59. if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + box.HalfSize.X) {
  60. continue;
  61. }
  62. Vector2 delta = Vector2.Add(box.HalfSize, Vector2.Subtract(box.Position, eyePos));
  63. if (delta.LengthSquared() > visionRangeSq) {
  64. continue;
  65. }
  66. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  67. if (maybeHit != null) {
  68. Hit hit = maybeHit.Value;
  69. if (hit.Time < hitTime) {
  70. hitTime = hit.Time;
  71. closestHit = hit.Position;
  72. }
  73. }
  74. }
  75. coneVertices[index][i + 1] = new VertexPositionColor(
  76. new Vector3((int) closestHit.X, (int) closestHit.Y, 0), color);
  77. }
  78. }
  79. public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  80. // Draw the cones themselves.
  81. for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
  82. indices[i * 3] = 0;
  83. indices[i * 3 + 1] = i + 1;
  84. indices[i * 3 + 2] = i + 2;
  85. }
  86. for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
  87. if (!coneEnabled[npcIndex]) {
  88. continue;
  89. }
  90. vertexBuffer.SetData(coneVertices[npcIndex]);
  91. indexBuffer.SetData(indices);
  92. graphics.SetVertexBuffer(vertexBuffer);
  93. graphics.Indices = indexBuffer;
  94. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  95. pass.Apply();
  96. graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
  97. }
  98. }
  99. // Draw the outlines of the cones.
  100. for (int i = 0; i <= NUM_EDGE_VERTICES; i++) {
  101. indices[i] = i;
  102. }
  103. indices[NUM_EDGE_VERTICES + 1] = 0;
  104. for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
  105. if (!coneEnabled[npcIndex]) {
  106. continue;
  107. }
  108. vertexBuffer.SetData(coneVertices[npcIndex]);
  109. indexBuffer.SetData(indices);
  110. graphics.SetVertexBuffer(vertexBuffer);
  111. graphics.Indices = indexBuffer;
  112. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  113. pass.Apply();
  114. // TODO: just draw a single opaque outline.
  115. for (int i = 0; i < 4; i++) {
  116. graphics.DrawIndexedPrimitives(
  117. PrimitiveType.LineStrip, 0, 0, NUM_EDGE_VERTICES + 1);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }