void CreateCell(int x, int z, int i, int colCount) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); HexCell cell = cells [i] = Instantiate <HexCell> (cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffSetCoordinates(x, z); cell.color = defaultColor; cell.resources = 1; }
/// <summary> /// 创建单元格 /// </summary> /// <param name="x">在地图宽度的位置</param> /// <param name="z">在地图高度的位置</param> /// <param name="i">在单元格列表中的位置</param> private void CreateCell(int x, int z, int i) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * HexMetrics.innerDiameter; position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); HexCell cell = cells[i] = Instantiate <HexCell>(cellPrefab); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffSetCoordinates(x, z); cell.Index = i; cell.ColumnIndex = x / HexMetrics.chunkSizeX; cell.ShaderData = cellShaderData; if (wrapping) { cell.Explorable = z > 0 && z < cellCountZ - 1; } else { cell.Explorable = x > 0 && z > 0 && x < cellCountX - 1 && z < cellCountZ - 1; } //初始化邻居关系 //初始化东西方向的关系 if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); if (wrapping && x == cellCountX - 1) { cell.SetNeighbor(HexDirection.E, cells[i - x]); } } //初始化其余方向的关系 if (z > 0) { //除第0行外索引为偶数的行 if ((z & 1) == 0) { //东南及相应单元格的反方向 cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]); //行内除第1个外其他单元格 if (x > 0) { //东北及相应单元格的反方向 cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX - 1]); } else if (wrapping) { cell.SetNeighbor(HexDirection.SW, cells[i - 1]); } } //索引为奇数的行 else { //东北及相应单元格的反方向 cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX]); //行内除最后1个外其他单元格 if (x < cellCountX - 1) { //东南及相应单元格的反方向 cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX + 1]); } else if (wrapping) { cell.SetNeighbor( HexDirection.SE, cells[i - cellCountX * 2 + 1] ); } } } Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; cell.Elevation = 0; AddCellToChunk(x, z, cell); }