2012-04-11 22 views
2

の問題は、ShowPath();それは、このコードに過負荷を保つような方法は、最短ルートを集めることになっている、それは開始と終了のタイルの両方を発見した後、それはUnity 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

答えて

0

あなたはユニティを持っていますかbegginingへの最短ルートを計算し、それを強調プロ?そうであれば、プロファイラを使用して、より多くの時間を費やしている場所を見つけることができます。そうでない場合は、DebuggerをMonoDevelopで実行することができます。コードにいくつかのブレークポイントを置き、Debugボタンを押します。 Unityの別のインスタンスが開きます。その後、プロジェクトを再生すると、ブレークポイントで停止します。その時点から、ステップごとに実行し、ロックされている場所を確認することができます。詳細情報here

+0

よろしくお願いします。unfortunatleyに最新のコードを入れなければなりません。ヒープを削除しました。コードのほんの数行しかなかったので、今朝は急いでいました。 –

+0

大歓迎です。ブールは決して初心者のミスのビットを切り替えることはないが、今すぐパスファインダのスクリプトはすぐにアセットストアに保存されますので、whileループ。= –

+0

@Matthewkingstonああ、私はちょうど私の答えを編集しました.. :( – Roberto

関連する問題