2012-04-11 61 views
2

問題是用ShowPath();方法,因爲它使超載這個代碼應該收集的最短路線,然後選中它一旦發現開始和結束瓷磚它計算的最短路線begginingUnity 3D C#代碼,過載電腦沒有錯誤

using UnityEngine; 
using System; 
using System.Collections.Generic; 

namespace PathfindingClass 
{ 
    public class pathFinding 
    { 
      public bool startFound = false; 
    public TileClass.Tile[,] grid = new TileClass.Tile[AStarPath.gridWidth,AStarPath.gridHeight]; 
    public Vector2 startTile; 
    public Vector2 endTile; 
    public Vector2 currentTile; 

    // create a list that stores the checked tiles 
    List<Vector2> openList = new List<Vector2>(); 
    List<Vector2> closedList = new List<Vector2>(); 

    public pathFinding (TileClass.Tile[,] grid) 
    { 
     this.grid = grid; 

    } 

    public void SearchPath(Vector2 startTile, Vector2 endTile){ 
     this.startTile = startTile; 
     this.endTile = endTile; 

     #region Path Validation 
     bool canSearch = true; 

     if(grid[(int)startTile.x,(int)startTile.y].walkable ==false){ 
     canSearch = false; 
      Console.WriteLine("the start square is not walkable"); 
     } 
     if(grid[(int)endTile.x,(int)endTile.y].walkable ==false){ 
     canSearch = false; 
      Console.WriteLine("the end square is not walkable"); 
     } 

     #endregion 

     if(canSearch){ 
      //add the starting tile to the open list 
     openList.Add(startTile);  
      currentTile = new Vector2(-1,-1); 

      //while the open list is not empty 
      while(openList.Count > 0){ 
       currentTile = getTyleWithLowestTotal(openList); 

       //if the current tile is the end tile stop searching 
       if((int)currentTile.x == (int)endTile.x && (int)currentTile.y == (int)endTile.y ){ 
       // if((int)currentTile.x == (int)endTile.x){ 
         break; 
        //} 
       }else{ 
        openList.Remove(currentTile); 
        closedList.Add(currentTile); 

        //get all the adjacent tiles 
        List<Vector2> adjacentTiles = getAdjacentTiles(currentTile); 

        foreach(Vector2 adjacentTile in adjacentTiles){ 
        // the adjacent tile is not aloude within eith of the open or closed lists 
         if(!openList.Contains(adjacentTile)){ 
          if(!closedList.Contains(adjacentTile)){ 
           // move it to the open list 
           openList.Add(adjacentTile); 

           TileClass.Tile tile = grid[(int)adjacentTile.x,(int)adjacentTile.y]; 

           tile.cost = grid[(int)adjacentTile.x,(int)adjacentTile.y].cost+1; 

           //calculate the manhattan distance 
           tile.horistic = ManhattanDistance(adjacentTile); 

           //calculate the total cost 
           tile.total = tile.cost + tile.horistic; 

           tile.color = new Vector4(0,0,1,1); 
           tile.Y=2; 
           } 
         }       
        } 
       } 
      } 
     } 
     grid[(int)startTile.x,(int)startTile.y].color = Color.yellow; 
     grid[(int)endTile.x,(int)endTile.y].color = Color.yellow; 

     //Show the shortestPath 

     ShowPath(); 
    } 


    public void ShowPath(){ 
     Vector2 currentTile = endTile; 
     List<Vector2> PathTiles = new List<Vector2>(); 

     while(!startFound){ 
     List<Vector2> adjacentTiles = getAdjacentTiles(currentTile); 

      //check to see what the used current tile is 
      foreach(Vector2 adjacentTile in adjacentTiles){ 
       if(openList.Contains(adjacentTile) || closedList.Contains(adjacentTile)){ 

        grid[(int)adjacentTile.x,(int)adjacentTile.y].color = Color.yellow; 

        if(adjacentTile.x == startTile.x){ 
         startFound = true; 
        break; 
        } 
       } 
      } 
     }   
    } 



    //calculate the manhattan distance 
    public int ManhattanDistance(Vector2 adjacentTile){ 
    int manhattan = Math.Abs((int)(endTile.x - adjacentTile.x)) + Math.Abs((int)(endTile.y - adjacentTile.y)); 
     return manhattan; 
    } 

        //check the adjacent tiles to the current tile 
    public List<Vector2> getAdjacentTiles(Vector2 currentTile){ 
     List<Vector2> adjacentTiles = new List<Vector2>(); 
     Vector2 adjacentTile; 

     //above 
     adjacentTile = new Vector2(currentTile.x,currentTile.y+1); 
     if(adjacentTile.y < AStarPath.gridHeight && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){ 
      adjacentTiles.Add(adjacentTile); 
     } 
        //below 
     adjacentTile = new Vector2(currentTile.x,currentTile.y-1); 
     if(adjacentTile.y >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){ 
      adjacentTiles.Add(adjacentTile); 
     } 
        //right 
     adjacentTile = new Vector2(currentTile.x +1,currentTile.y); 
     if(adjacentTile.x < AStarPath.gridWidth && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){ 
      adjacentTiles.Add(adjacentTile); 
     } 
        //left 
     adjacentTile = new Vector2(currentTile.x -1,currentTile.y); 
     if(adjacentTile.x >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){ 
      adjacentTiles.Add(adjacentTile); 
     } 

     //optional to add diagonal checking 
     return adjacentTiles; 
    } 

    // get the tiles with the lowest total value 
    public Vector2 getTyleWithLowestTotal(List<Vector2> openList){ 
     //temp vars 
     Vector2 tileWithLowestTotal = new Vector2(-1,-1); 
     int lowestTotal = int.MaxValue; 

     // search all the open tiles and get the tile with the lowest total cost 
     foreach(Vector2 openTile in openList){ 
      if(grid[(int)openTile.x,(int)openTile.y].total <= lowestTotal){ 
      lowestTotal = grid[(int)openTile.x,(int)openTile.y].total; 
       tileWithLowestTotal = grid[(int)openTile.x,(int)openTile.y].ID; 
      }    
     } 
    return tileWithLowestTotal; 
    } 

} 

}

+1

是'startFound'曾設置爲'true'? – deltree 2012-04-11 22:07:48

回答

0

你有統一臨?如果是這樣,您可以使用分析器查找花費更多時間的位置。如果沒有,您可以在MonoDevelop中運行Debugger:在代碼中放置一些斷點並按下Debug按鈕;它會打開Unity的另一個實例。然後你玩這個項目,它會停在你的斷點上。從這一點開始,您可以逐步運行並查看它被鎖定的位置。更多信息here

+0

對不起將不得不把最新的代碼放在unfortunatley我刪除了一堆,因爲它是更多的幾行代碼,並在今天上午高呼歡呼 – 2012-04-11 23:50:04

+0

好的謝謝你們經過大量的挖掘和調試腳本徘徊在while循環,因爲布爾永遠不會切換位新手的錯誤,但工作良好現在路徑查找器腳本將在免費資產商店很快=) – 2012-04-12 13:09:56

+0

@Matthewkingston哦,我剛剛編輯我的答案.. :( – Roberto 2012-04-12 13:17:33