Advertisement
443eb9

Untitled

Aug 30th, 2023 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. public struct PathFindJob : IJob
  2. {
  3. private HexUnit _origin;
  4. private HexUnit _destination;
  5. private Stack<HexUnit> _path;
  6. private HashSet<PathUnit> _explored;
  7. private SortedSet<PathUnit> _toExplore;
  8. public PathFindJob(HexUnit origin, HexUnit destination)
  9. {
  10. _origin = origin;
  11. _destination = destination;
  12. _path = new Stack<HexUnit>();
  13. _explored = new HashSet<PathUnit>();
  14. _toExplore = new SortedSet<PathUnit> { new(origin) };
  15. }
  16. public void Execute()
  17. {
  18. while (_toExplore.Count != 0)
  19. {
  20. PathUnit cur = _toExplore.Min;
  21. if (cur.hexUnit.coordinate == _destination.coordinate)
  22. {
  23. while (cur!.hexUnit.coordinate != _origin.coordinate)
  24. {
  25. _path.Push(cur.hexUnit);
  26. cur = cur.prior;
  27. }
  28. return;
  29. }
  30. _toExplore.Remove(cur);
  31. _explored.Add(cur);
  32. foreach (PathUnit u in cur.GetNeighbours())
  33. {
  34. if (u == null) continue;
  35. if (!u.hexUnit.walkable || _explored.Contains(u)) continue;
  36. bool alreadyScheduled = _toExplore.Contains(u);
  37. if (!alreadyScheduled || cur.G + 1 < u.G)
  38. {
  39. u.G = cur.G + 1;
  40. u.H = HexUnit.GetDistance(u.hexUnit.coordinate, _destination.coordinate);
  41. u.prior = cur;
  42. if (!alreadyScheduled)
  43. _toExplore.Add(u);
  44. }
  45. }
  46. }
  47. }
  48. public Stack<HexUnit> GetPath() => _path;
  49. }
  50.  
  51.  
  52. public class PathUnit : IComparable<PathUnit>, IEquatable<PathUnit>
  53. {
  54. public int F => G + H;
  55. public int G;
  56. public int H;
  57. [CanBeNull]
  58. public PathUnit prior;
  59. public readonly HexUnit hexUnit;
  60. private HexUnit _destination;
  61. public PathUnit(HexUnit hexUnit)
  62. {
  63. this.hexUnit = hexUnit;
  64. G = 0;
  65. H = 0;
  66. prior = null;
  67. }
  68. public int CompareTo(PathUnit other)
  69. {
  70. if (hexUnit.GetHashCode() == other.hexUnit.GetHashCode()) return 0;
  71. if (F < other.F) return -1;
  72. if (F == other.F && H < other.H) return -1;
  73. return 1;
  74. }
  75. public bool Equals(PathUnit other) =>
  76. other != null && other.CompareTo(this) == 0;
  77. public override int GetHashCode() =>
  78. hexUnit.GetHashCode();
  79. public override bool Equals(object obj) =>
  80. (PathUnit)obj != null && ((PathUnit)obj).CompareTo(this) == 0;
  81. public List<PathUnit> GetNeighbours()
  82. {
  83. HexMap m = EnvironmentManager.Instance.targetMap;
  84. Vector3Int c = hexUnit.coordinate;
  85. return new List<PathUnit>
  86. {
  87. new(m.GetUnit(c + new Vector3Int(1, 0, -1))),
  88. new(m.GetUnit(c + new Vector3Int(0, 1, -1))),
  89. new(m.GetUnit(c + new Vector3Int(-1, 1, 0))),
  90. new(m.GetUnit(c + new Vector3Int(-1, 0, 1))),
  91. new(m.GetUnit(c + new Vector3Int(0, -1, 1))),
  92. new(m.GetUnit(c + new Vector3Int(1, -1, 0)))
  93. };
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement