六边形格子地图坐标计算与转换

// 世界场景的一些数据管理,如提供坐标转换之类的接口var WorldMapManager = function () { this.mapSize = null; // 地图大小,像素 this.curViewPos = null; // 当前大地图视野坐标 // 初始化世界地图的数据 this.init = function (mapSize,
摘要由CSDN通过智能技术生成
// 世界场景的一些数据管理,如提供坐标转换之类的接口

var WorldMapManager = function () {
    this.mapSize = null;     // 地图大小,像素
    this.curViewPos = null;  // 当前大地图视野坐标

    // 初始化世界地图的数据
    this.init = function (mapSize, tileSize) {
        this.mapSize = {
                width  : globalConsts.WorldMapSize.width * globalConsts.TileSize.width + globalConsts.TileSize.width / 2,
                height : globalConsts.WorldMapSize.height * ((globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2 + globalConsts.TileSize.hex) + 
                            (globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2
            };
        this.tileSize = globalConsts.TileSize;
    };

    // 大地图坐标转成蜂窝cell
    this.mapPosToTile = function (pos) {
        // 算出缩放成正六边形后边长 a 的值
        var a = this.tileSize.width / Math.sqrt(3);
        var x = pos.x, y = (this.mapSize.height - pos.y) / this.tileSize.height * a * 2 + a / 2;    // 加 a / 2 是因为矩形网格计算时会在底部增加 a / 2
        
        //位于矩形网格边线上的三个CELL中心点
        var points = new Array(cc.p(0, 0), cc.p(0, 0), cc.p(0, 0));
        //当前距离的平方
        var dist;
        //      index:被捕获的索引
        var i, index;
        //二分之根号3 边长的平方,如果距离比它还小,就必然捕获
        var g_MinDistance2 = Math.pow(a * Math.sqrt(3) / 2, 2);
        // 网格宽、高
        var g_unitx = a * Math.sqrt(3);     //sqrt(3) * a
        va
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值