public List <int> FindPath(int startCellIndex, int endCellIndex) { worldMapGlobe.ClearCells(true, false, false); List <int> cellIndices = worldMapGlobe.FindPath(startCellIndex, endCellIndex); if (cellIndices == null) { return(null); // no path found } //This might be a redundant check if (cellIndices.Count == 0) { cellIndices = null; return(null); } List <int> finalPath = new List <int>(); finalPath.Add(cellIndices[0]); //Check that there is enough remaining movement to travel path //Start by getting the cost between the starting cell and the first cell in the path int neighborIndex = worldMapGlobe.GetCellNeighbourIndex(startCellIndex, cellIndices[0]); int pathCost = worldMapGlobe.GetCellNeighbourCost(startCellIndex, neighborIndex); int i = 0; //Get the cumlative cost for the rest of the path foreach (int cellIndex in cellIndices) { if (i < (cellIndices.Count - 1)) { neighborIndex = worldMapGlobe.GetCellNeighbourIndex(cellIndices[i], cellIndices[i + 1]); pathCost = pathCost + worldMapGlobe.GetCellNeighbourCost(cellIndices[i], neighborIndex); if (pathCost <= TravelRange) { finalPath.Add(cellIndices[i + 1]); } else { return(finalPath); } i++; } } return(finalPath); }
void HandleOnCellClick(int cellIndex) { Debug.Log("Clicked cell: " + cellIndex); switch (selectionMode) { case SELECTION_MODE.CUSTOM_PATH: if (selectionState == 0) { firstCell = cellIndex; map.SetCellColor(firstCell, Color.green, true); selectionState = 1; } else { DrawPath(firstCell, cellIndex); selectionState = 0; } break; case SELECTION_MODE.CUSTOM_COST: if (selectionState == 0) { firstCell = cellIndex; map.SetCellColor(firstCell, Color.green, true); selectionState = 1; } else { // Assign crossing cost between previously selected cell and clicked cell map.SetCellNeighbourCost(firstCell, cellIndex, 1000, true); // Draw a line Vector3[] points = map.GetCellSharedEdge(firstCell, cellIndex); if (points != null) { map.ClearCells(true, false, false); map.AddLine(points [0], points [1], Color.yellow, 0, 0, 0.002f, 0); // Switch selection mode back to select first cell } selectionState = 0; } break; } }