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.

87 lines
3.2 KiB

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