简单四方向a*寻路学习记录7 识别单个障碍绕行

在上次递归调用后 添加障碍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);
        }
        
    }
}
相关推荐
西岸行者6 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意6 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码6 天前
嵌入式学习路线
学习
毛小茛6 天前
计算机系统概论——校验码
学习
babe小鑫6 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms6 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下6 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。6 天前
2026.2.25监控学习
学习
im_AMBER6 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J6 天前
从“Hello World“ 开始 C++
c语言·c++·学习