自动生成正六棱柱

思想
  • 递归生成
  • 判断是否超过边界
  • 超过就不再生成
  • 所以核心是边界函数
  • 定义不同的边界函数就会生成不同的棱柱
	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);
				}
			}

		}

	}


	//3^0.5 *border  border*2
	//3^0.5 *border  border*2
	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;
			//Debug.Log (borderX);
		}
		else
		{
			//y = (x)*-1.73f+1.73*len

			borderX = (z - 1.73f * len) / -1.73f + 0.2f;
		}


		return x < borderX && z < borderY ? true : false;
	}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值