diff --git a/jaws.js b/jaws.js index 65a432f..eb8a91a 100644 --- a/jaws.js +++ b/jaws.js @@ -2502,6 +2502,115 @@ jaws.TileMap.prototype.cell = function(col, row) { return this.cells[col][row] } +/** + * A-Star pathfinding + */ +jaws.TileMap.prototype.findPath = function(start_position, end_position) { + //console.log("finding Path: "+start_position + if (start_position[0] === end_position[0] && start_position[1] === end_position[1]) { + return [this.at(start_position[0], start_position[1])] + } + + var start_col = parseInt(start_position[0] / this.cell_size[0]) + var start_row = parseInt(start_position[1] / this.cell_size[1]) + + var end_col = parseInt(end_position[0] / this.cell_size[0]) + var end_row = parseInt(end_position[1] / this.cell_size[1]) + + var col = start_col + var row = start_row + + var max_distance = this.size[0]+this.size[1] * 2 //if there were a lot of walls, you might have to go a long way + + //var node = [] + //var parent = this.cell(start_col, start_row) + + var open_nodes = [] + var closed_nodes = [] + closed_nodes.push( {node: [col, row], parent: []} ) + + var crowFlies = function(node) { + return Math.abs(end_col-node[0]) + Math.abs(end_row-node[1]); + } + + var findInClosed = function(col, row) { + for (var i=0 ; i < closed_nodes.length ; i++) { + if (closed_nodes[i].node[0] === col && closed_nodes[i].node[1] === row) { + return true + } + } + return false + } + + /* +var findInOpen = function(col, row) { + for (var i=0 ; i < open_nodes.length ; i++) { + if (open_nodes[i].col === col && open_nodes[i].row === row) { + return true + } + } + return false + } +*/ + + //console.log("starting at "+crowFlies([col, row]) +":"+col+","+row) + //console.log("heading to :"+end_col+","+end_row) + + //While something! not sure what yet. + while (! (col === end_col && row === end_row) ) { + if (this.cell(col-1, row).length === 0 && !findInClosed(col-1, row)) { + open_nodes.unshift( {node: [col-1, row], parent: [col, row]} ) + } + + if (this.cell(col+1, row).length === 0 && !findInClosed(col+1, row)) { + open_nodes.unshift( {node: [col+1, row], parent: [col, row]} ) + } + + if (this.cell(col, row-1).length === 0 && !findInClosed(col, row-1)) { + open_nodes.unshift( {node: [col, row-1], parent: [col, row]} ) + } + + if (this.cell(col, row+1).length === 0 && !findInClosed(col, row+1)) { + open_nodes.unshift( {node: [col, row+1], parent: [col, row]} ) + } + + var best_node = {score: max_distance, node: [], index: -1, parent: []} + var node_score = 0 + for (var i=0 ; i