/// <summary> /// Obtain hex cell according to hex coordinates /// </summary> /// <param name="coordinates">Hex cell coordinates</param> /// <returns>the hex cell at specified coordinates</returns> public HexCell GetCell(HexCoordinates coordinates) { int z = coordinates.Z; if (z < 0 || z >= cellCountZ) { return(null); } int x = coordinates.X + z / 2; if (x < 0 || x >= cellCountX) { return(null); } return(cells[x + z * cellCountX]); }
/// <summary> /// Add a hex cell with specified offset coordinates (x, z), and index i /// </summary> /// <param name="x">x component of offset coordinates</param> /// <param name="z">z component of offset coordinates</param> /// <param name="i">index i of cell array</param> private void CreateCell(int x, int z, int i) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); var cell = cells[i] = Instantiate(cellPrefab); cell.transform.localPosition = position; cell.Index = i; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.ShaderData = cellShaderData; if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX - 1]); } } else { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX]); if (x < cellCountX - 1) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX + 1]); } } } var label = Instantiate(cellLabelPrefab); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; cell.Elevation = 0; AddCellToChunk(x, z, cell); }
void CreateCell(int x, int z, int i) { 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.HexCoords = HexCoordinates.FromOffsetCoordinates(x, z); cell.Text.text = cell.HexCoords.ToStringOnSeparateLines(); cell.Color = DefaultColor; SetUpNeighbors(cell, x, z, i); }
private List <HexCell> GetVisibleCells(HexCell fromCell, int range) { List <HexCell> visibleCells = ListPool <HexCell> .Get(); searchFrontierPhase += 2; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } range += fromCell.ViewElevation; fromCell.SearchPhase = searchFrontierPhase; fromCell.Distance = 0; searchFrontier.Enqueue(fromCell); HexCoordinates fromCoordinates = fromCell.coordinates; while (searchFrontier.Count > 0) { HexCell current = searchFrontier.Dequeue(); current.SearchPhase += 1; visibleCells.Add(current); for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if ( neighbor == null || neighbor.SearchPhase > searchFrontierPhase || !neighbor.Explorable ) { continue; } int distance = current.Distance + 1; if (distance + neighbor.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbor.coordinates) ) { continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } } return(visibleCells); }
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; } // link neighbor 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) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]); 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]); 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(cellLabelPrefab); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; cell.Elevation = 0; AddCellToChunk(x, z, cell); }