簡體   English   中英

Unity 3D C#代碼,重載計算機沒有錯誤

[英]Unity 3D C# code, overloads computer no error

問題在於ShowPath(); 方法,因為它不斷超載,所以該代碼應該收集最短的路線,然后在找到開始和結束圖塊時將其突出顯示,從而計算出到達起點的最短路線

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;
    }

}

}

您有Unity Pro嗎? 如果是這樣,您可以使用探查器來查找花費更多時間的位置。 如果沒有,您可以在MonoDevelop中運行Debugger :在代碼中放置一些斷點,然后按“ Debug按鈕; 它將打開另一個Unity實例。 然后您播放該項目,它將在您的斷點處停止。 從這一點開始,您可以逐步運行並查看其鎖定位置。 更多信息在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM