/// <summary> /// 总入口 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> public void OnInit(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int rows = maps.GetLength(0); int column = maps.GetLength(1); int r = rows - 2; int c = column - 2; int startRowInd = Random.Range(2, r); int startColumnInd = Random.Range(2, c); SetStartPoint(startRowInd, startColumnInd); //设置开始点 CreateWalkableRoad(maps, changeDirectionPer, workableCount, randomTimes); for (int i = 0; i < randomTimes - 1;) { startRowInd = Random.Range(2, r); startColumnInd = Random.Range(2, c); if (IsEnd(rows, column, startRowInd, startColumnInd)) continue; SetStartPoint(startRowInd, startColumnInd); if (maps[startRowInd, startColumnInd].IsContainMapType(eMapType.Road)) { i++; CreateWalkableRoad(maps, changeDirectionPer, workableCount, randomTimes); } } CreateWall(maps, changeDirectionPer, workableCount, randomTimes); }
public void StartCreateBlocks(GridBase[,] mapData) { int r = mapData.GetLength(0); int c = mapData.GetLength(1); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { GameObject go = null; if (mapData[i, j].IsContainMapType(eMapType.BlockCanPass)) { go = Instantiate(canPassObj) as GameObject; } else if(mapData[i, j].IsContainMapType(eMapType.BlockCannotPass)) { go = Instantiate(cannotPassObj) as GameObject; } if (go == null) continue; go.transform.parent = transform; Vector3 worldPos = GetWorldPos(mapData[i, j].RId, mapData[i, j].CId); //设置层级 int layercount = mapData[i, j].SortOrder; Transform gridTf = MapObj.transform.FindChild(MapObj.GetGridName(i, j, mapData[i, j])); go.transform.parent = gridTf; go.transform.localPosition = gridTf.InverseTransformPoint(worldPos); go.transform.GetComponentInChildren<Renderer>().material.renderQueue = layercount; go.name = i + "_" + j+"_"+ layercount; } } }
/// <summary> /// 创建墙壁 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> private void CreateWall(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int rows = maps.GetLength(0); int column = maps.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { if (IsWall(maps,rows,column,i, j)) maps[i, j].AddMapType(eMapType.Wall); } } }
/// <summary> /// 创建可行走格子 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> private void CreateWalkableRoad(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int count = 0; int cacheLastDir = -10; int rows = maps.GetLength(0); int column = maps.GetLength(1); do { if (count >= workableCount) break; int changePer = Random.Range(0, 100); eDirectionType dir = eDirectionType.None; if (changePer >= changeDirectionPer && cacheLastDir >= 0) { //保持原来的方向前进 dir = direction[cacheLastDir]; } else { int dirInd = Random.Range(0, direction.Length); if (Mathf.Abs(cacheLastDir - dirInd) == 1) dir = direction[cacheLastDir]; else dir = direction[dirInd]; cacheLastDir = dirInd; } int tempColumn = 0; int tempRow = 0; GetRowAndColumnIndex(dir, ref tempColumn, ref tempRow); if (IsEnd( rows , column,startRowInd + tempRow, startColumnInd + tempColumn)) cacheLastDir = -10; //清除缓存方向,重新随机产生下一个方向 else { count++; startColumnInd += tempColumn; startRowInd += tempRow; maps[startRowInd, startColumnInd].AddMapType(eMapType.Road);//可走的 } } while (true); }
//创建地图 private void RandomCreateRoad(GridBase[,] maps,int maxLinkCount, int changeDirectionPer, int workableCount) { //创建地图 //随机 地图在同方向上的个数 , 不大于 地图总格子数 int rows = maps.GetLength(0); int columns = maps.GetLength(1); int count = 0; int cacheLastDir = -10; int linkCount = 0; do { if (count >= workableCount) break; int changePer = UnityEngine.Random.Range(0, 100); eDirectionType dir = eDirectionType.None; //随机生成最大连接数 linkCount = UnityEngine.Random.Range(2, maxLinkCount); if (changePer >= changeDirectionPer && cacheLastDir >= 0) { //保持原来的方向前进 dir = direction[cacheLastDir]; } else { int dirInd = UnityEngine.Random.Range(0, direction.Length); if (Mathf.Abs(cacheLastDir - dirInd) == 1) dir = direction[cacheLastDir]; else dir = direction[dirInd]; cacheLastDir = dirInd; } for (int i = 0; i < linkCount; i++) { int tempColumn = 0; int tempRow = 0; GetRowAndColumnIndex(dir, ref tempColumn, ref tempRow); if (IsEnd(rows, columns, startRowInd + tempRow, startColumnInd + tempColumn)) { cacheLastDir = -10; //清除缓存方向,重新随机产生下一个方向 break; } else { count++; startColumnInd += tempColumn; startRowInd += tempRow; maps[startRowInd, startColumnInd].ClearAndAddMaptType( eMapType.Road);//可走的 if(!RoadList.Contains(maps[startRowInd,startColumnInd])) { RoadList.Add(maps[startRowInd, startColumnInd]);//加入列表 } } } } while (true); }
/// <summary> /// 在矩形地图四条边上生成 开始路点 /// </summary> /// <param name="maps"></param> /// <param name="i"></param> private string GetRoadStartPoint(GridBase[,] maps,int i) { int rows = maps.GetLength(0); int column = maps.GetLength(1); int r = rows - 2; int c = column - 2; int startRowInd = 0;// UnityEngine.Random.Range(2, r); int startColumnInd = 0;// UnityEngine.Random.Range(2, c); string result = ""; switch (i) { case 0://上 startColumnInd = UnityEngine.Random.Range(2, c); startRowInd = r; if (maps[startRowInd, startColumnInd].IsContainMapType(eMapType.None) && maps[startRowInd,startColumnInd - 1].IsContainMapType(eMapType.None) && maps[startRowInd,startColumnInd + 1].IsContainMapType(eMapType.None))//相邻都没有路 { result = startRowInd+"_"+startColumnInd; } break; case 1://左 startColumnInd = 1; startRowInd = UnityEngine.Random.Range(2, r); if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd - 1, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd + 1, startColumnInd].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; case 2://下 startColumnInd = UnityEngine.Random.Range(2, c); startRowInd = 1; if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd, startColumnInd - 1].IsContainMapType( eMapType.None) && maps[startRowInd, startColumnInd + 1].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; case 3://右 startColumnInd = c; startRowInd = UnityEngine.Random.Range(2, r); if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd - 1, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd + 1, startColumnInd].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; } if (string.IsNullOrEmpty(result) == false) return result; //递归 return GetRoadStartPoint(maps, i); }
/// <summary>设置格子上的物体的层级</summary> /// <param name="maps"></param> private void CreateSortLayer(GridBase[,] maps) { int r = maps.GetLength(0); int c = maps.GetLength(1); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int sort = 4 * c - 2 * (j + i); maps[i, j].SortOrder = sort; } } }