package
{
/**
* ...
* @author dreamnight
*/
public class MapUtil
{
import flash.geom.Point;
public function MapUtil()
{
}
/**
*
* //这里的 px 是相对于原点的像素坐标
* */
public static function getCellPoint(tileWidth:int, tileHeight:int, px:int, py:int):Point
{
/**
* 网格的x坐标
* */
var xtile:int = 0;
/**
* 网格的y坐标
* */
var ytile:int = 0;
var cx:int, cy:int;
/**
* 计算出当前X所在的以tileWidth为宽的矩形的中心的X坐标
* */
cx = int(px / tileWidth) * tileWidth + tileWidth/2;
/**
* 计算出当前Y所在的以tileHeight为高的矩形的中心的Y坐标
* */
cy = int(py / tileHeight) * tileHeight + tileHeight / 2;
/*var y1:int = cy - tileHeight * 0.5;
var y2:int = cy + tileHeight * 0.5;
var x1:int = cx - tileWidth * 0.5;
var x2:int = cx + tileWidth * 0.5;
trace("y1:", y1, " y2", y2, " x1", x1, " x2:", x2);*/
//四个直线方程
var line1:Number = py + (px - cx) *tileHeight / tileWidth - cy + tileHeight * 0.5; // 满足 >0条件
var line2:Number = py + (cx - px) *tileHeight / tileWidth - cy + tileHeight * 0.5; // >0
var line3:Number = py + (cx - px) * tileHeight / tileWidth - cy - tileHeight * 0.5;// <0
var line4:Number = py + (px - cx) * tileHeight / tileWidth - cy - tileHeight * 0.5;// <0
//当点在矩形内 也就是 y 为偶数的 tile
if ((line1>=0)&&(line2>=0)&&(line3<=0)&&(line4<=0)) {
xtile = int(px / tileWidth);
ytile = int(py / tileHeight) * 2;
// trace("棱形内_____");
}
//、当在棱形外
else {
//对X进行定位 当在左半边时
if ((px <= cx + tileWidth * 0.5) && (cx <= px)) {
xtile = int(px / tileWidth);
// trace("左半边_____");
}
else {
//右半边
xtile = int(px / tileWidth) - 1;
// trace("右半边_____");
}
//对 y 进行定位 当在上半部分时
if ((cy <= py) && (py <= cy + tileHeight * 0.5)) {
ytile = int(py / tileHeight) * 2 + 1;
// trace("上半边____");
}else {
ytile = (int(py / tileHeight) - 1) * 2 +1;
// trace("下半边___");
}
}
return new Point(xtile,ytile);
}
///end class
}
}
地图编辑器第二部!! 给网格坐标定位
最新推荐文章于 2023-07-25 15:11:21 发布