/** * 導航網格類 左上角為原點(0,0) * @author BrightLi * @since 2020/4/18 */ import Node from "./Node"; import AStar from "./AStar" export default class Grid { private _startNode:Node; //起點 private _endNode:Node; //終點 private _nodes:Array>; //Node數組 private _width:number; //網格列數 private _height:number; //網格行數 private _astar:AStar; // 構造函數 public constructor(width:number, height:number) { this._width = width; this._height = height; this._nodes = []; var x:number,y:number; for(x=0;x{ return this._astar.findPath(this); } // 網格寬度 public get width(){ return this._width; } // 網格高度 public get height(){ return this._height; } // 獲得起點 public get startNode(){ return this._startNode; } // 獲得終點 public get endNode(){ return this._endNode; } // 坐標點是否在網格內 public isInside(x:number,y:number):boolean{ return (x>=0 && x=0 && y { var x=node.x, y=node.y, neighbors=Array(), //上下左右 s0=false,s1=false, s2=false,s3=false, //斜方向 d0=false,d1=false, d2=false,d3=false, nodes=this._nodes; // 上 if(this.isWallableAt(x,y-1)){ neighbors.push(nodes[x][y-1]); s0=true; } // 右 if(this.isWallableAt(x+1,y)){ neighbors.push(nodes[x+1][y]); s1=true; } // 下 if(this.isWallableAt(x,y+1)){ neighbors.push(nodes[x][y+1]); s2=true; } // 左 if(this.isWallableAt(x-1,y)){ neighbors.push(nodes[x-1][y]); s3=true; } if(diagonallMovement==1){ return neighbors; } if(diagonallMovement==3){ d0=s3 && s0; d1=s0 && s1; d2=s1 && s2; d3=s2 && s3; }else if(diagonallMovement==2){ d0=s3 || s0; d1=s0 || s1; d2=s1 || s2; d3=s2 || s3; }else if(diagonallMovement==0){ d0=true; d1=true; d2=true; d3=true; } // 左上 if(d0 && this.isWallableAt(x-1,y-1)){ neighbors.push(nodes[x-1][y-1]); } // 右上 if(d1 && this.isWallableAt(x+1,y-1)){ neighbors.push(nodes[x+1][y+1]); } // 右下 if(d2 && this.isWallableAt(x+1,y+1)){ neighbors.push(nodes[x+1][y+1]); } // 左下 if(d2 && this.isWallableAt(x-1,y+1)){ neighbors.push(nodes[x-1][y+1]); } return neighbors; } // 圓滑路徑 public smoothenPath(path:Array){ var len=path.length, x0=path[0].x, y0=path[0].y, x1=path[path.length-1].x, y1=path[path.length-1].y, sx:number,sy:number, ex:number,ey:number, newPath:Array, i:number,j:number,coord:Node,line:Array,testCoord:Node,blocked:boolean; sx=x0; sy=y0; newPath=[new Node(sx,sy)]; for(i=2;i=[], sx:number,sy:number,dx:number,dy:number,err:number,e2:number; dx=Math.abs(x1-x0); dy=Math.abs(y1-y0); sx=(x0-dy){ err=err-dy; x0=x0+sx; } if(e2