六边形格子地图 数据结构和寻路

最近做了个跳棋小游戏,于是接触了一下六边形的东西,简单记录一下

六边形格子地图,坐标为数据结构大概可以抽象为


六边形格子地图 数据结构和寻路_第1张图片

每个棋子的位置为

position = cc.v2((x +  (y % 2 == 0 ? 0 : 0.5) )* gridWidth , y * gridHeight);

六边形格子地图 数据结构和寻路_第2张图片

再抽象一下

六边形格子地图 数据结构和寻路_第3张图片

每两个格子的寻路步数为

getDistance: function (from, to) {
    let dx = to.x - from.x;
    let dy = to.y - from.y;
    var lucky = dx > 0 ^ from.y % 2 == 0;
    var xOffset = lucky ? Math.ceil(Math.abs(dy / 2)) : Math.floor(Math.abs(dy / 2));
    var step = xOffset >= Math.abs(dx) ? Math.abs(dy) : (Math.abs(dy) + Math.abs(dx) - xOffset);
    return step;
}

————————

想要学习Cocos的同学,欢迎关注我的零基础Cocos教程

https://ke.qq.com/course/313749

你可能感兴趣的:(六边形格子地图 数据结构和寻路)