/// <summary> /// 添加地形 /// </summary> /// <param name="map">所添加的地图</param> /// <param name="terrain">添加的地形类型</param> /// <param name="coefficient">地形系数 越大会生成越多地形 MAX:100 MIN:0</param> /// <param name="math">地形会从什么条件的单元格上生成的断言 True:可以生成 False:不会生成 </param> /// <param name="CoreSize">单个增殖块的标准大小</param> /// <param name="wave">单个核芯增殖数量波动值</param> /// <param name="maxSize">单个增殖块的大小的最大值</param> /// <param name="minSize">单个增殖块的大小的最大值</param> private void AddTerrain(HexCell[,] map, Terrain terrain, int coefficient, Predicate <HexCell> math = null, int CoreSize = 6, int wave = 0, int maxSize = 0, int minSize = 0) { int addSzie = (int)(coefficient * 0.01 * MapSize); //根据系数计算添加的总数 int CoreChunkCount = addSzie / CoreSize; //增殖数量 Debug.LogFormat("{0}:CoreChunkCount = {1}", terrain.ToString(), CoreChunkCount); if (math == null) { math = x => true; } for (int i = 0; i < CoreChunkCount; i++) { HexCell hexCell = GetRandomCell(map, math); if (hexCell == null) { continue; } hexCell.TerrainTypeIndex = terrain; int size = random.Next(CoreSize - wave, CoreSize + wave); //如果有设置大小限制 if (maxSize + minSize > 0) { if (maxSize > 0 && minSize > 0) { Extension.Limit(size, minSize, maxSize); } else if (maxSize > 0) { if (size > maxSize) { size = maxSize; } } else if (minSize > 0) { if (size < minSize) { size = minSize; } } } ProliferationHexCell(hexCell, size, math ); } }
/// <summary> /// 增殖单元格 /// </summary> /// <param name="cell">需要增殖的单元格</param> /// <param name="Size">单元格增值的数量</param> /// <param name="math">增殖单元格选择范围断言</param> /// <returns>增殖过项的列表</returns> private List <HexCell> ProliferationHexCell(HexCell cell, int Size, Predicate <HexCell> math) { //增殖过的列表 List <HexCell> ProliferationList = new List <HexCell>(); //增殖备选列表 List <HexCell> AddCell = new List <HexCell> { cell }; if (Size > MapSize) { Size = MapSize; } //增殖的单元格的地图类型 即核心单元格的地图类型 Terrain thisTerrain = cell.TerrainTypeIndex; int i = 0; while (i < Size && AddCell.Count > 0) { HexCell selectedCell = AddCell.GetRandomItem(random); //相邻可更改的单元格的备选列表 List <HexCell> alternative = new List <HexCell>(); //向备选列表添加所有的可以返回条件的相邻单元格 foreach (HexCell item in selectedCell.neighbors) { if (item != null && item.TerrainTypeIndex != thisTerrain && math(item)) { alternative.Add(item); } } //如果没有可改变的单元格则将这个单元格从增殖单元格备选列表中移除 //然后重新选择一个备选列表中的单元格 if (alternative.Count == 0) { AddCell.Remove(selectedCell); continue; } //随机从邻近备选列表中选一个单元格改变地貌,并加它添加到备选列表中 HexCell neighbor = alternative.GetRandomItem(random); neighbor.TerrainTypeIndex = thisTerrain; ProliferationList.Add(neighbor); AddCell.Add(neighbor); i++; } return(ProliferationList); }