// TODO вынести поиск достижимой юнитом ячейки за пределы этого метода(в search или куда нибудь в другое место) /// <summary> /// Показать путь от клетки до клетки в ходах /// </summary> /// <param name="speed"></param> void ShowPath(int speed, bool showPath) { if (currentPathExists) { HexCell current = currentPathTo; int previousTurn = 0;//чтобы вернуться к клетке, которой можно достигнуть while (current != currentPathFrom) { int turn = (current.Distance - 1) / speed; current.SetLabel(turn.ToString()); if (previousTurn >= 1 && turn == 0) { currentPathReach = current; if (showPath) { current.EnableHighlight(Color.green); //Ту клетку, которой можем достичь подсвечиваем зеленым } } else { if (showPath) { current.EnableHighlight(Color.white); } } previousTurn = turn; current = current.PathFrom; } } if (showPath) { currentPathFrom.EnableHighlight(Color.blue); } if (showPath) { currentPathTo.EnableHighlight(Color.red); } if (currentPathReach == null && currentPathTo.Distance <= speed) { currentPathReach = currentPathTo; } else if (!currentPathReach) { ClearPath(); //сначала очистка пути, иначе не будет работать currentPathExists = false; //временный костыль, пока не сделал нормальное сохраниение пути } }
void DoSelection() { grid.ClearPath(); grid.ClearShowMovement(); UpdateCurrentCell(); if (currentCell) { selectedUnit = currentCell.Unit; if (currentCell.Unit) { currentCell.EnableHighlight(Color.blue); if (resourcePointIndices.Contains(currentCell.SpecialIndex)) { ShowScoutUI(); } else { HideScoutUI(); } } else { HideScoutUI(); } if (currentCell.SpecialIndex == HiveSpecialIndex) { Debug.Log("Hive UI"); HiveUI.SetActive(true); } } }
void HandleInput() { Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(inputRay, out hit)) { EditCell(hexGrid.GetCell(hit.point)); HexCell currentCell = hexGrid.GetCell(hit.point); if (Input.GetKey(KeyCode.LeftShift) && searchToCell != currentCell) { if (searchFromCell) { searchFromCell.DisableHighlight(); } searchFromCell = currentCell; searchFromCell.EnableHighlight(Color.blue); if (searchToCell) { hexGrid.FindPath(searchFromCell, searchToCell); } } if (searchFromCell && searchFromCell != currentCell) { searchToCell = currentCell; hexGrid.FindPath(searchFromCell, searchToCell); } } }
public void Select(HexCell cell) { Unselect(); cell.EnableHighlight(logic.CurrentPlayer); selectedCell = cell; nearestCity = logic.FindNearestCity(selectedCell.coordinates); inMapResourcePanel.HasMoney = logic.IsThereEnoughShields(COST_COLLECT); if (nearestCity == null) { inMapResourcePanel.IsCityNear = false; } else { inMapResourcePanel.IsCityNear = true; inMapResourcePanel.NearestCity = nearestCity.Name; } if (cell.ResourceKind == MapResource.ResourceKind.CORN) { inMapResourcePanel.HasTechnology = logic.HasTechnology(TechnologyType.AGRICULTURE); } else if (cell.ResourceKind == MapResource.ResourceKind.FISH) { inMapResourcePanel.HasTechnology = logic.HasTechnology(TechnologyType.NAVIGATION); } //inMapResourcePanel.gameObject.SetActive(true); infoUserCanvas.OpenInMapResourcePanel(); }
//shows the current path at a given speed, if it exists. //even it if doesn't, set the highlight on the start and end hexes. void ShowPath(int unitMovement, int unitMovementRemaining) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { int turn = (current.MovementCost - 1) / unitMovement; //turn cost if cell has full movement if (unitMovementRemaining == 0) { turn++; } else if (unitMovementRemaining < unitMovement) { int remainder = (current.MovementCost - 1) % unitMovement; if (remainder >= unitMovementRemaining) { turn++; } } current.SetLabel(turn.ToString()); current.EnableHighlight(Color.white); current = current.PathFrom; } } currentPathFrom.EnableHighlight(Color.blue); currentPathTo.EnableHighlight(Color.red); }
IEnumerator Search(HexCell fromCell, HexCell toCell) { for (int i = 0; i < cells.Length; i++) { cells[i].Distance = int.MaxValue; cells[i].DisableHighlight(); } fromCell.EnableHighlight(Color.blue); toCell.EnableHighlight(Color.red); WaitForSeconds delay = new WaitForSeconds(1 / 60f); List <HexCell> frontier = new List <HexCell>(); fromCell.Distance = 0; frontier.Add(fromCell); while (frontier.Count > 0) { yield return(delay); HexCell current = frontier[0]; frontier.RemoveAt(0); if (current == toCell) { current = current.PathFrom; while (current != fromCell) { current.EnableHighlight(Color.white); current = current.PathFrom; } break; } for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); int distance = current.Distance; //Here we should also have to search for obstacles in the if statement //example: neighbor.tag != "obstacle" if (neighbor == null) { continue; } if (neighbor.Distance == int.MaxValue) { neighbor.Distance = distance; neighbor.PathFrom = current; frontier.Add(neighbor); } else if (distance < neighbor.Distance) { neighbor.Distance = distance; neighbor.PathFrom = current; } frontier.Sort((x, y) => x.Distance.CompareTo(y.Distance)); } } }
void HandleTapInput() { //If a cell has been selected, show how far away it is hexGrid.ClearPath(); currentCell = GetCellUnderCursor(); if (currentCell) { if (currentCell != previousCell) { #region Handle input on hexagon in grid //If the player has just moved, reset the move components if (endCell) { endCell.DisableHighlight(); endCell = null; } //If the player has selected that they want to move, set the selected cell to be the move target, highlight it, if (isMoving) { isMoving = false; endCell = currentCell; currentCell = previousCell; endCell.EnableHighlight(selectedColor); startCell = previousCell; if (startCell.unit) { currentUnit = startCell.unit; } else { currentUnit = null; } } else { currentCell.EnableHighlight(highlightColor); if (previousCell) { previousCell.DisableHighlight(); } previousCell = currentCell; } #endregion } } if (endCell != null) { if (endCell != currentCell) { if (currentUnit) { hexGrid.FindPath(currentUnit.Location, endCell, speed - hexGrid.hexesTravelled); } } } }
public void Select(HexCell cell, City city) { Unselect(); cell.EnableHighlight(city.PlayerID); selectedCell = cell; cityInfoPanel.SelectedCity = city; cityInfoPanel.gameObject.SetActive(true); }
public void SwitchSelectedCell(HexCell cell, int playerID) { if (selectedCell != null) { selectedCell.DisableHighlight(); selectedCell = cell; cell.EnableHighlight(playerID); } }
private void DoSelection() { grid.ClearPath(); UpdateCurrentCell(); if (currentCell) { selectedUnit = currentCell.Unit; currentCell.EnableHighlight(Color.blue); } }
public void Select(HexCell cell, Unit unit) { Unselect(); cell.EnableHighlight(unit.PlayerID); selectedUnitGO = unit.gameObject; selectedCell = cell; if (infoUserCanvas != null) { infoUserCanvas.OpenUnitPanel(unit); } }
void HandleTouchInput() { //Checking if the player has just tapped the screen string debugString = "SelectedBot with a touch"; Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: debugString = "[TOUCH PHASE BEGAN]"; ClearStartingCells(); hexGrid.ClearPath(); //Select Cell HexCell cell = GetCellUnderCursor(); if (cell) { cell.EnableHighlight(hexGrid.startHexColor); if (cell.unit) { startCell = cell; currentCell = startCell; } } break; case TouchPhase.Moved: debugString = "[TOUCH PHASE MOVED]"; previousCell = currentCell; currentCell = GetCellUnderCursor(); isMoving = true; hexGrid.FindPath(startCell, currentCell, speed - hexGrid.hexesTravelled); break; case TouchPhase.Ended: debugString = "[TOUCH PHASE ENDED]"; previousCell = currentCell; currentCell = null; hexGrid.DoMove(selectedBot); isMoving = false; break; } if (selectedBot) { debugString = "Selected Bot"; } selectedBot.AttackTarget.text = debugString; return; }
public void FindPath(HexCell fromCell, HexCell toCell, HexUnit unit) { ClearPath(); bool pathExists = Search(fromCell, toCell, unit); if (pathExists) { ShowPath(fromCell, toCell, unit); } fromCell.EnableHighlight(Color.blue); toCell.EnableHighlight(Color.red); }
bool Search(HexCell fromCell, HexCell toCell, int speed) { #region Setting all Hex Distances to Max int value and building the open set for (int i = 0; i < cells.Length; i++) { cells[i].Distance = int.MaxValue; } Queue <HexCell> openSet = new Queue <HexCell>(); fromCell.Distance = 0; openSet.Enqueue(fromCell); #endregion #region Breadth First Search bool pathfound = false; while (openSet.Count > 0) { HexCell current = openSet.Dequeue(); #region Termination Condition if (current == toCell) { pathfound = true; } #endregion #region Recurse for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if (neighbor != null) { //This currently Checks all it's neighbors if (neighbor.Distance == int.MaxValue) { neighbor.Distance = current.Distance + 1; if (neighbor.Distance == speed - 1) { neighbor.EnableHighlight(defaultHexColor); } neighbor.PathFrom = current; openSet.Enqueue(neighbor); } //To search more intelligently, we would add an else statement here to check if any other cells have a lower heuristic } } #endregion } return(pathfound); #endregion }
/// <summary> /// Shows the path. /// </summary> /// <param name="speed">Speed.</param> public void ShowPath(/*int speed*/) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { //int turn = (current.Distance-1) / speed; current.EnableHighlight(Color.white); current = current.PathFrom; current.turnsToReach = 0; } } currentPathFrom.EnableHighlight(Color.blue); }
void ShowPath(int speed) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { //current.SetLabel(turn.ToString()); current.EnableHighlight(Color.white); current = current.PathFrom; } currentPathFrom.EnableHighlight(Color.blue); currentPathTo.EnableHighlight(Color.green); } }
void ShowPath() { if (currentPathExists) { HexCell current = currentPathEnd; while (current != currentPathStart) { int turn = current.Distance; current.EnableHighlight(); current = current.PathFrom; } } currentPathStart.EnableBeginPathHighlight(); currentPathEnd.EnableEndPathHighlight(); }
void ShowPath() { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { current.SetLabel(current.Distance.ToString()); current.EnableHighlight(Color.white); current = current.PathFrom; } } currentPathFrom.EnableHighlight(Color.blue); currentPathTo.EnableHighlight(Color.red); }
void ShowPath(int speed) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { int turn = (current.Distance - 1) / speed; current.SetLabel(turn.ToString()); current.EnableHighlight(Color.white); current = current.PathFrom; } } currentPathFrom.EnableHighlight(Color.blue); currentPathTo.EnableHighlight(Color.red); }
public void GetHexGridInfo() { currentHexCell = hexGrid.GetCell(currentHexCoordinates); terrain = currentHexCell.TerrainTypeIndex.ToString(); elevation = currentHexCell.Elevation.ToString(); IsUnderwater = currentHexCell.IsUnderwater; HasRiver = currentHexCell.HasRiver; HasRiverBeginOrEnd = currentHexCell.HasRiverBeginOrEnd; HasRoads = currentHexCell.HasRoads; IsSpecial = currentHexCell.IsSpecial; Walled = currentHexCell.Walled; IsVisible = currentHexCell.IsVisible; IsExplored = currentHexCell.IsExplored; Explorable = currentHexCell.Explorable; urbanLevel = currentHexCell.UrbanLevel; currentHexCell.EnableHighlight(Color.yellow); currentHexCell.PortalIndex = 1; //currentHexCell.Walled = true; // currentHexCell.SpaceCrackIndex = 1; //currentHexCell.SpecialIndex = 1; // Debug.Log(currentHexCell.SpaceCrackIndex); //currentHexCell.SpaceCrackIndex //currentHexCell.UrbanLevel = 1; if (preSelectCell.GetValue(0) != null && (HexCell)preSelectCell.GetValue(0) != currentHexCell) { preSelectCell.SetValue(currentHexCell, 1); HexCell cell = (HexCell)preSelectCell.GetValue(0); cell.DisableHighlight(); preSelectCell.SetValue(null, 0); } else if (preSelectCell.GetValue(1) != null && (HexCell)preSelectCell.GetValue(1) != currentHexCell) { preSelectCell.SetValue(currentHexCell, 0); HexCell cell = (HexCell)preSelectCell.GetValue(1); cell.DisableHighlight(); preSelectCell.SetValue(null, 1); } else if (preSelectCell.GetValue(0) == null && preSelectCell.GetValue(1) == null) { preSelectCell.SetValue(currentHexCell, 0); } cellInfoDisplay.text = "Cell Position:" + currentHexCoordinates.ToString() + "\n" + "Terrain:" + terrain + "\n" + "Elevation:" + elevation + "\n" + "Is Underwater:" + IsUnderwater + "\n" + "Has River:" + HasRiver + "\n" + "Has River B OR E:" + HasRiverBeginOrEnd + "\n" + "Has Roads:" + HasRoads + "\n" + "Is Special:" + IsSpecial + "\n" + "Is Visible:" + IsVisible + "\n" + "Is Explored:" + IsExplored + "\n" + "Explorable:" + Explorable; }
private void ShowPath(HexCell fromCell, HexCell toCell, HexUnit unit) { HexCell current = toCell; pathList.Add(current); int speed = unit.Speed; while (fromCell != current) { int turn = (current.Distance - 1) / speed; current.SetLabel(turn); current.EnableHighlight(); current = current.PathFrom; pathList.Add(current); } pathList.Reverse(); }
/// <summary> /// 显示路径 /// </summary> /// <param name="speed"></param> private void ShowPath(int speed) { if (currentPathExists) { var current = currentPathTo; while (current != currentPathFrom) { var turn = (current.Distance - 1) / speed; current.SetLabel(turn.ToString()); current.EnableHighlight(searchPathColor); current = current.PathFrom; } currentPathFrom.EnableHighlight(searchFromColor); currentPathTo.EnableHighlight(searchToColor); } }
IEnumerator TravelPath() { Grid.DecreaseVisibility(currentTravelLocation ? currentTravelLocation : pathToTravel[0], this); pathToTravel[0].DisableHighlight(); for (int i = 1; i < pathToTravel.Count; i++) { currentTravelLocation = pathToTravel[i]; HexCell A = currentTravelLocation; HexCell B = pathToTravel[i - 1]; Direction = A.GetHexDirection(B); spentPoints += GetMoveCost(A, B, Direction); Vector3 a = pathToTravel[i - 1].Position; Vector3 b = currentTravelLocation.Position; float t = Time.deltaTime * travelSpeed; Grid.IncreaseVisibility(pathToTravel[i], this); if (infoScreen) { // if the infocreen is not null, we still have the unit selected after travel. B.EnableHighlight(GameManager.instance.selectedCellColor); infoScreen.UpdateValues(); } for (; t < 1f; t += Time.deltaTime * travelSpeed) { transform.localPosition = Vector3.Lerp(a, b, t); yield return(null); } Grid.DecreaseVisibility(pathToTravel[i], this); B.DisableHighlight(); t -= 1f; } if (infoScreen) { // if the infocreen is not null, we still have the unit selected after travel. location.EnableHighlight(GameManager.instance.selectedCellColor); infoScreen.UpdateValues(); } currentTravelLocation = null; transform.localPosition = location.Position; Grid.IncreaseVisibility(location, this); hasMoved = true; currentState = HexUnitState.Idle; }
void ShowPath(int speed) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { int turn = (current.Distance - 1) / speed; current.SetLabel(turn.ToString()); current.EnableHighlight(Color.magenta); current = current.PathFrom; } } //else { // Debug.Log("Destination can't reached!"); //} currentPathFrom.EnableHighlight(Color.blue); currentPathTo.EnableHighlight(Color.red); }
public void ShowPath(List <HexCell> path) { // 先擦掉之前画的 if (_listPathSaved != null) { foreach (var current in _listPathSaved) { current.SetLabel(current.GetLabelStr(showLabel)); current.DisableHighlight(); } } _listPathSaved.Clear(); if (path == null) { return; } // 再画新的 for (int i = 0; i < path.Count; ++i) { HexCell current = path[i]; string label = $"{i}"; if (current.Unit) { label = $"{i}-<color=#FF0000FF>T</color>"; } current.SetLabel(label); current.EnableHighlight(Color.yellow); } if (path.Count >= 2) { path[0].EnableHighlight(Color.blue); path[path.Count - 1].EnableHighlight(Color.red); } foreach (var current in path) { _listPathSaved.Add(current); } }
void ShowPath(int speed) { if (currentPathExists && currentPathFrom != currentPathTo) { HexCell current = currentPathTo; while (current != currentPathFrom) { current.EnableHighlight(HexGameUI.pathColor); current = current.PathFrom; } currentPathTo.SetLabel(currentPathTo.Distance.ToString()); currentPathTo.EnableHighlight(HexGameUI.toColor); } else { currentPathTo.EnableHighlight(HexGameUI.unableColor); } currentPathFrom.EnableHighlight(HexGameUI.selectedColor); }
bool UpdateCurrentCell() { HexCell cell = grid.GetCell(Camera.main.ScreenPointToRay(Input.mousePosition)); if (cell != currentCell) { if (currentCell) { currentCell.DisableHighlight(); } currentCell = cell; if (currentCell) { currentCell.EnableHighlight(Color.blue); } return(true); } return(false); }
void ShowPath(int speed) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { int turn = current.Distance / speed; current.SetLabel(turn.ToString()); current.EnableHighlight(pathHexColor); if (turn <= 1) { current.inRange = true; } current = current.PathFrom; } currentPathFrom.EnableHighlight(startHexColor); currentPathTo.EnableHighlight(destinationHexColor); } }
void ShowPath(HexUnit unit) { if (unit.currentState == HexUnitState.Idle) { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { current.SetLabel((unit.ActionPoints - current.Distance).ToString()); current.EnableHighlight(GameManager.instance.pathColor); current = current.PathFrom; } currentPathTo.EnableHighlight(GameManager.instance.validColor); } else { currentPathTo.EnableHighlight(GameManager.instance.nonValidColor); } } }
public void ClearPath() { if (currentPathExists) { HexCell current = currentPathTo; while (current != currentPathFrom) { if (reachableCells.Contains(current)) { current.EnableHighlight(Color.green); } else { current.DisableHighlight(); } current.SetLabel(null); current = current.PathFrom; } current.DisableHighlight(); currentPathExists = false; } currentPathFrom = currentPathTo = null; }