Creator实战项目【保卫萝卜】-- 格子地图

2023-10-31

import ScriptBase from './ScriptBase'
const {ccclass, property} = cc._decorator;

@ccclass
export default class TiledMapCtrl extends ScriptBase {

    //初始化格子地图组件变量
    private tiledMap:cc.TiledMap = null;

    //定义格子地图相关变量
    private tiledSize:cc.Size = null;
    private mapSize:cc.Size = null;

    onInit(){
        //初始化相关变量
        this.tiledSize = this.tiledMap.getTileSize();
        this.mapSize = this.tiledMap.getMapSize();
    }

    //像素坐标转换成格子坐标
    getTiledByPos(pos:cc.Vec2):cc.Vec2{
        //Math.floor:向下取整   获得小于等于这个数的最大整数  1.000001   1  1.9999999999999    1
        let tiledX:number =Math.floor(pos.x/this.tiledSize.width);
        let tiledY:number = this.mapSize.height- Math.floor(pos.y/this.tiledSize.height)-1;
        return cc.v2(tiledX,tiledY);
    }
    
    //格子坐标转换成像素坐标
    getPosByTiled(tiledPos:cc.Vec2):cc.Vec2{
        let x = tiledPos.x * this.tiledSize.width +this.tiledSize.width/2;
        let y = (this.mapSize.height - tiledPos.y-1)*this.tiledSize.height+this.tiledSize.height/2;
        return cc.v2(x,y);
    }

    //出界
    isOutMap(tiledPos:cc.Vec2):boolean{
        if(tiledPos.x < 0
            ||tiledPos.y < 0
            ||tiledPos.x >this.mapSize.width-1
            ||tiledPos.y > this.mapSize.height-1){
                return true;
            }
        return false;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Creator实战项目【保卫萝卜】-- 格子地图 的相关文章

随机推荐