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

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