版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
对上一篇小怪系列文章作详细分析,帮助大家理解。
正文
首先,对于曼哈顿估价法。
你就可以理解为直线的一段或者几段距离的加和,直线距离。
// 曼哈顿估价法,传入当前点与目标点,返回估值
// abs 为取绝对值
manHattan (nowPoint, pIndex) {
let dx = Math.abs(nowPoint.x - pIndex.x);
let dy = Math.abs(nowPoint.y - pIndex.y);
return dx + dy;
}
然后让我们分析一下路径上点的对象应该具有的信息。
// new 一个空对象
let obj = new Object();
// 每个网格的点的行和列 对应 x 和 y
obj.x = v.x;
obj.y = v.y;
obj.g = this.manHattan(v, this.mIndex);
obj.h = this.manHattan(v, this.pIndex);
// g 为从起点,沿着路径,移动到当前点的移动耗费。
// h 为从当前点到终点的移动耗费。这不包括障碍,单纯的是曼哈顿距离,也就是直线距离。因为我们不知道什么时候有障碍,这叫启发式。
// 我们的路径是通过反复遍历 open 列表并且选择具有最低 f 值装入 close 列表。因为 f 是综合值,调整 g 和 h 的比例会起到不同寻路效果。
obj.f = obj.g + obj.h;
// 起点无上级,然后搜索到目标点后可以轻易的靠着 parent 回溯到起点。
obj.parent = parent;
按步演示
在 start 中声明数据。
start () {
// 小怪的坐标,起点
this.mIndex = cc.v2(4, 0);
// 玩家坐标点,终点
this.pIndex = cc.v2(3, 9);
// 开始
this.aStar();
},
限制 500 次很好理解,每一次循环 time-- 这样可以防止找不到路径造成卡死。
然后让我们为起点建立对象。然后放入 close 列表中。
close 列表装的是那些已经搜索过的点,open 列表中放入待选择的点,然后在 open 列表中选择 f 值较低的点,放入 close 中,完成一轮搜索。直到我们找到终点。
aStar () {
// 限制次数 500;
// 首先将小怪的位置装入 close 列表
let time = 500;
let obj = new Object();
obj.x = this.mIndex.x;
obj.y = this.mIndex.y;
obj.g = this.manHattan(this.mIndex, this.mIndex);
obj.h = this.manHattan(this.mIndex, this.pIndex);
obj.f = obj.g + obj.h;
obj.parent = null;
// 将起点放入
this.pushInClose(obj);
// ......
}
pushInClose (obj) {
this.close.push(obj);
},
图示
我们从起点开始,寻找当前点的周围一圈,然后计算 g h 得到 f 值。
while (true) {
time--;
// 周围一圈装入 open
this.aroundPos(temp);
// 在 open 中找到 f 最小的,装入 close 并返回该点;
temp = this.findMinInOpen();
if (temp.x == this.pIndex.x && temp.y == this.pIndex.y) {
// 到达目的地
break;
}
if (time <= 0) {
console.log('寻找不到');
break;
}
}
向四周寻找
aroundPos (parent) {
// 上下左右四个方向
let dir = [[0,1],[1,0],[0,-1],[-1,0]];
for (let i = 0; i < 4;i++) {
let mx = parent.x + dir[i][0];
let my = parent.y + dir[i][1];
// 是否出界
if (mx < 0 || mx > 6 || my < 0 || my > 9) {
continue;
}
// 是否为墙
if (this.map[mx][my] == 1) {
continue;
}
// 是否已经在 close 中了
if (this.isInClose(mx, my)) {
continue;
}
// 是否已经在 close 中了
if (this.isInOpen(mx, my)) {
continue;
}
// 装入 open
this.pushInOpen(cc.v2(mx, my), parent);
}
},
findMinInOpen () {
let min = 999;
let index = null;
// 找到 open 中最小的 f 的点的下标
for (let i = 0; i < this.open.length; i++) {
if (this.open[i].f <= min) {
min = this.open[i].f;
index = i;
}
}
// 运用 splice 将 f 最小的点切出来
let obj = this.open.splice(index, 1);
// 放入 close 列表并返回
this.pushInClose(obj[0]);
return obj[0];
},
放入 close 列表方法是直接放入 close 数组,而 open 列表需要我们创建点的信息,因为是新的点。
pushInOpen (v, parent) {
let obj = new Object();
obj.x = v.x;
obj.y = v.y;
obj.g = this.manHattan(v, this.mIndex);
obj.h = this.manHattan(v, this.pIndex);
obj.f = obj.g + obj.h;
obj.parent = parent;
this.open.push(obj);
},
判断是否在 open close 两个列表里就是 for 循环
isInOpen (mx, my) {
for (let i = 0; i < this.open.length; i++) {
if (this.open[i].x == mx && this.open[i].y == my) {
return true;
}
}
return false;
},
isInClose (mx, my) {
for (let i = 0; i < this.close.length; i++) {
if (this.close[i].x == mx && this.close[i].y == my) {
return true;
}
}
return false;
},
最后我们发现
在 close 列表里是很多发现的点,数组的最后一定是目标点。
在 close 数组的最后就是目标点,我们只要根据目标点,进行不断的向上访问 parent 就能回溯到起点。
代码实现
// 根据 parent 最终确认路线
let l = this.close.length - 1;
let p = this.close[l];
this.final = [];
while(p) {
this.final.push(p);
p = p.parent;
}
// 将 close 中的正确路线装入 final 后其实是反序的
// 翻转
this.final.reverse();
// 沿着 final 走
this.go(0);
沿着路径走就很简单了,利用 runAction不断的走,直到走完
go (i) {
this.me.runAction(cc.sequence(
cc.moveTo(0.5,this.convertToPoints(this.final[i].x, this.final[i].y)),
cc.callFunc(() => {
if (i == this.final.length - 1) return;
i++;
this.go(i);
},this)
));
},
行列坐标与实际坐标转化
// 转化坐标
convertToPoints (dx, dy) {
let y = 300 - 100 * dx;
let x = 100 * dy - 450;
return cc.v2(x, y);
},
好了,不知小伙伴们这回看明白没有!
O(∩_∩)O~~