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.

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