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

相关推荐
醇氧6 分钟前
第一、二、三范式学习
数据库·学习·oracle
苦瓜小生1 小时前
【黑马点评学习笔记 | 实战篇 】| 10-用户签到+UV统计
笔记·后端·学习
_饭团1 小时前
C 语言内存函数全解析:从 memcpy 到 memcmp 的使用与模拟实现
c语言·开发语言·c++·学习·算法·面试·改行学it
别催小唐敲代码1 小时前
FastAPI 从零开始完整学习教程
学习·fastapi
24白菜头1 小时前
第十五届蓝桥杯C&C++大学B组
数据结构·c++·笔记·学习·算法·leetcode·蓝桥杯
Engineer邓祥浩2 小时前
JVM学习问题记录(1) IDEA2025设置JVM启动参数
jvm·学习
qcwl662 小时前
深入理解Linux进程与内存 学习笔记#3
linux·笔记·学习
-Springer-2 小时前
STM32 学习 —— 个人学习笔记10-1(I2C 通信协议及 MPU6050 简介 & 软件 I2C 读写 MPU6050)
笔记·stm32·学习
小陈phd2 小时前
多模态大模型学习笔记(二十二)——大模型微调全解:从全量调参到LoRA的参数高效训练实战
笔记·学习
Engineer邓祥浩2 小时前
JVM学习笔记(3) 第二部分 自动内存管理 第2章 Java内存区域与内存溢出异常
jvm·笔记·学习