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.

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