思想
- 递归生成
- 判断是否超过边界
- 超过就不再生成
- 所以核心是边界函数
- 定义不同的边界函数就会生成不同的棱柱
public void CreateMap (int length)
{
len = length + 1;
if (grids != null)
{
ResetMap ();
}
AutoCreateMap (Vector3.zero, new Point (0, 0));
}
Dictionary<Point, Vector3> offsetDic;
public void InitOffsetDic ()
{
if (offsetDic == null)
{
offsetDic = new Dictionary<Point, Vector3> ();
borderPoint = new List<Point> ();
grids = new Dictionary<Point, Grid> ();
}
offsetDic.Add (new Point (1, -1), new Vector3 (1.5f, 0, -0.87f));
offsetDic.Add (new Point (-1, -1), new Vector3 (-1.5f, 0, -0.87f));
offsetDic.Add (new Point (1, 1), new Vector3 (1.5f, 0, 0.87f));
offsetDic.Add (new Point (-1, 1), new Vector3 (-1.5f, 0, 0.87f));
offsetDic.Add (new Point (0, 2), new Vector3 (0, 0, 1.73f));
offsetDic.Add (new Point (0, -2), new Vector3 (0, 0, -1.73f));
}
void AutoCreateMap (Vector3 originPos, Point gridPos)
{
if (IsInBorder (originPos) && !grids.ContainsKey (gridPos))
{
Grid floor = GridPool.Inst.GetFloor (originPos, gridPos);
grids.Add (gridPos, floor);
foreach (var pair in offsetDic)
{
if (!IsInBorder (originPos + pair.Value))
{
floor.IsBorder = true;
Renderer render = floor.floor.GetComponent<Renderer> ();
render.material.color = new Color (81, 128, 248) / 255;
borderPoint.Add (gridPos + pair.Key);
Rigidbody rigi = floor.floor.GetComponent<Rigidbody> ();
rigi.isKinematic = true;
}
else
{
AutoCreateMap (originPos + pair.Value, gridPos + pair.Key);
}
}
}
}
public bool IsInBorder (Vector3 pos)
{
float x = Mathf.Abs (pos.x);
float z = Mathf.Abs (pos.z);
float borderX = 0, borderY = 1.73f * len / 2 + 0.2f;
if (x == 0)
{
borderX = len;
}
else
{
borderX = (z - 1.73f * len) / -1.73f + 0.2f;
}
return x < borderX && z < borderY ? true : false;
}