简单四方向a*寻路学习记录3 将角色世界坐标传给地图数组 计算上下左右四个格子到目的地的f 选择最小的f方向


这里蓝色格子是角色起点 绿色是目的地 四周黄色是准备走的格子

红色是计算出最近的各自路线 是在上下左右格子的f中选择最小的

上次完成了将角色世界坐标转进地图数组

现在将地图数组中角色所在的格子从0换成7 (这其实是上片中没说到的没啥技术)

go 复制代码
// 在gezi.ts中添加一个**可靠的**坐标转换方法
public worldPositionToGrid(worldPos: Vec3): { x: number, y: number } {
    const gridWorldPos = this.node.getWorldPosition();
    const offsetX = worldPos.x - gridWorldPos.x;
    const offsetY = worldPos.y - gridWorldPos.y;
    
    // DEBUG: 打印调试信息

    console.log("=== 坐标转换调试 ===");
    console.log("1. 网格中心:", gridWorldPos);

    console.log("2. 角色位置:", worldPos);
    console.log("3. 偏移量 offsetX=", offsetX, "offsetY=", offsetY);
    


    // 网格半宽/半高 (注意:这里有坐标轴方向问题)
    const halfWidth = (this.gridWidth * this.cellSize) / 2;
    const halfHeight = (this.gridHeight * this.cellSize) / 2;
    

    // Cocos Y轴向上为正,但数组通常是左下角为(0,0)
    // 所以需要调整Y轴方向
    const gridX = Math.floor((offsetX + halfWidth) / this.cellSize);
    const gridY = Math.floor((offsetY + halfHeight) / this.cellSize);
    
    // Y轴翻转(因为屏幕上方是正Y,但数组从上到下索引)
    const flippedY = (this.gridHeight - 1) - gridY;
    
    console.log(`4. 计算:gridX=${gridX} (${offsetX}+${halfWidth})/${this.cellSize}`);
    console.log(`5. 计算:gridY=${gridY} (${offsetY}+${halfHeight})/${this.cellSize}`);


    console.log(`6. 翻转后:gridX=${gridX}, flippedY=${flippedY}`);
    console.log(`7. 结果:[${this.gezi[flippedY][gridX]}]`);

    
    // 使用 flippedY 因为数组是从上到下存储
    return { x: gridX, y: flippedY };
}

onload调用角色世界坐标转数组

然后将gezi=[[0,0,0,0,0,0,0,0,0,0],

0,0,0,0,0,0,0,0,0,1\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], \[0,0,0,0,0,0,0,0,0,0\], ]; 中的角色所在位置从0换成7 ```go // 获取角色位置并标记 const worldPos = this.xiaobing.getWorldPosition(); const gridPos = this.worldPositionToGrid(worldPos); console.log("转换结果:", gridPos); // 关键修正:检查边界并更新数组 if (this.isInGrid(gridPos.x, gridPos.y)) { // 注意:你的数组是 gezi[y][x] 结构!所以要 [gridPos.y][gridPos.x] this.gezi[gridPos.y][gridPos.x] = 7; } else { console.warn("角色在网格外!位置:", gridPos); } this.drawGrid(); director.emit('gezi_ditu_xiaobing', this.gezi); } ``` 然后调用this.drawGrid(); ```go // 绘制每个格子的填充背景 for (let x = 0; x < this.gridWidth; x++) { for (let y = 0; y < this.gridHeight; y++) { let huoqugezi=this.gezi[y][x]; // 然后反转Y坐标,让第一行显示在最上面 const screenY = this.gridHeight - 1 - y; const posX = (x - this.gridWidth/2) * this.cellSize; //const posY = (y - this.gridWidth/2) * this.cellSize; const posY = (screenY - this.gridHeight/2) * this.cellSize; if(huoqugezi==1){ this.graphics.fillColor = this.zhangaiColor; //this.graphics.rect(posX, posY, this.cellSize, this.cellSize); console.log(huoqugezi+"shi"); }else if(huoqugezi==7){ this.graphics.fillColor = this.jueseColor; //this.graphics.rect(posX, posY, this.cellSize, this.cellSize); console.log(huoqugezi+"shi"); }else if(huoqugezi==4){ this.graphics.fillColor = this.xiaColor; }else if(huoqugezi==9){ this.graphics.fillColor = this.zuixiaoColor; } else{ this.graphics.fillColor = this.fillColor; } this.graphics.rect(posX, posY, this.cellSize, this.cellSize); console.log(huoqugezi); this.graphics.fill(); } } ``` 将格子中的0是空 1是目的地 7是角色所在起点 然后能做出来上篇中的 现在开始计算角色四周格子的f 首先将上面包含角色 目的地的地图数组传给角色脚本 director.emit('gezi_ditu_xiaobing', this.gezi); ```go onLoad() { director.on('xiaobing_sxzy_gezi', this.huasxzy, this); // 1. 创建并获取 Graphics 组件 this.graphics = this.node.getComponent(Graphics); if (!this.graphics) { this.graphics = this.node.addComponent(Graphics); } // 2. 绘制网格 director.on('xiaobing_position_gezi', this.xiaobingposition, this); // 获取角色位置并标记 const worldPos = this.xiaobing.getWorldPosition(); const gridPos = this.worldPositionToGrid(worldPos); console.log("转换结果:", gridPos); // 关键修正:检查边界并更新数组 if (this.isInGrid(gridPos.x, gridPos.y)) { // 注意:你的数组是 gezi[y][x] 结构!所以要 [gridPos.y][gridPos.x] this.gezi[gridPos.y][gridPos.x] = 7; } else { console.warn("角色在网格外!位置:", gridPos); } this.drawGrid(); director.emit('gezi_ditu_xiaobing', this.gezi); } ``` 角色脚本获取到数组后开始遍历地图获取角色 目的地数组坐标 ```go director.on('gezi_ditu_xiaobing', this.huoquditu, this); huoquditu(ditu:[][]){ let len=ditu.length; console.log(len+"="); for(let y=0;y

相关推荐
学编程的闹钟2 小时前
浅谈VMProtectV2.13.8的IAT修复
学习
盐焗西兰花2 小时前
鸿蒙学习实战之路-多端交互最佳实践
学习·交互·harmonyos
AI绘画哇哒哒2 小时前
AI 智能体长期记忆系统架构设计与落地实践
人工智能·学习·算法·ai·程序员·产品经理·转行
JH灰色2 小时前
【大模型】学习路线
学习
林林宋3 小时前
nvidia&cuda&gpu 关系学习
学习
学编程的闹钟3 小时前
102【php开发准备】
学习
半夏知半秋3 小时前
Elasticsearch 分词器
大数据·学习·elasticsearch·搜索引擎·全文检索
逐云者1234 小时前
Nested Learning:Google Research 正在尝试重新定义深度学习的“学习结构”
人工智能·深度学习·学习·nested learning·google新模型·快慢记忆·学习结构
Ada大侦探4 小时前
新手小白学习Power BI第二弹--------《电商销售数据分析与可视化项目》需求展示
数据库·学习·数据分析