443eb9

Untitled

Aug 29th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using HexagonSurvive.Managers;
  4. using HexagonSurvive.Map.Models;
  5. using JetBrains.Annotations;
  6. using UnityEngine;
  7.  
  8. namespace HexagonSurvive.Navigation
  9. {
  10. public class PathUnit : IComparable<PathUnit>
  11. {
  12. public int F => G + H;
  13. public int G;
  14. public int H;
  15. [CanBeNull]
  16. public PathUnit prior;
  17. public readonly HexUnit hexUnit;
  18.  
  19. private HexUnit _destination;
  20.  
  21. public PathUnit(HexUnit hexUnit, HexUnit destination)
  22. {
  23. this.hexUnit = hexUnit;
  24. _destination = destination;
  25. G = 0;
  26. H = 0;
  27. prior = null;
  28. }
  29.  
  30. public override int GetHashCode() =>
  31. hexUnit.GetHashCode();
  32.  
  33. public override bool Equals(object obj) =>
  34. obj != null && obj.GetHashCode() == GetHashCode();
  35.  
  36. public int CompareTo(PathUnit other)
  37. {
  38. if (hexUnit.coordinate == other.hexUnit.coordinate) return 0;
  39. if (F < other.F) return -1;
  40. if (F == other.F && H < other.H) return -1;
  41. return 1;
  42. }
  43.  
  44. public List<PathUnit> GetNeighbours()
  45. {
  46. var m = EnvironmentManager.Instance.targetMap;
  47. var c = hexUnit.coordinate;
  48. return new List<PathUnit>
  49. {
  50. new(m.GetUnit(c + new Vector3Int(1, 0, -1)), _destination),
  51. new(m.GetUnit(c + new Vector3Int(0, 1, -1)), _destination),
  52. new(m.GetUnit(c + new Vector3Int(-1, 1, 0)), _destination),
  53. new(m.GetUnit(c + new Vector3Int(-1, 0, 1)), _destination),
  54. new(m.GetUnit(c + new Vector3Int(0, -1, 1)), _destination),
  55. new(m.GetUnit(c + new Vector3Int(1, -1, 0)), _destination)
  56. };
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment