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.

97 lines
3.4 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. 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[numEdgeVertices + 1];
  11. // The number of total triangles drawn is one less than the number of edge points.
  12. readonly int[] indices = new int[(numEdgeVertices - 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), 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. GC.SuppressFinalize(this);
  27. }
  28. public void Update(Player player, AABB[] collisionTargets) {
  29. Vector2 eyePos = player.EyePosition;
  30. float visionRange = player.VisionRange;
  31. Vector2 ray = player.VisionRay;
  32. float fov = player.FieldOfView;
  33. float visionRangeSq = visionRange * visionRange;
  34. float fovStep = fov / (numEdgeVertices - 1);
  35. coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
  36. for (int i = 0; i < numEdgeVertices; i++) {
  37. float angle = -fov / 2 + fovStep * i;
  38. Vector2 rotated = ray.Rotate(angle);
  39. Vector2 closestHit = Vector2.Add(eyePos, rotated);
  40. float hitTime = 1f;
  41. Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
  42. for (int j = 0; j < collisionTargets.Length; j++) {
  43. AABB box = collisionTargets[j];
  44. if (Math.Abs(box.Position.X - player.Position.X) > visionRange + halfTileSize.X) {
  45. continue;
  46. }
  47. Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
  48. if (delta.LengthSquared() > visionRangeSq) {
  49. continue;
  50. }
  51. Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
  52. if (maybeHit != null) {
  53. Hit hit = maybeHit.Value;
  54. if (hit.Time < hitTime) {
  55. hitTime = hit.Time;
  56. closestHit = hit.Position;
  57. }
  58. }
  59. }
  60. float tint = 0.6f - hitTime / 2;
  61. Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
  62. coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
  63. }
  64. }
  65. public void Draw(GraphicsDevice graphics, 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. }