需要能动态改变地图的层数,当输入的layer不同生成的地图层数也就不同,所以我使用算法动态生成棋盘格地图。
通过定义好的tileWeight和tileHeight,i从下到上,j从左到右确定每个格子的世界坐标x,y。
关键代码如下:
for(int i=2*layer-1);i>=0;i--){
int count=(i>=layer?3*layer-2-i:i+layer);//count代表当前行的格子数
for(int j=0;j<count;j++)
{
float x=j*tileWeight-(count-1)*tileWeight/2;
float y=(i-layer+1)*tileHeight;
}
}
我将生成的格子定义x,y,z三个坐标
https://www.redblobgames.com/grids/hexagons/
在这个上有解释,如以下图
好吧,其实这个不是关键,没有定义也不影响我地图的生成,只是定义了之后格子的位置可以很方便的获得
生成地图格子的代码如下:(Unity3D的脚本)
GameObject go = Instantiate(TilePrefab,new Vector3(x,y,0),TilePrefab.transform.localRotation);
Tile t