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.

120 lines
4.1 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(0, 0, 1, 0.6f));
  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. Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
  58. for (int j = 0; j < collisionTargets.Length; j++) {
  59. AABB box = collisionTargets[j];
  60. if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + halfTileSize.X) {
  61. continue;
  62. }
  63. Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
  64. if (delta.LengthSquared() > visionRangeSq) {
  65. continue;
  66. }
  67. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  68. if (maybeHit != null) {
  69. Hit hit = maybeHit.Value;
  70. if (hit.Time < hitTime) {
  71. hitTime = hit.Time;
  72. closestHit = hit.Position;
  73. }
  74. }
  75. }
  76. float tint = 0.6f - hitTime / 2;
  77. Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
  78. coneVertices[index][i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
  79. }
  80. }
  81. public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  82. for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
  83. indices[i * 3] = 0;
  84. indices[i * 3 + 1] = i + 1;
  85. indices[i * 3 + 2] = i + 2;
  86. }
  87. for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
  88. if (!coneEnabled[npcIndex]) {
  89. continue;
  90. }
  91. vertexBuffer.SetData(coneVertices[npcIndex]);
  92. indexBuffer.SetData(indices);
  93. graphics.SetVertexBuffer(vertexBuffer);
  94. graphics.Indices = indexBuffer;
  95. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  96. pass.Apply();
  97. graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
  98. }
  99. }
  100. }
  101. }
  102. }