示例#1
0
    //A* pathfinding for faults
    public static List<IntVector2> GetPath(IntVector2 start, IntVector2 goal, List<IntVector2> nodes)
    {
        List<PathHeuristic> openList = new List<PathHeuristic> ();
        List<PathHeuristic> closedList = new List<PathHeuristic> ();

        PathHeuristic currentTile = new PathHeuristic(start);

        float startFuncTime = Time.realtimeSinceStartup;

        do {
            //Get the best fault using heuristics
            if(openList.Count>0){
                PathHeuristic lowestFTile = null;
                foreach(PathHeuristic tile in openList){
                    if(lowestFTile==null){
                        lowestFTile = tile;
                    }
                    else if(tile.score<lowestFTile.score){
                        lowestFTile = tile;
                    }
                    if(lowestFTile.tileIndex==goal){
                        lowestFTile = tile;
                        break;
                    }
                }
                currentTile = lowestFTile;
            }

            //Put the currentTile in closedList and remove from open
            closedList.Add(currentTile);
            openList.Remove(currentTile);

            //If the current is at the end
            if(currentTile.tileIndex == goal){
                List<IntVector2> pathList = new List<IntVector2>();

                while(currentTile.parent!=null){
                    //Add parent to the pathlist
                    pathList.Add(currentTile.tileIndex);
                    currentTile = currentTile.parent;
                }
                pathList.Add(start);
                pathList.Reverse();

                float time = (Time.realtimeSinceStartup-startFuncTime)*100;

                return pathList;
            }

            //Set adjacent list up
            List<IntVector2> adjacentList = GetAdjacentNodes(currentTile.tileIndex, nodes);

            foreach(IntVector2 adjacent in adjacentList){
                //If adjacent is in closed list, skip
                bool adjacentInClosed = false;
                foreach(PathHeuristic tile in closedList){
                    if(tile.tileIndex == adjacent){
                        adjacentInClosed = true;
                    }
                }

                if(adjacentInClosed){
                    continue;
                }

                //If not in open list, add it
                bool adjacentInOpen = false;
                foreach(PathHeuristic tile in openList){
                    if(tile.tileIndex == adjacent){
                        adjacentInOpen = true;
                    }
                }
                //Add to open list
                if(!adjacentInOpen){
                    PathHeuristic newTile = new PathHeuristic(adjacent, currentTile);
                    //Score
                    newTile.SetScore((int)(IntVector2.Distance(adjacent,goal)*10));
                    openList.Add(newTile);
                }
            }
        } while(openList.Count>0);
        //Unable to find a path between the start-goal
        return null;
    }