//2-4个占4个格子的装饰 void addIsland4(GridBase grid, int index, Hashtable map) { List <int> cells = grid.getCells(index, editorCfg.pageSize); int count = NumEx.NextInt(2, 5); for (int i = 0; i <= count; i++) { int id = NumEx.NextBool() ? 2 : 3; occupyCells(grid, cells, id, 1, map); } }
public void resetPosition() { if (aStar != null) { int index = NumEx.NextInt(0, aStar.grid.NumberOfCells); transform.position = aStar.grid.GetCellCenter(index); } else { transform.position = new Vector3(NumEx.NextInt(-radius * 10, radius * 10) / 10.0f, 0, NumEx.NextInt(-radius * 10, radius * 10) / 10.0f); } Invoke("resetPosition", NumEx.NextInt(20, 80) / 10); }
void occupyCells(GridBase grid, List <int> cells, int id, int size, Hashtable map) { int count = 0; int i = NumEx.NextInt(0, cells.Count); int index = 0; while (true) { if (i >= cells.Count) { i = 0; } index = cells[i]; if (size > 1) { bool canAdd = true; List <int> _cells = grid.getCells(index, size); for (int j = 0; j < _cells.Count; j++) { if (_cells[j] < 0 || map[_cells[j]] != null) { canAdd = false; break; } } if (canAdd) { for (int j = 0; j < _cells.Count; j++) { map[_cells[j]] = 99; //占用 } map[index] = id; break; } } else { if (map[index] == null) { map[index] = id; break; } } i++; count++; if (count >= cells.Count) { break; } } }
void generateData() { if (editorCfg == null) { Debug.Log("Editor config is null"); return; } // 设置grid GridBase grid = new GridBase(); grid.init(Vector3.zero, editorCfg.size, editorCfg.size, 1); // 先把图的格式设置成RGBA32 Texture2D areaTex = editorCfg.oceanGidTexture; if (areaTex == null) { Debug.Log("oceanGid Texture is null"); return; } // 先取得大地图分区的数据 Color32[] colors = areaTex.GetPixels32(); Debug.Log("areaTex.width===" + areaTex.width); Debug.Log("colors.Length===" + colors.Length); // 区域块的index对应的val Hashtable areaMap = new Hashtable(); // 取得大地图与分区地图的比例,以例可以对换 int mapareaScale = editorCfg.size / areaTex.width; //地图分区块的网格 GridBase gridArea = new GridBase(); gridArea.init(Vector3.zero, areaTex.width, areaTex.width, mapareaScale); Color c; for (int i = 0; i < colors.Length - 1; i++) { c = colors[i]; int areaVal = 0; if (editorCfg.colors4MapArea.TryGetValue(c, out areaVal)) { areaMap[i] = areaVal; } else { Debug.LogError("get area val by color is nil!" + c + "==" + c * 255); } } //地图数据 Hashtable mapInfor = new Hashtable(); // 生成据点 int tmpSize = editorCfg.pageSize * 2; for (int i = tmpSize / 2; i < editorCfg.size; i += tmpSize) { for (int j = tmpSize / 2; j < editorCfg.size; j += tmpSize) { int index = grid.GetCellIndex(i, j); List <int> cells = grid.getCells(index, 5); int k = NumEx.NextInt(0, cells.Count); while (true) { if (k >= cells.Count) { k = 0; } if (mapInfor[(int)(cells[k])] == null) { mapInfor[(int)(cells[k])] = 1; break; } k++; } } } // 生成装饰 for (int i = editorCfg.pageSize / 2; i < editorCfg.size; i = i + editorCfg.pageSize) { for (int j = editorCfg.pageSize / 2; j < editorCfg.size; j = j + editorCfg.pageSize) { int index = grid.GetCellIndex(i, j); switch (NumEx.NextInt(0, 4)) { case 0: addIsland1(grid, index, mapInfor); break; case 1: addIsland2(grid, index, mapInfor); break; case 2: addIsland3(grid, index, mapInfor); break; default: addIsland4(grid, index, mapInfor); break; } } } //导出数据(会根据分区导出数据,这里会导出100份数据,方便加载) /* * 每个分区id对应一个文件,每个文件里存一个table,key是一屏的index,value是table2 * table2里的key是网格index,value是地块配置id */ string path = Application.dataPath + "/" + CLPathCfg.self.basePath + "/" + CLPathCfg.upgradeRes + "/priority/cfg/worldmap/maparea.cfg"; Directory.CreateDirectory(Path.GetDirectoryName(path)); MemoryStream ms = new MemoryStream(); B2OutputStream.writeObject(ms, areaMap); File.WriteAllBytes(path, ms.ToArray()); Debug.Log(path); for (int i = 0; i < areaTex.width; i++) { for (int j = 0; j < areaTex.width; j++) { int index = i * areaTex.width + j; Hashtable areaPageMap = new Hashtable(); List <int> pagesIndes = areaIndex2MapPageIndexs(grid, gridArea, index, mapareaScale); for (int p = 0; p < pagesIndes.Count; p++) { int center = pagesIndes[p]; List <int> cells = grid.getCells(center, editorCfg.pageSize); Hashtable map = new Hashtable(); for (int k = 0; k < cells.Count; k++) { if (mapInfor[cells[k]] != null) { map[cells[k]] = mapInfor[cells[k]]; } } areaPageMap[center] = map; } path = Application.dataPath + "/" + CLPathCfg.self.basePath + "/" + CLPathCfg.upgradeRes + "/priority/cfg/worldmap/" + index + ".cfg"; Directory.CreateDirectory(Path.GetDirectoryName(path)); ms = new MemoryStream(); B2OutputStream.writeObject(ms, areaPageMap); File.WriteAllBytes(path, ms.ToArray()); } } Debug.Log("total===" + mapInfor.Count); }
// Use this for initialization void Start() { Invoke("resetPosition", NumEx.NextInt(10, 50) / 10); }