Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- var length = 1;
- 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
- if(x2 > x1) {
- length = x2 - x1;
- } else if(x1 > x2) {
- length = x1 - x2;
- }
- if(y2 > y1) {
- width = y2 - y1;
- } else if(y1 > y2) {
- width = y2 - y1;
- }
- length++; // i dont know why i have do to this but it works
- width++;
- var arr = createArray(width, length);
- for (var i = 0; i < width; i++) {
- for (var j = 0; j < length; j++) { //i is y, j is x
- if(tilemap.has(coord(j + topleftx,i + toplefty))) {
- if(tilemap.get(coord(j + topleftx,i + toplefty)).kind == 1) {
- console.log(tilemap.get(coord(j + topleftx,i + toplefty)).kind);
- arr[i][j] = 1;
- } else if(tilemap.get(coord(j + topleftx,i + toplefty)).kind == 2) {
- console.log(tilemap.get(coord(j + topleftx,i + toplefty)).kind);
- arr[i][j] = 0;
- }
- }
- }
- }
- return arr;
- }
- function findastarmoves(grid, topleftx, toplefty) {
- var movearray = []; //stores next moves as "x,y"
- for(var i = 0; i < grid.length; i++) {
- console.log(movearray);
- movearray.push(coord(grid[i].x + topleftx,grid[i].y + toplefty));
- }
- }
- function pathfind(pos1, pos2) { // "x,y"
- var x1 = pos1.split(",")[0];
- var y1 = pos1.split(",")[1];
- var x2 = pos2.split(",")[0];
- var y2 = pos2.split(",")[1];
- var topleftx = x1;
- var toplefty = y1;
- if(x2 > x1) {
- topleftx = x1;
- } else if(x1 > x2) {
- topleftx = x2;
- }
- if(y2 > y1) {
- toplefty = y1;
- } else if(y1 > y2) {
- toplefty = y2;
- }
- var relx1 = x1 - topleftx;
- var rely1 = y1 - toplefty;
- var relx2 = x2 - topleftx;
- var rely2 = y2 - toplefty;
- var graph = new Graph(findastargraph(x1, y1, x2, y2));
- var astarresult = astar.search(graph, graph.grid[relx1][rely1], graph.grid[relx2][rely2]);
- return findastarmoves(astarresult, topleftx, toplefty);
- }
Add Comment
Please, Sign In to add comment