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.

95 lines
3.3 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace SemiColinGames {
  5. class LinesOfSight : IDisposable {
  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. ~LinesOfSight() {
  21. Dispose();
  22. }
  23. public void Dispose() {
  24. vertexBuffer.Dispose();
  25. indexBuffer.Dispose();
  26. }
  27. public void Update(Player player, AABB[] collisionTargets) {
  28. Vector2 eyePos = player.EyePosition;
  29. float visionRange = player.VisionRange;
  30. Vector2 ray = player.VisionRay;
  31. float fov = player.FieldOfView;
  32. float visionRangeSq = visionRange * visionRange;
  33. float fovStep = fov / (numEdgeVertices - 1);
  34. coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
  35. for (int i = 0; i < numEdgeVertices; i++) {
  36. float angle = -fov / 2 + fovStep * i;
  37. Vector2 rotated = ray.Rotate(angle);
  38. Vector2 closestHit = Vector2.Add(eyePos, rotated);
  39. float hitTime = 1f;
  40. Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
  41. for (int j = 0; j < collisionTargets.Length; j++) {
  42. AABB box = collisionTargets[j];
  43. if (Math.Abs(box.Position.X - player.Position.X) > visionRange + halfTileSize.X) {
  44. continue;
  45. }
  46. Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
  47. if (delta.LengthSquared() > visionRangeSq) {
  48. continue;
  49. }
  50. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  51. if (maybeHit != null) {
  52. Hit hit = maybeHit.Value;
  53. if (hit.Time < hitTime) {
  54. hitTime = hit.Time;
  55. closestHit = hit.Position;
  56. }
  57. }
  58. }
  59. float tint = 0.6f - hitTime / 2;
  60. Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
  61. coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
  62. }
  63. }
  64. public void Draw(Player player, AABB[] collisionTargets, GraphicsDevice graphics,
  65. BasicEffect lightingEffect) {
  66. for (int i = 0; i < numEdgeVertices - 1; i++) {
  67. indices[i * 3] = 0;
  68. indices[i * 3 + 1] = i + 1;
  69. indices[i * 3 + 2] = i + 2;
  70. }
  71. vertexBuffer.SetData(coneVertices);
  72. indexBuffer.SetData(indices);
  73. graphics.SetVertexBuffer(vertexBuffer);
  74. graphics.Indices = indexBuffer;
  75. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  76. pass.Apply();
  77. graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
  78. }
  79. }
  80. }
  81. }