update
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { _decorator, Component, Vec2 } from 'cc'
|
||||
import { Component, Vec2, _decorator } from 'cc'
|
||||
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
export enum AstarGridType {
|
||||
Hider = 0, //不能走
|
||||
Normal = 1, //能走
|
||||
End = 2, //终点
|
||||
Hider = 0, // 不能走
|
||||
Normal = 1, // 能走
|
||||
End = 2, // 终点
|
||||
}
|
||||
|
||||
@ccclass('Astar')
|
||||
@@ -28,8 +28,7 @@ export class Astar extends Component {
|
||||
private path: Array<AstarGrid> = []
|
||||
private gridsList: Array<Array<AstarGrid>> = []
|
||||
|
||||
onLoad() {
|
||||
}
|
||||
onLoad() {}
|
||||
|
||||
/**
|
||||
* @param mapW 宽格子数
|
||||
@@ -51,15 +50,12 @@ export class Astar extends Component {
|
||||
this.closeList = []
|
||||
this.path = []
|
||||
// 初始化格子二维数组
|
||||
this.gridsList = new Array(this.mapW + 1)
|
||||
for (let col = 0; col < this.gridsList.length; col++) {
|
||||
this.gridsList[col] = new Array(this.mapH + 1)
|
||||
}
|
||||
for (let col = 0; col <= this.mapW; col++) {
|
||||
for (let row = 0; row <= this.mapH; row++) {
|
||||
this.addGrid(col, row, AstarGridType.Normal)
|
||||
}
|
||||
}
|
||||
this.gridsList = Array.from({ length: this.mapW + 1 })
|
||||
for (let col = 0; col < this.gridsList.length; col++)
|
||||
this.gridsList[col] = Array.from({ length: this.mapH + 1 })
|
||||
|
||||
for (let col = 0; col <= this.mapW; col++)
|
||||
for (let row = 0; row <= this.mapH; row++) this.addGrid(col, row, AstarGridType.Normal)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +65,7 @@ export class Astar extends Component {
|
||||
* @param type
|
||||
*/
|
||||
private addGrid(x: number, y: number, type: number = AstarGridType.Hider) {
|
||||
let grid = new AstarGrid()
|
||||
const grid = new AstarGrid()
|
||||
grid.x = x
|
||||
grid.y = y
|
||||
grid.type = type
|
||||
@@ -83,7 +79,7 @@ export class Astar extends Component {
|
||||
* @param type
|
||||
*/
|
||||
public setGridType(x: number, y: number, type: number) {
|
||||
let curGrid: AstarGrid = this.gridsList[x][y]
|
||||
const curGrid: AstarGrid = this.gridsList[x][y]
|
||||
curGrid.type = type
|
||||
}
|
||||
|
||||
@@ -94,70 +90,59 @@ export class Astar extends Component {
|
||||
* @param callback
|
||||
*/
|
||||
public findPath(startPos: Vec2, endPos: Vec2, callback: Function = null) {
|
||||
let startGrid = this.gridsList[startPos.x][startPos.y]
|
||||
const startGrid = this.gridsList[startPos.x][startPos.y]
|
||||
this.openList.push(startGrid)
|
||||
let curGrid: AstarGrid = this.openList[0]
|
||||
while (this.openList.length > 0 && curGrid.type != AstarGridType.End) {
|
||||
while (this.openList.length > 0 && curGrid.type !== AstarGridType.End) {
|
||||
// 每次都取出f值最小的节点进行查找
|
||||
curGrid = this.openList[0]
|
||||
if (curGrid.type == AstarGridType.End) {
|
||||
if (curGrid.type === AstarGridType.End) {
|
||||
// Logger.log(this,"find path success.");
|
||||
this.generatePath(curGrid)
|
||||
if (callback) {
|
||||
callback(this.path)
|
||||
}
|
||||
if (callback) callback(this.path)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
for (let i: number = -1; i <= 1; i++) {
|
||||
for (let j: number = -1; j <= 1; j++) {
|
||||
if (i != 0 || j != 0) {
|
||||
let col = curGrid.x + i
|
||||
let row = curGrid.y + j
|
||||
if (i !== 0 || j !== 0) {
|
||||
const col = curGrid.x + i
|
||||
const row = curGrid.y + j
|
||||
if (
|
||||
col >= 0 &&
|
||||
row >= 0 &&
|
||||
col <= this.mapW &&
|
||||
row <= this.mapH &&
|
||||
this.gridsList[col][row].type != AstarGridType.Hider &&
|
||||
this.closeList.indexOf(this.gridsList[col][row]) < 0
|
||||
col >= 0
|
||||
&& row >= 0
|
||||
&& col <= this.mapW
|
||||
&& row <= this.mapH
|
||||
&& this.gridsList[col][row].type !== AstarGridType.Hider
|
||||
&& !this.closeList.includes(this.gridsList[col][row])
|
||||
) {
|
||||
if (this.is8dir) {
|
||||
// 8方向 斜向走动时要考虑相邻的是不是障碍物
|
||||
if (
|
||||
this.gridsList[col - i][row].type == AstarGridType.Hider ||
|
||||
this.gridsList[col][row - j].type == AstarGridType.Hider
|
||||
) {
|
||||
this.gridsList[col - i][row].type === AstarGridType.Hider
|
||||
|| this.gridsList[col][row - j].type === AstarGridType.Hider
|
||||
)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// 四方形行走
|
||||
if (Math.abs(i) == Math.abs(j)) {
|
||||
continue
|
||||
}
|
||||
if (Math.abs(i) === Math.abs(j)) continue
|
||||
}
|
||||
// 计算g值
|
||||
let g =
|
||||
curGrid.g +
|
||||
Math.floor(Math.sqrt(Math.pow(i * 10, 2)) + Math.pow(j * 10, 2))
|
||||
if (
|
||||
this.gridsList[col][row].g == 0 ||
|
||||
this.gridsList[col][row].g > g
|
||||
) {
|
||||
const g = curGrid.g + Math.floor(Math.sqrt((i * 10) ** 2) + (j * 10) ** 2)
|
||||
if (this.gridsList[col][row].g === 0 || this.gridsList[col][row].g > g) {
|
||||
this.gridsList[col][row].g = g
|
||||
// 更新父节点
|
||||
this.gridsList[col][row].parent = curGrid
|
||||
}
|
||||
// 计算h值 manhattan估算法
|
||||
this.gridsList[col][row].h =
|
||||
Math.abs(endPos.x - col) + Math.abs(endPos.y - row)
|
||||
this.gridsList[col][row].h = Math.abs(endPos.x - col) + Math.abs(endPos.y - row)
|
||||
// 更新f值
|
||||
this.gridsList[col][row].f =
|
||||
this.gridsList[col][row].g + this.gridsList[col][row].h
|
||||
this.gridsList[col][row].f = this.gridsList[col][row].g + this.gridsList[col][row].h
|
||||
// 如果不在开放列表里则添加到开放列表里
|
||||
if (this.openList.indexOf(this.gridsList[col][row]) < 0) {
|
||||
if (!this.openList.includes(this.gridsList[col][row]))
|
||||
this.openList.push(this.gridsList[col][row])
|
||||
}
|
||||
|
||||
// // 重新按照f值排序(升序排列)
|
||||
}
|
||||
}
|
||||
@@ -170,9 +155,8 @@ export class Astar extends Component {
|
||||
if (this.openList.length <= 0) {
|
||||
// Logger.log(this,"find path failed.");
|
||||
this.path = []
|
||||
if (callback) {
|
||||
callback(this.path)
|
||||
}
|
||||
if (callback) callback(this.path)
|
||||
|
||||
break
|
||||
}
|
||||
// 重新按照f值排序(升序排列)
|
||||
@@ -195,6 +179,5 @@ export class Astar extends Component {
|
||||
return this.path
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
}
|
||||
onDestroy() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user