
在上次递归调用后 添加障碍xy变量
判断上下左右格子
go
if(this.zhangaix==this.shangx&&this.zhangaiy==this.shangy){
this.zhangaifangxiang=0;//0 shang 1 zuo 2 you 3xia
this.shangf=-1;
console.log("zhangai shang");
}else if(this.zhangaix==this.zuox&&this.zhangaiy==this.zuoy){
this.zhangaifangxiang=1;
this.zuof=-1;
console.log("zhangai zuo");
}else if(this.zhangaix==this.youx&&this.zhangaiy==this.youy){
this.zhangaifangxiang=2;
this.youf=-1;
console.log("zhangai you");
}else if(this.zhangaix==this.xiax&&this.zhangaiy==this.xiay){
this.zhangaifangxiang=3;
this.xiaf=-1;
console.log("zhangai xia");
}
const suoyouf=[this.shangf,this.zuof,this.youf,this.xiaf];
let zuixiaof=Number.MAX_SAFE_INTEGER;
let fangxiang=0; // 0/shang 1/zuo 2/you 3/xia
let zuixiaog=0;
for(let i=0;i<suoyouf.length;i++){
const fValue = suoyouf[i];
if(fValue >= 0 && fValue < zuixiaof) { // ✅ 简单直接
zuixiaof = fValue;
fangxiang = i;
}
}
如果有障碍 将当前方向的f换成-1
go
for(let i=0;i<suoyouf.length;i++){
const fValue = suoyouf[i];
if(fValue >= 0 && fValue < zuixiaof) { // ✅ 简单直接
zuixiaof = fValue;
fangxiang = i;
}
在遍历上下左右格子的f时 跳过-1
go
import { _decorator, Collider2D, Component, Contact2DType, director, IPhysics2DContact, Node } from 'cc';
const { ccclass, property,executeInEditMode } = _decorator;
@ccclass('xiaobingzhangai')
@executeInEditMode
export class xiaobingzhangai extends Component {
juesex:number=null;
juesey:number=null;
mubiaox:number=null;
mubiaoy:number=null;
shangf:number=0;
shangg:number=0;
shangh:number=0;
zuof:number=0;
zuoh:number=0;
zuog:number=0;
youf:number=0;
youh:number=0;
youg:number=0;
xiaf:number=0;
xiah:number=0;
xiag:number=0;
shangx:number=0;
shangy:number=0;
zuox:number=0;
zuoy:number=0;
youx:number=0;
youy:number=0;
xiax:number=0;
xiay:number=0;
zhangaix:number=0;
zhangaiy:number=0;
zhangaifangxiang:number=0;
onLoad(){
// 监听碰撞事件
const collider = this.getComponent(Collider2D);
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
director.on('gezi_ditu_xiaobing', this.huoquditu, this);
}
// 碰撞回调
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
if(otherCollider.node.name.includes("shantou")){
this.scheduleOnce(()=>{
selfCollider.node.destroy();
},0.7);
}
}
start() {
director.emit('xiaobing_position_gezi', this.node.worldPosition);
}
update(deltaTime: number) {
}
huoquditu(ditu:[][]){
let len=ditu.length;
console.log(len+"=");
for(let y=0;y<len;y++){
for(let x=0;x<ditu[y].length;x++){
console.log(ditu[y][x]);
if(ditu[y][x]==1){
this.mubiaox=x;
this.mubiaoy=y;
console.log("/"+this.mubiaox,this.mubiaoy)
}else if(ditu[y][x]==7){
this.juesex=x;
this.juesey=y;
console.log("/"+this.juesex,this.juesey);
}else if(ditu[y][x]==11){
this.zhangaix=x;
this.zhangaiy=y;
}
}
}
//this.jisuanf(this.juesex,this.juesey);
this.huoqushangxiazuoyou(this.juesex,this.juesey,0);
}
jisuannext(x:number,y:number,f:number,g:number){
if(f>0){
//g=g+1;
this.huoqushangxiazuoyou(x,y,(g+1));
}
}
huoqushangxiazuoyou(qidianx:number,qidiany:number,g:number){
const ng=g+1;
this.shangx=qidianx;
this.shangy=qidiany-1;
this.zuox=qidianx-1;
this.zuoy=qidiany;
this.youx=qidianx+1;
this.youy=qidiany;
this.xiax=qidianx;
this.xiay=qidiany+1;
this.shangg=ng;
this.shangh=Math.abs(this.shangx-this.mubiaox)+Math.abs(this.shangy-this.mubiaoy);
this.shangf=this.shangg+this.shangh;
this.zuog=ng;
this.zuoh=Math.abs(this.zuox-this.mubiaox)+Math.abs(this.zuoy-this.mubiaoy);
this.zuof=this.zuog+this.zuoh;
this.youg=ng;
this.youh=Math.abs(this.youx-this.mubiaox)+Math.abs(this.youy-this.mubiaoy);
this.youf=this.youg+this.youh;
this.xiag=ng;
this.xiah=Math.abs(this.xiax-this.mubiaox)+Math.abs(this.xiay-this.mubiaoy);
this.xiaf=this.xiag+this.xiah;
if(this.zhangaix==this.shangx&&this.zhangaiy==this.shangy){
this.zhangaifangxiang=0;//0 shang 1 zuo 2 you 3xia
this.shangf=-1;
console.log("zhangai shang");
}else if(this.zhangaix==this.zuox&&this.zhangaiy==this.zuoy){
this.zhangaifangxiang=1;
this.zuof=-1;
console.log("zhangai zuo");
}else if(this.zhangaix==this.youx&&this.zhangaiy==this.youy){
this.zhangaifangxiang=2;
this.youf=-1;
console.log("zhangai you");
}else if(this.zhangaix==this.xiax&&this.zhangaiy==this.xiay){
this.zhangaifangxiang=3;
this.xiaf=-1;
console.log("zhangai xia");
}
const suoyouf=[this.shangf,this.zuof,this.youf,this.xiaf];
let zuixiaof=Number.MAX_SAFE_INTEGER;
let fangxiang=0; // 0/shang 1/zuo 2/you 3/xia
let zuixiaog=0;
for(let i=0;i<suoyouf.length;i++){
const fValue = suoyouf[i];
if(fValue >= 0 && fValue < zuixiaof) { // ✅ 简单直接
zuixiaof = fValue;
fangxiang = i;
}
}
let zuixiaofangxiangx=0;
let zuixiaofangxiangy=0;
if(fangxiang==0){
zuixiaofangxiangx=this.shangx;
zuixiaofangxiangy=this.shangy;
}else if(fangxiang==1){
zuixiaofangxiangx=this.zuox;
zuixiaofangxiangy=this.zuoy;
}else if(fangxiang==2){
zuixiaofangxiangx=this.youx;
zuixiaofangxiangy=this.youy;
}else if(fangxiang==3){
zuixiaofangxiangx=this.xiax;
zuixiaofangxiangy=this.xiay;
}
const directionData = {
shangf: this.shangf, // 上方向状态或坐标x
xiaf: this.xiaf, // 下
zuof: this.zuof, // 左
youf: this.youf, // 右
shangx: this.shangx, // 上方向x坐标
shangy: this.shangy, // 上方向y坐标
zuox: this.zuox, // 左方向x坐标
zuoy: this.zuoy, // 左方向y坐标
youx: this.youx, // 右方向x坐标
youy: this.youy, // 右方向y坐标(如果有)
xiax: this.xiax, // 下方向x坐标(如果有)
xiay: this.xiay, // 下方向y坐标(如果有)
zuixiaof:zuixiaof,
zuixiaofangxiangx:zuixiaofangxiangx,
zuixiaofangxiangy:zuixiaofangxiangy,
mubiaox:this.mubiaox,
mubiaoy:this.mubiaoy,
juesex:this.juesex,
juesey:this.juesey
};
// 标记这是哪一步
console.log(`📌 第${g}步:站在[${qidianx},${qidiany}],目标[${this.mubiaox},${this.mubiaoy}]`);
// 现在位置已经到达目标?
if(qidianx === this.mubiaox && qidiany === this.mubiaoy) {
console.log("🎉🎉🎉 已经站在目标上了!停止搜索!");
return;
}
// 选择的最小方向就是目标?
if(zuixiaofangxiangx === this.mubiaox && zuixiaofangxiangy === this.mubiaoy) {
console.log("✅ 下一步就是目标!准备结束...");
// 可以把这最后一步画出来,但不继续递归
director.emit('xiaobing_sxzy_gezi', directionData);
return;
}
console.log("s"+this.shangf+"z"+this.zuof+"y"+this.youf+"x"+this.xiaf+"f"+zuixiaof+"fang"+fangxiang+"xiagegezi"+zuixiaofangxiangx+"y"+zuixiaofangxiangy);
director.emit('xiaobing_sxzy_gezi', directionData);
//this.nextx=zuixiaofangxiangx;
//this.nexty=zuixiaofangxiangy;
//if(zuixiaofangxiangx!==this.mubiaox||zuixiaofangxiangy!==this.mubiaoy){
//this.huoqushangxiazuoyou(zuixiaofangxiangx,zuixiaofangxiangy,ng);
//}
// 🔥 关键修正:修改这个条件
// 原来的:if(zuixiaofangxiangx !== this.mubiaox || zuixiaofangxiangy !== this.mubiaoy)
// 改为:
const isAlreadyAtTarget = (qidianx === this.mubiaox && qidiany === this.mubiaoy);
const willGoToTarget = (zuixiaofangxiangx === this.mubiaox && zuixiaofangxiangy === this.mubiaoy);
if(!isAlreadyAtTarget && !willGoToTarget) {
console.log(`继续前进到[${zuixiaofangxiangx},${zuixiaofangxiangy}]`);
this.huoqushangxiazuoyou(zuixiaofangxiangx, zuixiaofangxiangy, ng);
}
}
}