private void CreateCell(int x, int z, int i) { Vector3 position; // ReSharper disable once PossibleLossOfFraction 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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.grid = this; cell.color = defaultColor; AddNeighbors(x, z, i, cell); var label = Instantiate(cellLabelPrefab); var rectTransform = label.rectTransform; rectTransform.anchoredPosition = new Vector2(position.x, position.z); rectTransform.sizeDelta = new Vector2(HexMetrics.outerRadius, HexMetrics.outerRadius); rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = rectTransform; AddCellToChunk(x, z, cell); }
void CreateCell(int column, int row, bool load) { Vector3 position; position.x = (column + row * 0.5f - row / 2) * (HexCell.Geometry.innerRadius * 2f); position.y = 0f; position.z = row * (HexCell.Geometry.outerRadius * 1.5f); HexCell cell = cells[GetCellIndex(column, row)] = Instantiate <HexCell>(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(column, row); cell.color = HexCell.defaultColor; cell.row = row; cell.column = column; if (load) { MapSerializer.CellProperties cellProperties = mapSerializer.GetValue(column, row); cell.SetType(cellProperties.m_Type); } Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); }
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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.index = cell.coordinates.X + cell.coordinates.Z * width + cell.coordinates.Z / 2; cell.transform.name = HexCoordinates.FromPosition(cell.transform.position).ToString(); cell.cost = Random.Range(0, 6); // random range 0 - 5 (int) // cell.color = Color.magenta /cell.cost; cell.color = Color.grey * 0.1f; Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); //label.rectTransform.rect.Set(position.x, position.y, 10,15); label.text = cell.coordinates.ToStringOnSeparateLines(); }
public void GenerateMap() { //used for pathfinding prototype //if (TileList == null) { TileList = new TileListBase(width, height, depth); } TileList2DControl ReturnedTileArray = new TileList2DControl(TileList); System.Random rng = new System.Random((int)DateTime.Now.Ticks); double MapCenterX = width / 2; //same off by one error double MapCenterY = height / 2; double TileCount = width * height; double TileCountSqr = Math.Sqrt(TileCount); float radius = width / 2; if (width > height) { radius = height / 2; //radius ought not to extend off the map so the small of the two is used here } for (int i = 0; i < TileCount; i++) { int xindex = i % width; int yindex = i / width; if (Mathf.Sqrt(Mathf.Pow(((float)xindex - (float)MapCenterX), 2) + Mathf.Pow(((float)yindex - (float)MapCenterY), 2)) <= radius) { ReturnedTileArray.AssignFloorType(HexCoordinates.FromOffsetCoordinates(xindex, yindex, 0), FloorTile); } else { ReturnedTileArray.AssignFloorType(HexCoordinates.FromOffsetCoordinates(xindex, yindex, 0), null); } } //map will be ~ 78.5% grass tiles, and a circle }
public void Init(List <CharacterClasses> pclasses) { // Init all teams by using data feed "GameInitData" (Static class) foreach (TeamDescriptor desc in GameInitData.teamsDescriptions) { // Create and instantiate team GameObject currentTeamGO = Instantiate(teamPrefab); Team currentTeam = currentTeamGO.GetComponent <Team>(); currentTeam.Init(grid, desc.species, desc.type); // createUnits for (int i = 0; i < unitsQuantityByTeam; ++i) { // Instantiate unit int startX, startY; HexCell cell; do { startX = Random.Range(1, 18); startY = Random.Range(1, 13); cell = grid.GetCell(HexCoordinates.FromOffsetCoordinates(startX, startY)); } while (cell.IsUnderwater || cell.Unit != null); currentTeam.CreateUnit(0, pclasses[Random.Range(0, pclasses.Count)], cell); } // Add newly created team to manager list AddTeam(currentTeam); } activeTeam = teams[activeTeamTurnIndex]; }
/// <summary> /// Adds cells to array based on metrics, max width & max height /// </summary> /// <param name="x">X coord of cell in array</param> /// <param name="z">Z coord of cell in array</param> /// <param name="type"></param> /// <param name="isHill"></param> void CreateCell(int x, int z, HexType type, bool isHill) { Vector3 pos; pos.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRad * 2f); pos.y = 0f; pos.z = z * HexMetrics.outerRad * 1.5f; HexCell cell = cells[x, z] = Instantiate <HexCell>(cellPrefab); cell.transform.localPosition = pos; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.name = "Hex Cell " + cell.coordinates.ToString(); if (type != null) { cell.Type = type; } else { cell.Type = HexType.types[HexType.typeKeys.ocean]; } cell.isHill = isHill; /*if (showCoords) * { * Text label = Instantiate<Text>(labelPrefab); * //label.rectTransform.SetParent(gridCanvas.transform, false); * label.rectTransform.anchoredPosition = new Vector2(pos.x, pos.z); * label.text = cell.coordinates.ToStringOnSeparateLines(); * }*/ 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(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.color = Color.white; Text label = Instantiate(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSpearateLines(); }
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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.color = defaultColor; // Colin Menz - 4 / 2 / 17 if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); }
/// <summary> /// Instantiates a HexCell in cells[i] at x*10, z*10. Parents the HexCell GameObject to this HexGrid /// </summary> /// <param name="x">x-grid position</param> /// <param name="z">z-grid position</param> /// <param name="i">index into cells[]</param> void CreateCell(int x, int z, int i) { Vector3 position = GridToPosition(x, z); //position.x = x * 10f; // grid positioning //position.z = z * 10f; /* * position.x = (x + z / 2f - 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); string rivetString = cell.coordinates.ToRivetsString(); bool inRangeOfBCPC = rivetString == "1103" || rivetString == "1104"; HexCoordinates bcpc = HexCoordinates.FromRivets(11, 5); int distance = bcpc.Distance(cell.coordinates); // Debug.Log($"{bcpc} ({bcpc.ToRivetsString()}) to {cell.coordinates} ({cell.coordinates.ToRivetsString()}): {distance}"); inRangeOfBCPC = distance <= 5; // todo cell.color = inRangeOfBCPC ? highlightColor : defaultColor; // new Color(1f, 0.5f, 0.5f);// Color.green; Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x + label_offset_x, position.z); //label.text = cell.coordinates.ToStringOnSeparateLines(); // $"{x}\n{z}"; label.text = cell.coordinates.ToRivetsString(); // + "\n" + cell.coordinates.ToString(); }
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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.color = defaultColor; if (showCoordinates) { Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x + 4.5f, position.z - 4.5f); //label.text = cell.coordinates.ToStringOnSeparateLines(); //label.text = cell.transform.localPosition.x.ToString() + "\n" + cell.transform.localPosition.z.ToString(); //label.text = i.ToString(); } }
//-----------CREATE----------------------------- 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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.color = defaultColor; cell.terrain = "Empty"; cell.buildingObj = null; cell.entityObj = null; cell.fog = true; Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); }
void CreateCell(int x, int y) { Vector3 position = HexCoordinates.PositionFromOffsetCoordinates(x, y); HexCell cell = cells[x, y] = Instantiate(cellPrefab); cell.name = "Cell[" + x.ToString() + ", " + y.ToString() + "]"; cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, y); cell.color = defaultColor; if (y >= height - numberLinesToFill) { CreateBubble(cell); } else { cell.bubble = null; } if (Debug.isDebugBuild) { Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.y); label.text = cell.coordinates.ToStringOnSeparateLines(); } }
/// <summary> /// Creates cells at the given positions. /// </summary> /// <param name="x">The x position.</param> /// <param name="z">The z position.</param> /// <param name="i">Index of the cell out of all.</param> void CreateCell(int x, int z, int i) { Vector3 position; // space them out according to radius and offset position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f); //position.x = x * 10f; // <-- non shifted position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); //position.z = z * 10f; // assign cell to the current index and assign its values 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; // instantiate the labels and show cell coordinates Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); }
private void CreateCell(int x, int z, int i) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetrices.InnerRadius * 2f); position.y = z * (HexMetrices.OuterRadius * 1.5f); position.z = 0f; ICell cell = _hexCellPresenterFactory.Create(); cell.Initialize(_hexCellViewFactory.Create()); cell.UiPosition = position; cell.Coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.UpdateLabel(cell.Distance.ToString()); cell.IsWalkable = Random.value > 0.2f ? true : false; if (!cell.IsWalkable) { cell.CellColor = _settings.BlockedCellColor; } else { cell.CellColor = _settings.DefaultCellColor; } bool makeRoad = Random.value > 0.7f && cell.IsWalkable ? true : false; if (makeRoad) { cell.CellColor = _settings.RoadCellColor; var directions = System.Enum.GetValues(typeof(HexDirection)) as HexDirection[]; foreach (var item in directions) { cell.AddRoad(item); } } if (x > 0) { cell.SetNeighbor(HexDirection.W, _map.Cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, _map.Cells[i - _settings.CellCountX]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, _map.Cells[i - _settings.CellCountX - 1]); } } else { cell.SetNeighbor(HexDirection.SW, _map.Cells[i - _settings.CellCountX]); if (x < _settings.CellCountX - 1) { cell.SetNeighbor(HexDirection.SE, _map.Cells[i - _settings.CellCountX + 1]); } } } _map.Add(cell); }
public void setSkillFilter(HexCoordinates coordinate, int size, int action) { if (hexFilter != null) { clearHexFilter(); } hexFilter = Instantiate <HexFilter>(hexFilterPrefab); hexFilter.transform.SetParent(transform, false); for (int x = -size; x <= size; x++) { int iX = x + coordinate.X; if ((iX < 0 || iX >= width)) { continue; } for (int y = Mathf.Max(-size, -x - size); y <= Mathf.Min(size, -x + size); y++) { int iY = y + coordinate.Y; if (((iY + iX / 2) < 0 || (iY + iX / 2) >= height)) { continue; } int index = iX + (iY + iX / 2) * width; if (cells[index] != null && (cells[index].mapType != 1 && cells[index].mapType != 2)) { hexFilter.setFilter(HexCoordinates.FromOffsetCoordinates(iX, iY + iX / 2), action); } } } }
private void DrawForbiddenLine( HashSet <MapSection> unassignedSections, HashSet <MapSection> forbiddenSections, GridPartition partition, IMapTemplate mapTemplate ) { int lineXCoord = Mathf.RoundToInt(Grid.CellCountX * UnityEngine.Random.Range( mapTemplate.ContinentSeparationLineXMin, mapTemplate.ContinentSeparationLineXMax )); IHexCell startCell = Grid.GetCellAtCoordinates(HexCoordinates.FromOffsetCoordinates(lineXCoord, 0)); IHexCell endCell = Grid.GetCellAtCoordinates(HexCoordinates.FromOffsetCoordinates(lineXCoord, Grid.CellCountZ - 1)); var cellLine = Grid.GetCellsInLine(startCell, endCell); var sectionsOnLine = cellLine.Select(cell => partition.GetSectionOfCell(cell)).Distinct(); var sectionsToForbid = sectionsOnLine.SelectMany(section => partition.GetNeighbors(section)) .Concat(sectionsOnLine).Distinct(); foreach (var section in sectionsToForbid) { unassignedSections.Remove(section); forbiddenSections.Add(section); } }
// public void Refresh () { // hexMesh.Triangulate(cells); // } /// <summary> /// 创建六边形细胞 /// </summary> /// <param name="x"></param> /// <param name="z"></param> /// <param name="i"></param> 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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.Color = defaultColor; cell.name = x.ToString() + "," + z.ToString(); if (x > 0) { //水平顺序,位置大于1的设置西边邻居为前一位六边形 cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { //垂直顺序,位置大于1的位置 if ((z & 1) == 0) { //垂直顺序,偶数位置的,设置东南邻居为前一行长度的六边形 cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]); if (x > 0) { //同时,水平位置大于1的设置西南邻居为前一行长度再加一个单位长度的六边形 cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX - 1]); } } else { //垂直顺序,奇数位置的,设置西南邻居为前一行长度的六边形 cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX]); if (x < cellCountX - 1) { //同时,水平位置大于1的设置东南邻居为前一行长度再减一个单位长度的六边形 cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX + 1]); } } } Text label = Instantiate <Text>(cellLabelPrefab); // label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); cell.uiRect = label.rectTransform; //立即设置高度,触发高度的扰动 cell.Elevation = 0; AddCellToChunk(x, z, cell); }
void CreateProcCell(int x, int z, int i) { // create position of cell Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetric.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetric.outerRadius * 1.5f); //spawn cell HexCell cell = grid[i] = Instantiate <HexCell>(tile); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); // Neighbors Setup if (x > 0) { // east and west cell.SetNeighbor(HexDirection.W, grid[i - 1]); } if (z > 0) { if ((z & 1) == 0) // even rows { //south east and north west cell.SetNeighbor(HexDirection.SE, grid[i - cellCountX]); if (x > 0) { //south west and north east cell.SetNeighbor(HexDirection.SW, grid[i - cellCountX - 1]); } } else // odd rows { //south east and north west cell.SetNeighbor(HexDirection.SW, grid[i - cellCountX]); if (x < cellCountX - 1) { //south west and north east cell.SetNeighbor(HexDirection.SE, grid[i - cellCountX + 1]); } } } //spawn and populate label Text label = Instantiate <Text>(cellLabel); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; float val = (heightNoise.GetValue(x, z, 0)); int ele = ElevetionClamp(val); cell.Elevation = (int)ele; //Debug.Log(ele + " " + moisture); //cell.SetTerrain(terrains.SelectTerrain()); AddCellToChunk(x, z, cell); }
//创建 void CreateCell(int x, int z, int i) { HexCellItem hexCellItem = new HexCellItem(x, z); hexCellItem.borderState = BorderState.Normal; hexCellItem.coordinates = HexCoordinates.FromOffsetCoordinates(x, z, modelInfo.arrayMode); cellMap.Add(hexCellItem.coordinates, hexCellItem); }
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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.owner = emptyPlayer; cell.units = 3; if (x > 0) { cell.SetNeighbor(HexDirection.W, cells [i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells [i - width]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells [i - width - 1]); } } else { cell.SetNeighbor(HexDirection.SW, cells [i - width]); if (x < width - 1) { cell.SetNeighbor(HexDirection.SE, cells [i - width + 1]); } } } RectTransform rect = Instantiate <RectTransform>(cellLabelPrefab); rect.SetParent(gridCanvas.transform, false); rect.anchoredPosition = new Vector2(position.x, position.z); CellLabel cellLabel = rect.GetComponent <CellLabel> (); cell.cellLabelRect = cellLabel.unitLabelRect; cell.cellLabelRect = cellLabel.unitLabelRect; cell.upgradeLabel = cellLabel.upgradeLabel; cell.unitText = cellLabel.unitText; cell.goldPerSecondText = cellLabel.goldPerSecondText; cell.progressBar = cellLabel.progressBar; cell.upgradeLabel.GetComponent <UpgradeLabel> ().cell = cell; }
private void CreateCell(int x, int z, int i) { // By subtrackting the interger division of z we push every other row back // This keeps it a rectangular grid 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 <HexCell>(cellPrefab); var cellTransform = cell.transform; cellTransform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); // Set cell neigbors if (x > 0) { // E to W cells cell.SetNeighbor(HexDirection.W, _cells[i - 1]); } // First row has no connections if (z > 0) { // All even rows have NW to SE connections if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, _cells[i - cellCountX]); // Connect to SW neigbor if not first in row if (x > 0) { cell.SetNeighbor(HexDirection.SW, _cells[i - cellCountX - 1]); } } // All uneven rows have NW to SW connections else { cell.SetNeighbor(HexDirection.SW, _cells[i - cellCountX]); // Connect to SE neigbor if not last in row if (x < cellCountX - 1) { cell.SetNeighbor(HexDirection.SE, _cells[i - cellCountX + 1]); } } } var label = Instantiate <Text>(cellLabelPrefab); var rextLabelTransform = label.rectTransform; rextLabelTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); 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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.Index = i; cell.ShaderData = cellShaderData; //不在边缘的单元是可探测的,而所有其他单元都是不可探测的。 cell.Explorable = x > 0 && z > 0 && x < cellCountX - 1 && z < cellCountZ - 1; //邻居关系建立 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]); } } } //显示坐标 Text label = Instantiate(cellLabelPrefab); //label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); ///停用显示坐标。同时停用HexMapEditor.ShowUI() ///label.text = cell.coordinates.ToStringOnSeparateLines(); cell.uiRect = label.rectTransform; cell.Elevation = 0;//最后cell的高度重置,确保每个cell建立时都不一样 AddCellToChunk(x, z, cell); }
/// <summary> /// Create hexagonal cell at certain positions. /// </summary> /// <param name="x">Number of hexgon on x-axis.</param> /// <param name="z">Number of hexgon on z-axis.</param> /// <param name="i">Index of the cell being created.</param> void CreateCell(int x, int z, int i) { // Position of the hex center realtive to the grid Vector3 position; // z/2 so that each consecutive row shifts to the right, -z/2 is integer division, // so that each even row is shifted back to its original place position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); // Instantiate the cell an set its position HexCell cell = _cells[i] = Instantiate <HexCell>(cellPrefab); // Make this cell a child of the grid cell.transform.SetParent(transform, false); // Positions transform relative to the parent cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.cellColor = defaultCellColor; if (x > 0) { cell.SetNeighbor(HexDirection.W, _cells[i - 1]); } if (z > 0) { // Bitwise AND. This is one of the ways to check if number is even. Here binary AND is used. // If result yielded is zero, then number is even. if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, _cells[i - width]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, _cells[i - width - 1]); } } else { cell.SetNeighbor(HexDirection.SW, _cells[i - width]); if (x < width - 1) { cell.SetNeighbor(HexDirection.SE, _cells[i - width + 1]); } } } // Write the coordinates of the cell as GUI Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(_gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); cell.uiRect = label.rectTransform; }
private void CreateCell(int x, int z, int i) { Vector3 position; // Distance to the next adjacent hexagon on the X axis is twice the inner radius. // Distance to the next adjacent hexagon on the Z axis be 1.5 times the outer radius. // Hexagons are not immediately on top but rather, they are offset along the X axis by the inner radius. // Also, we need to fill a rectangular grid rather than a rombus, which is why we undo part of the offset, // depending on the row. Note that the division is the integer division. 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); // East West connections if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { // Even rows, set the south east neighbor if ((z & 1) == 0) /* Binary AND mask, ignoring everything but the first bit. */ { 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]); } } } Text label = Instantiate <Text>(cellLabelPrefab); //label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); cell.uiRect = label.rectTransform; cell.Elevation = 0; // This will perturb the cell elevation. AddCellToChunk(x, z, cell); }
/// <summary> /// 创建单元格 /// </summary> /// <param name="x">x轴位置</param> /// <param name="z">z轴位置</param> /// <param name="idx">是第几个</param> private void CreateCell(int x, int z, int idx) { Vector3 position = new Vector3(); ///每行的距离是外径的1.5倍 position.z = z * HexMertics.outerRadius * 1.5f; ///每列的间隔是内径的两倍,同时,偶数行需要向正方向偏移内径长度 position.x = (x + z % 2 * 0.5f) * HexMertics.innerRadius * 2f; ///在对应的位置生成单元格 HexCell cell = cells[idx] = Instantiate(cellPrefab, position, Quaternion.identity, _cellParent); ///为单元格赋值游戏内坐标 cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); if (x > 0) { ///每一行的单元格,除了第一个,都有左相邻 cell.SetNeighbor(HexDirection.W, cells[idx - 1]); } if (z > 0) { ///偶数的最后一位肯定是0 if ((z & 1) == 0) { ///和下方行同索引位置成东南相邻 cell.SetNeighbor(HexDirection.SE, cells[idx - width]); ///除了每行第一个,都存在西北相邻的格子 if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[idx - width - 1]); } } else { ///奇数行全部存在西南相邻 cell.SetNeighbor(HexDirection.SW, cells[idx - width]); ///除了最后一个之外,都存在东南方向的相邻 if (x < width - 1) { cell.SetNeighbor(HexDirection.SE, cells[idx - width + 1]); } } } Text label = Instantiate(cellLabelPrefab, _labelParent); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringOnSeparateLines(); }
HexCoordinates RandomCoordinate() { // randomGrid.GetComponent<HexGridChunk>(); int x = Random.Range(-29, 79); int z = Random.Range(0, 59); randomCoordinate = HexCoordinates.FromOffsetCoordinates(x, z); Debug.Log(randomCoordinate); return(randomCoordinate); }
protected virtual 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.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.setColor(defaultColor); // // Image ground = GetComponent<Image> (); // ground.transform.localPosition = position; // ground.transform.SetParent(cell.transform, false); if (i == 0) { Debug.Log("First Coordinate HexGrid: " + cell.coordinates.ToString()); } if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells[i - boardWidth]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[i - boardWidth - 1]); } } else { cell.SetNeighbor(HexDirection.SW, cells[i - boardWidth]); if (x < boardWidth - 1) { cell.SetNeighbor(HexDirection.SE, cells[i - boardWidth + 1]); } } } // Text label = Instantiate<Text>(cellLabelPrefab); // label.rectTransform.SetParent(gridCanvas.transform, false); // label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); // label.text = cell.coordinates.ToStringOnSeparateLines(); // cell.init(playerColors); }
void CreateCell(int x, int z, int i) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetrics.instance.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.instance.outerRadius * 1.5f); HexCell cell = cells[i] = Instantiate(cellPrefab) as HexCell; cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.color = HexMetrics.instance.defaultColor; //设置邻居 if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountWidth]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountWidth - 1]); } } else { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountWidth]); if (x < cellCountWidth - 1) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountWidth + 1]); } } } if (!HexMetrics.instance.isEditor) { Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition3D = new Vector3(position.x, position.z, cell.transform.position.y); cell.label = label; cell.uiRect = label.rectTransform; } cell.isStepDirection = new bool[] { false, false, false, false, false, false }; CellToChunk(x, z, cell); cell.Elevation = 0; }
//Build each given cell at these coordinates void CreateCell(int x, int z, int i) { #region Setting the position Vector3 position; position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f * xOffset); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f * zOffset); #endregion #region Building the Cell's transform and name HexCell cell = cells[i] = Instantiate <HexCell>(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.name = "HexCell_" + (x - z / 2) + "_" + z; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.tag = "HexCell"; #endregion #region Adding the Cell's UI and Highlight components Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = ""; label.name = cell.name + "_Label"; cell.uiRect = label.rectTransform; #endregion #region Setting the cell's Neighbors if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) { cell.SetNeighbor(HexDirection.SE, cells[i - width]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[i - width - 1]); } } else { cell.SetNeighbor(HexDirection.SW, cells[i - width]); if (x < width - 1) { cell.SetNeighbor(HexDirection.SE, cells[i - width + 1]); } } } #endregion }
void CreateCell(int x, int z, int i) { //Create Cell 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.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.Index = i; cell.ShaderData = cellShaderData; cell.Explorable = x > 0 && z > 0 && x < cellCountX - 1 && z < cellCountZ - 1; //Initialize neighbor connections if (x > 0) { cell.SetNeighbor(HexDirection.W, cells[i - 1]); } if (z > 0) { if ((z & 1) == 0) //even rows { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX - 1]); } } else //odd rows { cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX]); if (x < cellCountX - 1) { cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX + 1]); } } } //Display Text on Cell Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; //Set initial elevation cell.Elevation = 0; //Add cell to appropriate chunk of map AddCellToChunk(x, z, cell); }