简单四方向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

相关推荐
野犬寒鸦5 分钟前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
陈桴浮海6 分钟前
【Linux&Ansible】学习笔记合集二
linux·学习·ansible
xhbaitxl24 分钟前
算法学习day39-动态规划
学习·算法·动态规划
ZH154558913133 分钟前
Flutter for OpenHarmony Python学习助手实战:数据库操作与管理的实现
python·学习·flutter
试着2 小时前
【huawei】机考整理
学习·华为·面试·机试
風清掦2 小时前
【江科大STM32学习笔记-05】EXTI外部中断11
笔记·stm32·学习
Purple Coder2 小时前
基于CNN对YBCO超导块材孔隙研究
学习
wdfk_prog2 小时前
[Linux]学习笔记系列 -- [drivers][tty]sysrq
linux·笔记·学习
优橙教育2 小时前
通信行业四大热门岗位解析:谁才是数字时代的黄金赛道?
网络·学习·5g
西西学代码2 小时前
A---(1)
学习