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.

154 lines
5.5 KiB

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