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.

99 lines
3.4 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. const int NUM_EDGE_VERTICES = 60;
  7. readonly VertexBuffer vertexBuffer;
  8. readonly IndexBuffer indexBuffer;
  9. // coneVertices[0] is the eye position; the rest are the edge vertices.
  10. readonly VertexPositionColor[] coneVertices = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
  11. // The number of total triangles drawn is one less than the number of edge points.
  12. readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
  13. Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
  14. public LinesOfSight(GraphicsDevice graphics) {
  15. vertexBuffer = new VertexBuffer(
  16. graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly);
  17. indexBuffer = new IndexBuffer(
  18. graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
  19. }
  20. ~LinesOfSight() {
  21. Dispose();
  22. }
  23. public void Dispose() {
  24. vertexBuffer.Dispose();
  25. indexBuffer.Dispose();
  26. GC.SuppressFinalize(this);
  27. }
  28. public void Update(NPC[] npcs, AABB[] collisionTargets) {
  29. NPC npc = npcs[0];
  30. Vector2 eyePos = npc.EyePosition;
  31. float visionRange = npc.VisionRange;
  32. Vector2 ray = npc.VisionRay;
  33. float fov = npc.FieldOfView;
  34. float visionRangeSq = visionRange * visionRange;
  35. float fovStep = fov / (NUM_EDGE_VERTICES - 1);
  36. coneVertices[0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color);
  37. for (int i = 0; i < NUM_EDGE_VERTICES; i++) {
  38. float angle = -fov / 2 + fovStep * i;
  39. Vector2 rotated = ray.Rotate(angle);
  40. Vector2 closestHit = Vector2.Add(eyePos, rotated);
  41. float hitTime = 1f;
  42. Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
  43. for (int j = 0; j < collisionTargets.Length; j++) {
  44. AABB box = collisionTargets[j];
  45. if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + halfTileSize.X) {
  46. continue;
  47. }
  48. Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
  49. if (delta.LengthSquared() > visionRangeSq) {
  50. continue;
  51. }
  52. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  53. if (maybeHit != null) {
  54. Hit hit = maybeHit.Value;
  55. if (hit.Time < hitTime) {
  56. hitTime = hit.Time;
  57. closestHit = hit.Position;
  58. }
  59. }
  60. }
  61. float tint = 0.6f - hitTime / 2;
  62. Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
  63. coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
  64. }
  65. }
  66. public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  67. for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
  68. indices[i * 3] = 0;
  69. indices[i * 3 + 1] = i + 1;
  70. indices[i * 3 + 2] = i + 2;
  71. }
  72. vertexBuffer.SetData(coneVertices);
  73. indexBuffer.SetData(indices);
  74. graphics.SetVertexBuffer(vertexBuffer);
  75. graphics.Indices = indexBuffer;
  76. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  77. pass.Apply();
  78. graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
  79. }
  80. }
  81. }
  82. }