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

相关推荐
xian_wwq14 小时前
【学习笔记】多租户的 Agent 隔离设计
笔记·学习
深蓝海拓14 小时前
PySide6,图形按钮使用系统内置图标
笔记·python·学习·pyqt
念恒1230614 小时前
Python(列表入门)
python·学习
十安_数学好题速析15 小时前
二进魔法:16人分组难题的4个月破解
笔记·学习·高考
承渊政道15 小时前
【动态规划算法】(子数组系列问题建模与解题思路精讲)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
袋子(PJ)15 小时前
2026年pytorch基础学习(基于jupyter notebook开发)——从原理到落地:PyTorch神经网络架构与工程优化解析
人工智能·pytorch·深度学习·学习·jupyter
炽烈小老头15 小时前
【每天学习一点算法 2026/04/30】寻找重复数
学习·算法
YJlio16 小时前
《Windows Internals》10.5.1 ETW 概述:看懂 Windows 的“事件高速公路”
java·windows·笔记·stm32·嵌入式硬件·学习·eclipse
木木_王16 小时前
嵌入式Linux学习 | 数据结构 (Day03)顺序表与单链表 超详细解析(含 C 语言实现 + 作业 + 避坑指南)
linux·c语言·数据结构·学习
知识分享小能手16 小时前
R语言入门学习教程,从入门到精通,R语言层次关系数据可视化(7)
学习·信息可视化·r语言