Guest User

Untitled

a guest
Nov 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. function findastargraph(x1, y1, x2, y2, topleftx, toplefty) { // finds the top left, length, and width of the smallest rectangle containing the two points and then turns it into a 2d array of the map data
  2. var length = 1;
  3. var width = 1; // the farther one is the one more to the right, and we need the farthest left; if they're the same, default value is 1
  4. if(x2 > x1) {
  5. length = x2 - x1;
  6. } else if(x1 > x2) {
  7. length = x1 - x2;
  8. }
  9. if(y2 > y1) {
  10. width = y2 - y1;
  11. } else if(y1 > y2) {
  12. width = y2 - y1;
  13. }
  14. length++; // i dont know why i have do to this but it works
  15. width++;
  16. var arr = createArray(width, length);
  17. for (var i = 0; i < width; i++) {
  18. for (var j = 0; j < length; j++) { //i is y, j is x
  19. if(tilemap.has(coord(j + topleftx,i + toplefty))) {
  20. if(tilemap.get(coord(j + topleftx,i + toplefty)).kind == 1) {
  21. console.log(tilemap.get(coord(j + topleftx,i + toplefty)).kind);
  22. arr[i][j] = 1;
  23. } else if(tilemap.get(coord(j + topleftx,i + toplefty)).kind == 2) {
  24. console.log(tilemap.get(coord(j + topleftx,i + toplefty)).kind);
  25. arr[i][j] = 0;
  26. }
  27. }
  28. }
  29. }
  30. return arr;
  31. }
  32. function findastarmoves(grid, topleftx, toplefty) {
  33. var movearray = []; //stores next moves as "x,y"
  34. for(var i = 0; i < grid.length; i++) {
  35. console.log(movearray);
  36. movearray.push(coord(grid[i].x + topleftx,grid[i].y + toplefty));
  37. }
  38. }
  39. function pathfind(pos1, pos2) { // "x,y"
  40. var x1 = pos1.split(",")[0];
  41. var y1 = pos1.split(",")[1];
  42. var x2 = pos2.split(",")[0];
  43. var y2 = pos2.split(",")[1];
  44. var topleftx = x1;
  45. var toplefty = y1;
  46. if(x2 > x1) {
  47. topleftx = x1;
  48. } else if(x1 > x2) {
  49. topleftx = x2;
  50. }
  51. if(y2 > y1) {
  52. toplefty = y1;
  53. } else if(y1 > y2) {
  54. toplefty = y2;
  55. }
  56. var relx1 = x1 - topleftx;
  57. var rely1 = y1 - toplefty;
  58. var relx2 = x2 - topleftx;
  59. var rely2 = y2 - toplefty;
  60. var graph = new Graph(findastargraph(x1, y1, x2, y2));
  61. var astarresult = astar.search(graph, graph.grid[relx1][rely1], graph.grid[relx2][rely2]);
  62. return findastarmoves(astarresult, topleftx, toplefty);
  63. }
Add Comment
Please, Sign In to add comment