//Path Generation private TileA GetCheapestTile(TileA[] arr) { float bestScore = float.MaxValue; //initialize TileA bestTile = null; //initialize for (int i = 0; i < arr.Length; ++i) { if (arr[i].fScore < bestScore) //if current tile has the best score { bestTile = arr[i]; //set to current index bestScore = arr[i].fScore; //set to current index score } } return(bestTile); }
private Phase currentPhaseA = Phase.PreSetupA; //start at PreSetUp phase public void Setup() { int startIdxA = (graphA.gridHeight / 2) * graphA.gridWidth; //sets a new int at the start of the graph int endIdxA = startIdxA + graphA.gridWidth - 1; //sets a new int at the end of the graph startTileA = graphA.tilesA[startIdxA]; //sets startTile to the first tile using startIdx endTileA = graphA.tilesA[endIdxA]; //sets endTile to the last tile at the end of the graph using endIdx Instantiate(spawnPrefabA, startTileA.transform.position, Quaternion.identity); //spawns enemy spawner at startTile position GameObject defendInstanceA = Instantiate(defendPrefabA, endTileA.transform.position, Quaternion.identity); //spawns base at endTile position defendInstanceA.GetComponent <BaseA>().gameStateA = this; enemyPathA = graphA.CalculatePath(startTileA, endTileA); //uses CalculatePath function to make its way from enemy spawner to base/makes its way from startTile to endTile(startTile and endTile are used as start and end points) currentPhaseA = Phase.BuildA; //set current state to build }
// whats the tile, whats the destination tile private int GetHScoreManhattan(TileA selectedTile, TileA destinationTile) { Vector2Int tileOffset = selectedTile.tilePosition - destinationTile.tilePosition; return(Math.Abs(tileOffset.x) + Math.Abs(tileOffset.y));//returns score as a single int (getting the differences between the x's and y's and putting it into one val) to get h score }
public TileA[] CalculatePath(TileA origin, TileA destination) // CalculatePath(start and end) { ResetNodes(); //clear old data List <TileA> openListA = new List <TileA>(); //nodes that have NOT been traversed through List <TileA> closedListA = new List <TileA>(); //nodes that have been traversed through openListA.Add(origin); //add starting tile while (openListA.Count != 0 && // still stuff left to explore !closedListA.Contains(destination)) // AND we haven't reached the destination yet { // TODO: replace this with a proper sorted array implementation TileA currentA = GetCheapestTile(openListA.ToArray());//update current openListA.Remove(currentA); //remove current node from list closedListA.Add(currentA); //add current node to list // TODO... // calculate g scores for connected tiles //throw new System.NotImplementedException(); for (int i = 0; i < currentA.connectedTilesA.Length; ++i) //until it reaches the last connected tile { TileA adjTileA = currentA.connectedTilesA[i]; //create adjTile and set to current connected tile int calGScore = currentA.gScore + adjTileA.cost; //calculate gScore = current gScore + travel cost(hard-coded to 1) if (!adjTileA.traversible) { continue; } //if not traversable move on to next node if (adjTileA.previousTile == null || //if adjTile previous tile is equal to null(I think this condition is used because preiousTile starts off null) calGScore < adjTileA.gScore) //or estScore is less than current adjTile gScore { adjTileA.previousTile = currentA; //set adjTile previous tile to current(adjTile is one tile ahead of current) adjTileA.gScore = calGScore; adjTileA.hScore = GetHScoreManhattan(adjTileA, destination); } if (!closedListA.Contains(adjTileA) && !openListA.Contains(adjTileA)) //if neither of theses lists have this tile { openListA.Add(adjTileA); //add tile to openList } } //// iterate through all connected tiles //for (int i = 0; i < currentA.connectedTilesA.Length; ++i) //{ // // create variable for to current connected tile for readability // TileA adjTileA = currentA.connectedTilesA[i]; // // skip tiles that were already processed or not traversible // if (closedListA.Contains(adjTileA) || !adjTileA.traversible) { continue; } // // NOTE: hard-coded cost of 1 // int estGScore = currentA.gScore + 1; // if (adjTileA.previousTile == null || // there is no score (no previous tile) OR // estGScore < adjTileA.gScore) // this is a cheaper route... // { // adjTileA.previousTile = currentA; // adjTileA.gScore = estGScore; // adjTileA.hScore = GetHScoreManhattan(adjTileA, destination); // } // if (!closedListA.Contains(adjTileA) && !openListA.Contains(adjTileA))//if neither of theses lists have this tile // { // openListA.Add(adjTileA);//add tile to openList // } //} } List <TileA> path = new List <TileA>(); //create list if (closedListA.Contains(destination)) //if closedList destination was reached { TileA prevTile = destination; //start at the end of closedList while (prevTile != origin) //while on not at the start of closedList { path.Add(prevTile); //add a previous tile prevTile = prevTile.previousTile; //update previous tile so that it can be added the next loop } path.Add(prevTile); //add origin/start/first node to the end of the list } path.Reverse();//reverses path order return(path.ToArray()); }