1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import { Data } from "./Data";
const {ccclass, property} = cc._decorator;
@ccclass export default class NewClass extends cc.Component {
@property(cc.Node) startNode:cc.Node = null; @property(cc.Node) endNode:cc.Node = null;
queueArr = []; lcArr =[]; atSquareWidth = 0; forNode = {}; isEnd = false;
test = null; start () { this.lcArr = Data.lcArr; this.atSquareWidth = Data.squareWidth; } startBtn(){ let _rowColumn = this.xyTransitionRowColum(this.startNode.x,this.startNode.y,Data.squareWidth); this.test = this.AStarSearch(_rowColumn); this.test.next(); }
*AStarSearch(start){ let _end = this.xyTransitionRowColum(this.endNode.x,this.endNode.y,Data.squareWidth); let frontier = this.queueArr; let _rowColumn = this.xyTransitionRowColum(this.startNode.x,this.startNode.y,Data.squareWidth); this.enqueue(_rowColumn,0); let came_from = {}; let cost_so_far = {}; let _far = `${_rowColumn.row}*${_rowColumn.column}`; came_from[_far] = null; cost_so_far[_far] = 0; while(frontier.length){ let current = this.dequeue(); if(current.row == _end.row && current.column == _end.column){ current.node.color = new cc.Color(211,25,215); break; } let g_cost = this.getNeighbors(current); for(let next in g_cost){ let new_cost = cost_so_far[`${current.row}*${current.column}`] + 1; let nextRCString = `${g_cost[next].row}*${g_cost[next].column}`; if(!cost_so_far[nextRCString] || new_cost < cost_so_far[nextRCString]){ cost_so_far[nextRCString] = new_cost; console.log(new_cost); let priority = new_cost + this.heuristic(_end,g_cost[next]); this.enqueue(g_cost[next],priority); g_cost[next].node.color = new cc.Color(255,25,255); this.scheduleOnce(()=>{ this.test.next(); },0.02); yield came_from[nextRCString] = current; } } } let _curr = `${_end.row}*${_end.column}`; let _sta = `${_rowColumn.row}*${_rowColumn.column}`; let path = []; while (_curr != _sta){ if(came_from[_curr].node){ came_from[_curr].node.color = new cc.Color(65,25,215); } path.push(_curr); _curr = `${came_from[_curr].row}*${came_from[_curr].column}`; } console.log('------完成',path); console.log('------完成',Object.keys(came_from).length); console.log('------完成',came_from); console.log('------完成',cost_so_far); }
getNeighbors(_rowColumn){ let next = [ {row:0,column:+1}, {row:-1,column:0}, {row:0,column:-1}, {row:+1,column:0} ]; let _arr = []; for(let i = 0;i<4;i++){ let _row = _rowColumn.row + next[i]['row']; let _column = _rowColumn.column + next[i]['column']; let _nodeData = this.lcArr[ _row -1]; let nextRow = _nodeData; let nextColum = _column - 1; if(nextRow && nextRow[nextColum] && nextRow[nextColum]['type'] != undefined && nextRow[nextColum]['type'] != 1){ let node = nextRow[nextColum]['node']; _arr.push({row:_row,column:_column,node:node}); } else { continue; } } return _arr; }
heuristic(a,b){ let x1 = a.row; let y1 = a.column; let x2 = b.row; let y2 = b.column; return Math.abs(x1 - x2) + Math.abs(y1 - y2); }
enqueue(data, priority){ let node = {data:data,priority:priority}; if (!this.queueArr.length) { this.queueArr.push(node); } else { let isEnqueue = false; for (let i = 0; i < this.queueArr.length; i++) { if (this.queueArr[i].priority < node.priority) { this.queueArr.splice(i, 0, node); isEnqueue = true; break; } } if (!isEnqueue) { this.queueArr.push(node); } } }
dequeue() { if (this.queueArr.length) { let _arr = this.queueArr.pop(); return _arr['data']; } }
xyTransitionRowColum (x,y,width){ let _x = Math.ceil(x / width); let _y = Math.ceil(y / width); return {row:_y,column:_x}; }
rowColumTransitionXY (r,c){ let _b = this.atSquareWidth / 2; let _row = r * this.atSquareWidth - _b; let _column = c * this.atSquareWidth - _b; return {x:_column,y:_row}; }
}
|