public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) { HexCoordinates coordinates = new HexCoordinates( property.FindPropertyRelative("_x").intValue, property.FindPropertyRelative("_z").intValue ); position = EditorGUI.PrefixLabel(position, label); GUI.Label(position, coordinates.ToString()); }
public void ReachableTest() { var expected = new List <IHexCoordinate> { HexCoordinates.Cube(0, 1, -1), HexCoordinates.Cube(0, 2, -2), HexCoordinates.Cube(1, 2, -3), HexCoordinates.Cube(2, 1, -3) }; Func <IHexCoordinate, bool> isBlock = c => c.Equals(HexCoordinates.Cube(0, 0, 0)) || c.Equals(HexCoordinates.Cube(1, 0, -1)) || c.Equals(HexCoordinates.Cube(2, 0, -2)); CollectionAssert.AreEquivalent(expected, Center.Reachable(1, isBlock)); }
public static void ApplyUpgrade(HexCoordinates coords, Tribe tribe) { HexCell cell = grid.GetCell(coords); if (cell.Structure is Building) { Building building = (Building)cell.Structure; if (!building.IsUpgradable()) { return; } tribe.HQ.Inventory.ApplyRecipe(building.Recipes[building.Level]); ((Building)cell.Structure).Upgrade(); } }
public static bool PlayerInRange(HexCoordinates coords, Player player) { if (coords == player.Position) { return(true); } for (HexDirection dir = HexDirection.NE; dir <= HexDirection.NW; dir++) { if (coords != player.Position.InDirection(dir)) { continue; } return(true); } return(false); }
public void RemoveTerrainObject(HexCoordinates coordinates) { //TODO this could be more efficient CellStack stack = GetCellStackFromWorldCoords(coordinates); if (stack) { Vector2Int index = stack.indexWithinChunk; TerrainObject obj = terrainObjects[index.x, index.y]; if (obj != null) { terrainObjects[index.x, index.y] = null; GameObject.Destroy(obj.gameObject); } } }
private int[] FringeHexTiles(int index) { HexCoordinates coord = hexGrid.GetCellCoord(index); int coordx = coord.X; int coordz = coord.Z; int left = hexGrid.GetCellIndexFromCoord(coordx - 1, coordz); int right = hexGrid.GetCellIndexFromCoord(coordx + 1, coordz); int uleft = hexGrid.GetCellIndexFromCoord(coordx - 1, coordz + 1); int uright = hexGrid.GetCellIndexFromCoord(coordx, coordz + 1); int lleft = hexGrid.GetCellIndexFromCoord(coordx, coordz - 1); int lright = hexGrid.GetCellIndexFromCoord(coordx + 1, coordz - 1); //makes the order go clockwise, which is needed for vision calculations return(new int[] { left, lleft, lright, right, uright, uleft }); }
public void ColorCell(Vector3 position, Color color) { position = transform.InverseTransformPoint(position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2; if (index < 0 || index > width * height) { return; } HexCell cell = cells[index]; cell.color = Color.black; cell.borderColor = color; cell.Recolor(); }
/// <summary> /// /// </summary> /// <param name="_coordinate"></param> /// <returns></returns> public static TileBase CreateHexSpriteTileAt(Vector3Int _coordinate) { GameObject prefab = TurnBasedGameKit.DemoHexTilemap.ResourcesLoader.m_HexSpriteTilePrefab; GameObject hexagon = Object.Instantiate(prefab) as GameObject; hexagon.transform.SetParent(m_Parent, false); Vector3 position = HexCoordinates.FromCoordinates2D(_coordinate); hexagon.transform.localPosition = position; HexSpriteTile tile = hexagon.GetComponent <HexSpriteTile>(); tile.m_Property.m_Coordinate = _coordinate; return(tile); }
/// <summary> /// create a hexagon tile at _position /// </summary> /// <param name="_position"></param> /// <returns></returns> public static TileBase CreateHexTileAt(Vector3Int _coordinate) { GameObject prefab = ResourcesLoader.m_HexTilePrefab; GameObject cubeObject = Object.Instantiate(prefab) as GameObject; cubeObject.transform.SetParent(m_Parent, false); Vector3 position = HexCoordinates.FromCoordinates3D(_coordinate); cubeObject.transform.localPosition = position; TileHex hexagonTile = cubeObject.GetComponent <TileHex>(); hexagonTile.m_Property.m_Coordinate = _coordinate; return(hexagonTile); }
void CreateCell(int x, int z, int i) { Vector3 position; position.x = (x + z * 0.5f - z / width) * (HexMetrics.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); HexCell cell = Instantiate(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); cell.coordText.text = cell.coordinates.X + "\n" + cell.coordinates.Z; cells.Add(cell); }
public void ColorCell(Vector3 position, Color color) { position = transform.InverseTransformPoint(position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); if (AreCoordinatesInRange(coordinates.X, coordinates.Y) == true) { HexCell cell = cells[coordinates.X, coordinates.Y]; cell.color = color; if (Debug.isDebugBuild) { hexMesh.Triangulate(cells); } } }
public TerrainChunk GetChunkFromWorldCoords(HexCoordinates coords) { Vector2 offsetCoords = coords.ToOffsetCoordinates(); Vector2 chunkIndex = new Vector2(offsetCoords.x / chunkSize, offsetCoords.y / chunkSize); TerrainChunk chunk = null; try { chunk = chunks[(int)Math.Floor(chunkIndex.x), (int)Math.Floor(chunkIndex.y)]; } catch (IndexOutOfRangeException e) { Debug.LogWarning("WARNING: Tried to access chunk that is out of bounds"); } return(chunk); }
public void FieldOfViewTest() { Func <IHexCoordinate, bool> isBlock = c => c.Equals(HexCoordinates.Cube(0, 0, 0)) || c.Equals(HexCoordinates.Cube(1, 0, -1)) || c.Equals(HexCoordinates.Cube(2, 0, -2)) || c.Equals(HexCoordinates.Cube(0, 2, -2)); var field = Center.FieldOfView(5, isBlock).ToList(); Assert.False(field.Contains(HexCoordinates.Cube(0, 0, 0))); Assert.False(field.Contains(HexCoordinates.Cube(-1, -2, 3))); Assert.False(field.Contains(HexCoordinates.Cube(2, -1, -1))); Assert.False(field.Contains(HexCoordinates.Cube(3, 0, -3))); Assert.True(field.Contains(HexCoordinates.Cube(1, 2, -3))); Assert.True(field.Contains(HexCoordinates.Cube(4, 0, -4))); }
public void DiagonalTest() { var expected = new List <IHexCoordinate> { HexCoordinates.Cube(3, 0, -3), HexCoordinates.Cube(2, -1, -1), HexCoordinates.Cube(0, 0, 0), HexCoordinates.Cube(-1, 2, -1), HexCoordinates.Cube(0, 3, -3), HexCoordinates.Cube(2, 2, -4) }; var result = EnumUtils.Values <HexDirection>().Select(direction => Center.Diagonal(direction)).ToList(); CollectionAssert.AreEquivalent(expected, result); }
public static Hex GetHex(HexCoordinates hexCoordinates) { int q = hexCoordinates.q; int r = hexCoordinates.r; if (hexes == null) { Debug.LogError("hexes == null"); } else if (q >= 0 && q < numberOfColumns && r >= 0 && r < numberOfRows) { return(hexes[q, r]); } Debug.LogError("hex is null"); return(null); }
// protected override void movedCell(HexCell cell) { // if (cell.GetInfo().human) { // game.fatigue++; // } // BaseSaver.putGame (game); // } // // protected override void postEndCheck(int turn) { // this.turn = turn; // if (turn == 0) { // game.fatigue = 0; // BaseSaver.putGame (game); // } else { // PlayAI (); // } // } public void PaintTile(Vector3 position, Color color) { position = transform.InverseTransformPoint(position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); int index = coordinates.X + coordinates.Z * boardWidth + coordinates.Z / 2; HexCell cell = cells [index]; cell.setType((TileInfo.tileType)idx); Debug.Log("Setting type: " + ((TileInfo.tileType)idx).ToString()); Debug.Log("idx: " + idx.ToString()); hexMesh.Triangulate(cells); ResetCells(); }
public HexCell GetCell(Vector3 position) { position = transform.InverseTransformPoint(position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); Debug.Log("touched at " + coordinates.ToString()); int index = coordinates.X + coordinates.Z * cellCountX + coordinates.Z / 2; HexCell cell = cells[index]; //-- cell.color = touchedColor; ClickSetTile(cell, 0, 0, 0, 1); // hexMesh.Triangulate(cells); //hexTiles.SetTileToLocation(1,cell.transform.localPosition,cell); return(cells[index]); }
void CheckLocation() { Ray down = new Ray(); down.direction = Vector3.down * 100f; down.origin = transform.position; RaycastHit[] hits = Physics.RaycastAll(down); foreach (RaycastHit hit in hits) { HexGrid grid = hit.collider.GetComponentInParent <HexGrid>(); if (grid) { HexLoc = grid.GetCell(transform.position).coordinates; } } }
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]); }
public HexCell GetCell(Vector3 position) { position = transform.InverseTransformPoint(position); //坐标转换 HexCoordinates coordinates = HexCoordinates.FromPosition(position); int index = coordinates.X + coordinates.Z * cellCountWidth + coordinates.Z / 2; if (cells == null) { return(null); } if (index < 0 || index >= cells.Length) { return(null); } return(cells[index]); }
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; }
public HexCell GetCell(HexCoordinates coordinates) { int y = coordinates.Y; int x = coordinates.X + y / 2; if (y < 0 || y >= CellCountY) { return(null); } if (x < 0 || x >= CellCountX) { return(null); } return(Cells[x + y * CellCountX]); }
HexCell EmptyCell(HexCoordinates startCooridantes) { // serch for empty hexCell HexCell cell; foreach (HexCoordinates offset in HexCoordinates.NeighboursOffsets) { HexCoordinates newCoordinates = new HexCoordinates(startCooridantes.X + offset.X, startCooridantes.Z + offset.Z); cell = grid.FromCoordinates(newCoordinates); if (cell != null && cell.IsEmpty()) { return(cell); } } return(null); }
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.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z); 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 <Text>(cellLabelPrefab); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); cell.uiRect = label.rectTransform; cell.Elevation = 0; AddCellToChunk(x, z, cell); }
public void AddBubbleToGameBoard(Bubble bubble) { Vector3 position = GameBoard.transform.InverseTransformPoint(bubble.transform.position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); if (GameBoard.HasCellBubble(coordinates.X, coordinates.Y) == false) { GameBoard.AddBubbleToCoordinates(coordinates.X, coordinates.Y, bubble); var clusterBubbles = GameBoard.GetClusterFromCoordinates(coordinates.X, coordinates.Y); if (clusterBubbles != null && clusterBubbles.Count >= MinBubblesToRemove) { foreach (var bubbleCoordinates in clusterBubbles) { GameBoard.RemoveBubbleFromCoordinates(bubbleCoordinates.X, bubbleCoordinates.Y); } var floatingClusters = GameBoard.GetFloatingClusters(); foreach (var bubbleCoordinates in floatingClusters) { GameBoard.RemoveBubbleFromCoordinates(bubbleCoordinates.X, bubbleCoordinates.Y); } UpdateScore(clusterBubbles.Count + floatingClusters.Count); } GameBoard.SetTurnOver(); if (GameBoard.IsTurnEventCountReached() == true) { GameBoard.AddNewBubblesRow(); } } IsGameOver = CheckIsGameOver(); if (IsGameOver) { StartCoroutine(GotoGameOver()); } bubbleIndex = GameBoard.GetRandomBubbleIndex(); Color bubbleColor = GameBoard.GetBubbleColor(bubbleIndex); Paddle.SetLineRendererColor(bubbleColor); bubbleToLaunch = null; }
int SinkTerrain(int chunkSize, int budget, MapRegion region) { searchFrontierPhase += 1; HexCell firstCell = GetRandomCell(region); firstCell.SearchPhase = searchFrontierPhase; firstCell.Distance = 0; firstCell.SearchHeuristic = 0; searchFrontier.Enqueue(firstCell); HexCoordinates center = firstCell.coordinates; int sink = Random.value < highRiseProbability ? 2 : 1; int size = 0; while (size < chunkSize && searchFrontier.Count > 0) { HexCell current = searchFrontier.Dequeue(); int originalElevation = current.Elevation; int newElevation = originalElevation - sink; if (newElevation < elevationMinimum) { continue; } current.Elevation = newElevation; if (originalElevation >= waterLevel && newElevation < waterLevel) { budget += 1; } size += 1; for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if (neighbor && neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = neighbor.coordinates.DistanceTo(center); neighbor.SearchHeuristic = Random.value < jitterProbability ? 1 : 0; searchFrontier.Enqueue(neighbor); } } } searchFrontier.Clear(); return(budget); }
public void ObjectSpawner(GameObject objectToBeSpawned, Vector3 location) { spawnTargetCoords = hexGrid.ReturnHexCoords(location); UnitTypes spawnUt; if (OverallHexCoordsDict.GameDictionary.TryGetValue(spawnTargetCoords, out spawnUt)) { switch (spawnUt) { default: StartCoroutine(gameUI.SetMessage("Move out unit from spawn Zone")); break; } } else { if (bc.NumberOfActionsRemaining > 0) { switch (objectToBeSpawned.GetComponent <ObjectInfo>().ut) { case (UnitTypes.Miner): NumberOfResources -= baseActionController.CostOfMiner; pt.AlterPercentApproval(Random.Range(-2, 2), PoliticsParty.Warhawk); pt.AlterPercentApproval(Random.Range(2, 5), PoliticsParty.Peacenik); pt.AlterPercentApproval(Random.Range(0, 1), PoliticsParty.Balance); break; case (UnitTypes.Settler): pt.AlterPercentApproval(Random.Range(-2, 2), PoliticsParty.Warhawk); pt.AlterPercentApproval(Random.Range(2, 5), PoliticsParty.Peacenik); pt.AlterPercentApproval(Random.Range(0, 2), PoliticsParty.Balance); NumberOfResources -= baseActionController.CostOfSettler; break; case (UnitTypes.Soldier): pt.AlterPercentApproval(Random.Range(2, 5), PoliticsParty.Warhawk); pt.AlterPercentApproval(Random.Range(-4, -2), PoliticsParty.Peacenik); pt.AlterPercentApproval(Random.Range(-2, 2), PoliticsParty.Balance); NumberOfResources -= baseActionController.CostOfSoldier; break; } Instantiate(objectToBeSpawned, location, Quaternion.identity); StartCoroutine("RefreshUnitArray"); bc.NumberOfActionsRemaining--; } } }
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 (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]); } } } Text label = Instantiate(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; }
public HexCell[] GetNeighbours() { HexCell[] neighbours = new HexCell[6]; HexCoordinates[] offsets = new HexCoordinates[6] { new HexCoordinates(-1, 0), new HexCoordinates(1, 0), null, null, null, null }; if (position.y % 2 == 0) { offsets[2] = new HexCoordinates(0, 1); offsets[3] = new HexCoordinates(-1, 1); offsets[4] = new HexCoordinates(-1, -1); offsets[5] = new HexCoordinates(0, -1); } else { offsets[2] = new HexCoordinates(1, 1); offsets[3] = new HexCoordinates(0, 1); offsets[4] = new HexCoordinates(0, -1); offsets[5] = new HexCoordinates(1, -1); } for (int i = 0; i < offsets.Length; i++) { HexCoordinates realPos = position + offsets[i]; GameObject map = GameObject.Find("Map"); Map mapScript = map.GetComponent <Map>(); HexCell cell = mapScript.GetCellByPosition(realPos); if (cell != null) { neighbours[i] = cell; } } List <HexCell> returnValue = new List <HexCell>(); foreach (var item in neighbours) { if (item != null) { returnValue.Add(item); } } return(returnValue.ToArray()); }
void CreateCell(int x, int y, int i, bool newMap, bool defaultTraversable, bool fowHidden) { Vector3 position; position.x = (x + y * 0.5f - y / 2) * (HexMetrics.innerRadius * 2f); position.z = 0f; position.y = y * (HexMetrics.outerRadius * 1.5f); HexCell cell = Cells[i] = Instantiate <HexCell>(cellPrefab); cell.transform.SetParent(this.transform); cell.transform.localPosition = position; cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, y); cell.FOWMode = fowHidden ? FOW.FOWMode.Hidden : FOW.FOWMode.InView; if (newMap) { //Connect hex neighbors to the west if (x > 0) { cell.SetNeighbor(HexDirection.W, Cells[i - 1]); } if (y > 0) { if ((y & 1) == 0) //If even row (with X = 0 to the leftmost position) { cell.SetNeighbor(HexDirection.SE, Cells[i - CellCountX]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, Cells[i - CellCountX - 1]); } } else //Un-even row (with X = 0 with incline into the row) { cell.SetNeighbor(HexDirection.SW, Cells[i - CellCountX]); if (x < CellCountX - 1) { cell.SetNeighbor(HexDirection.SE, Cells[i - CellCountX + 1]); } } } cell.Traversable = defaultTraversable; } cell.myGrid = this; }
private void CreateCell(int x, int z, int i) { // Z is indexed starting from below. var xPosition = (x + z * .5f - z / 2) * HexMetrics.innerRadius * 2f; var zPosition = z * HexMetrics.outerRadius * 1.5f; var position = new Vector3(xPosition, 0, zPosition); var cell = Instantiate(hexCell, position, Quaternion.identity); var collider = cell.gameObject.AddComponent(typeof(BoxCollider)) as BoxCollider; // Box collider is not ideal, but placeholder until we can get an actual hexagon prefab? collider.size = new Vector3(20.0f, 5.0f, 20.0f); collider.isTrigger = true; cell.transform.parent = parentCell.transform; hexCells[i] = cell; cell.coordinates = HexCoordinates.FromOffsetCoords(x, z); if (x > 0) { cell.SetNeighbor(HexDirection.W, hexCells[i - 1]); } if (z > 0) { if (z % 2 == 0) //even { cell.SetNeighbor(HexDirection.SE, hexCells[i - xWidth]); if (x > 0) { cell.SetNeighbor(HexDirection.SW, hexCells[i - xWidth - 1]); } } else { cell.SetNeighbor(HexDirection.SW, hexCells[i - xWidth]); if (x < xWidth - 1) { cell.SetNeighbor(HexDirection.SE, hexCells[i - xWidth + 1]); } } } var label = Instantiate(hexCellLabel); label.rectTransform.SetParent(gridUICanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(xPosition, zPosition); label.text = x + "," + z; //hex grid coordinates, not global }