/**
* 像素坐标转换为地图坐标
* */
public SpaceToTiled(point: egret.Point): egret.Point {
var y = Math.floor(point.y * 2 / this.TILE_HEIGHT) * this.TILE_HEIGHT / 2;
var x = Math.floor(point.x * 2 / this.TILE_WIDTH) * this.TILE_WIDTH / 2;
var leftPos: egret.Point = new egret.Point();
var rightPos: egret.Point = new egret.Point();
//console.log("x = " + Math.floor(point.x * 2 / this.TILE_WIDTH) + " y = " + Math.floor(point.y * 2 / this.TILE_HEIGHT))
if((Math.floor(point.y * 2 / this.TILE_HEIGHT) % 2 == 0 && Math.floor(point.x * 2 / this.TILE_WIDTH) % 2 == 0) || (Math.floor(point.y * 2 / this.TILE_HEIGHT) % 2 == 1 && Math.floor(point.x * 2 / this.TILE_WIDTH) % 2 == 1)) {
//表示定点在真确的tile中心点,只需要再花一个右下角的点
leftPos.x = x;
leftPos.y = y;
rightPos.x = x + this.TILE_WIDTH / 2;
rightPos.y = y + this.TILE_HEIGHT / 2;
} else {
leftPos.x = x;
leftPos.y = y + this.TILE_HEIGHT / 2;
rightPos.x = x + this.TILE_WIDTH / 2;
rightPos.y = y;
}
if(egret.Point.distance(leftPos,point) <= egret.Point.distance(rightPos,point)) {
return new egret.Point(Math.round(leftPos.x / this.TILE_WIDTH) - 1,leftPos.y * 2 / this.TILE_HEIGHT - 1);
} else {
return new egret.Point(Math.round(rightPos.x / this.TILE_WIDTH) - 1,rightPos.y * 2 / this.TILE_HEIGHT - 1);
}
}
/**
* tile坐标转为像素坐标
* */
public TiledToSpace(point: egret.Point): egret.Point {
var y = this.TILE_HEIGHT * (point.y + 1) / 2;
var x = (point.x + ((point.y) % 2 / 2.0) + 0.5) * this.TILE_WIDTH;
return new egret.Point(x,y);
}
格子地图 像素坐标 地图坐标 转化
最新推荐文章于 2023-04-26 13:46:15 发布