Advertisement
tiago_gazzola

Untitled

Mar 29th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. package com.gazzola.world;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7.  
  8. public class AStar {
  9.  
  10. public static double lastTime = System.currentTimeMillis();
  11. private static Comparator<Node> nodeSorter = new Comparator <Node> () {
  12.  
  13. @Override
  14.  
  15. public int compare(Node n0, Node n1) {
  16. if(n1.fCost < n0.fCost)
  17. return + 1;
  18. if(n1.fCost > n0.fCost)
  19. return -1;
  20. return 0;
  21. }
  22.  
  23. };
  24.  
  25. public static boolean clear() {
  26. if(System.currentTimeMillis() - lastTime >= 1000) {
  27. return true;
  28. }
  29. return false;
  30. }
  31.  
  32. public static List<Node> findPath(World world, Vector2i start, Vector2i end){
  33. lastTime = System.currentTimeMillis();
  34. List<Node> openList = new ArrayList <Node> ();
  35. List<Node> closedList = new ArrayList <Node>();
  36.  
  37. Node current = new Node(start, null, 0, getDistance(start, end));
  38. openList.add(current);
  39. while(openList.size() > 0) {
  40. Collections.sort(openList, nodeSorter);
  41. current = openList.get(0);
  42. if(current.tile.equals(end)) {
  43. //Chegamos no ponto final!
  44. //Basta retornar o valor!
  45. List<Node> path = new ArrayList<Node>();
  46. while(current.parent != null) {
  47. path.add(current);
  48. current = current.parent;
  49. }
  50. openList.clear();
  51. closedList.clear();
  52. return path;
  53. }
  54.  
  55. openList.remove(current);
  56. closedList.add(current);
  57.  
  58. for(int i = 0; i < 9; i++) {
  59. if(i == 4) continue;
  60. int x = current.tile.x;
  61. int y = current.tile.y;
  62. int xi = (i%3) - 1;
  63. int yi = (i/3) - 1;
  64. Tile tile = World.tiles[x+xi+((y+yi)*World.WIDTH)];
  65. if(tile instanceof WallTile) continue;
  66. if(i == 0) {
  67. Tile test = World.tiles[x+xi+1+((y+yi) * World.WIDTH)];
  68. Tile test2 = World.tiles[x+xi+((y+yi+1) * World.WIDTH)];
  69. if(test instanceof WallTile || test2 instanceof WallTile) {
  70. continue;
  71. }
  72. }
  73. else if(i == 2) {
  74. Tile test = World.tiles[x+xi-1+((y+yi) * World.WIDTH)];
  75. Tile test2 = World.tiles[x+xi+((y+yi+1) * World.WIDTH)];
  76. if(test instanceof WallTile || test2 instanceof WallTile) {
  77. continue;
  78. }
  79. }
  80. else if(i == 6) {
  81. Tile test = World.tiles[x+xi+((y+yi-1) * World.WIDTH)];
  82. Tile test2 = World.tiles[x+xi+1+((y+yi) * World.WIDTH)];
  83. if(test instanceof WallTile || test2 instanceof WallTile) {
  84. continue;
  85. }
  86. }
  87. else if(i == 8) {
  88. Tile test = World.tiles[x+xi+((y+yi-1) * World.WIDTH)];
  89. Tile test2 = World.tiles[x+xi-1+((y+yi) * World.WIDTH)];
  90. if(test instanceof WallTile || test2 instanceof WallTile) {
  91. continue;
  92. }
  93.  
  94. Vector2i a = new Vector2i (x+xi, y+yi);
  95. double gCost = current.gCost + getDistance(current.tile, a);
  96. double hCost = getDistance(a, end);
  97.  
  98. Node node = new Node(a, current, gCost, hCost);
  99.  
  100. if(vecInList(closedList,a) && gCost >= current.gCost) continue;
  101.  
  102. if(!vecInList(openList, a)) {
  103. openList.add(node);
  104. }else if(gCost < current.gCost);
  105. openList.remove(current);
  106. openList.add(node);
  107. }
  108. }
  109. }
  110. closedList.clear();
  111. return null;
  112. }
  113.  
  114. private static boolean vecInList(List<Node> list, Vector2i vector) {
  115. for(int i = 0; i < list.size(); i++) {
  116. if(list.get(i).tile.equals(vector)) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122.  
  123. private static double getDistance(Vector2i tile, Vector2i goal) {
  124. double dx = tile.x - goal.x;
  125. double dy = tile.y - goal.y;
  126.  
  127. return Math.sqrt(dx*dx + dy*dy);
  128.  
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement