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
| export class PathFind extends Component { @property({type: Vec2, displayName:"开始点"}) private startPos:Vec2 = new Vec2(0, 0); @property({type: Vec2, displayName:"结束点"}) private endPos: Vec2 = new Vec2(1, 1);
private StartNode: MapNode; private EndNode: MapNode;
private OpenList: IDict<string, MapNode> = new IDict<string, MapNode>(); private CloseList: IDict<string, MapNode> = new IDict<string, MapNode>();
private Add2OpenList(node: MapNode): void{ node.SetColor(Color.CYAN); this.OpenList.Add(node.GetKey(), node); } private Add2CloseList(node: MapNode): void{ node.SetColor(Color.GRAY); this.CloseList.Add(node.GetKey(), node); }
private rowNodes: IMapRow[] = []; protected onLoad(): void { let rowsNode = this.node.getChildByName('Rows'); let rows = rowsNode.children; for (let i = 0; i < rows.length; i++) { const element = rows[i]; let childs = element.children; let cells:MapNode[] = []; for (let j = 0,jMax = childs.length; j < jMax; j++) { const cell = childs[j].getComponent(MapNode); cell.Init(j, i); cells.push(cell); } let row:IMapRow = {row: element, cells: cells};
this.rowNodes.push(row); }
Messenger.AddListener('click_node', this.ClickMapNodeCallback, this); } protected start(): void { this.StartNode = this.GetNode(this.startPos.x, this.startPos.y); this.StartNode.NodeType = NodeType.Start;
this.EndNode = this.GetNode(this.endPos.x, this.endPos.y); this.EndNode.NodeType = NodeType.End;
this.Add2OpenList(this.StartNode); this.RefreshEightNode(this.StartNode, this.EndNode); let node = this.GetMinOpenListNode(); node.ClickEnable = true; }
protected onDestroy(): void { Messenger.RemoveListener('click_node', this.ClickMapNodeCallback, this); }
private GetNode(x:number, y:number): MapNode{ let row:IMapRow = this.rowNodes[y]; let cells:MapNode[] = row.cells; return row.cells[x]; }
private GetEightNode(node:MapNode): MapNode[]{ let data = []; let x = node.PosX; let y = node.PosY;
let checkAndAdd=(item)=>{ let key = item.GetKey(); if(item.NodeType != NodeType.Wall && !this.OpenList.HasKey(key) && !this.CloseList.HasKey(key)){ item.PreNode = node; data.push(item); } };
if(x > 0){ let left = this.GetNode(x-1, y); checkAndAdd(left); } if(x < 9){ let right= this.GetNode(x+1, y); checkAndAdd(right); } if(y > 0){ let top = this.GetNode(x, y-1); checkAndAdd(top); } if(y < 6){ let buttom = this.GetNode(x, y+1); checkAndAdd(buttom); } if(x>0 && y>0){let l_t = this.GetNode(x-1, y-1);checkAndAdd(l_t); } if(x>0 && y<6){let l_b = this.GetNode(x-1, y+1);checkAndAdd(l_b); } if(x<9 && y>0){let r_t = this.GetNode(x+1, y-1);checkAndAdd(r_t); } if(x<9 && y<6){let r_b = this.GetNode(x+1, y+1);checkAndAdd(r_b); }
return data; }
private RefreshEightNode(node: MapNode, endNode: MapNode): void{ let nodes = this.GetEightNode(node); console.log(`8点个数:${nodes.length}`); if(nodes.length == 0) return;
this.OpenList.Remove(node.GetKey()); this.Add2CloseList(node);
for (let i = 0; i < nodes.length; i++) { const element = nodes[i]; element.SetColor(math.Color.CYAN); let g_x = Math.abs(node.PosX - element.PosX); let g_y = Math.abs(node.PosY - element.PosY); let g_value = g_x + g_y == 1 ? 10 : 14; element.GValue = g_value;
let h_x = Math.abs(endNode.PosX - element.PosX); let h_y = Math.abs(endNode.PosY - element.PosY); let min = Math.min(h_x, h_y); let max = Math.max(h_x, h_y); let h_value = 14*min + 10* (max-min); element.HValue = h_value; element.FValue = g_value + h_value; this.OpenList.Add(element.GetKey(), element); } }
private GetMinOpenListNode(): MapNode{ let items = this.OpenList.Values(); if(items.length == 0) return null;
let result:MapNode = items[0]; for(let i = 1,iMax = items.length;i<iMax;i++){
if(result.FValue > items[i].FValue) { result = items[i]; } else if(result.FValue == items[i].FValue){ if(result.HValue > items[i].HValue){ result = items[i]; } } } let nodes = this.GetEightNode(result); if(nodes.length > 0){ result.SetColor(Color.GREEN); return result; } this.OpenList.Remove(result.GetKey()); this.Add2CloseList(result); return this.GetMinOpenListNode(); }
private ClickMapNodeCallback(clickNode: MapNode): void{ console.log(clickNode.PosX, clickNode.PosY); if(clickNode == this.EndNode){ this.PrintSearchPath(this.EndNode); return; }
clickNode.ClickEnable = false; this.Add2OpenList(clickNode); this.RefreshEightNode(clickNode, this.EndNode); let node = this.GetMinOpenListNode(); if(node == null) return;
node.ClickEnable = true; }
private PrintSearchPath(node?: MapNode):void{ if(node == null){ node = this.EndNode; } node.SetColor(math.Color.GREEN); if(node == this.StartNode) return; if(node.PreNode == null) return;
this.PrintSearchPath(node.PreNode); } }
|