Ejemplo n.º 1
0
    //if this platform is part of a path, this function set the previous and next transform
    //public void SetNeighbouringWP(PathSection prev, PathSection next){
    //	prevNeighbouringWP=prev;
    //	nextNeighbouringWP=next;
    //}



    //check if building on particular point of the platform will block all possible route
    //use brute force check to return a flag instantly. try find a more perfomance friendly solution
    public bool CheckForBlock(Vector3 pos, int footprint)
    {
        float gridSize = BuildManager.GetGridSize();
        bool  blocked  = false;

        nextBuildNode = PathFinderTD.GetNearestNode(pos, nodeGraph);
        //Debug.DrawLine(nextBuildNode.pos, nextBuildNode.pos+new Vector3(0, 2, 0), Color.red, 0.5f);

        foreach (PathOnPlatform pathObj in pathObjects)
        {
            //Debug.Log("check path for "+pathObj.path);

            //check if the start of end has been blocked
            if (Vector3.Distance(pos, pathObj.startN.pos) < gridSize / 2)
            {
                return(true);
            }
            if (Vector3.Distance(pos, pathObj.endN.pos) < gridSize / 2)
            {
                return(true);
            }

            //check if the node is in currentPath, if not, then the node is buildable
            //this is only applicable if pathSmoothing is off
            if (!PathFinderTD.IsPathSmoothingOn())
            {
                bool inCurrentPath = false;
                foreach (Vector3 pathPoint in pathObj.currentPath)
                {
                    float dist = Vector3.Distance(pos, pathPoint);
                    if (dist < gridSize / 2)
                    {
                        inCurrentPath = true;
                        break;
                    }
                }

                if (inCurrentPath)
                {
                    //the current node is in current path, check to see if there's other alternative path if this one's blocked
                    //while getting another path, cache it so it can be used later without redo the search
                    //use force instant search so the path is return immediately
                    pathObj.altPath = PathFinderTD.ForceSearch(pathObj.startN, pathObj.endN, nextBuildNode, nodeGraph, footprint);
                    if (pathObj.altPath.Count == 0)
                    {
                        blocked = true;
                    }
                }
                else
                {
                    pathObj.altPath = pathObj.currentPath;
                }
            }
            else
            {
                //the current node is in current path, check to see if there's other alternative path if this one's blocked
                //while getting another path, cache it so it can be used later without redo the search
                //use force instant search so the path is return immediately
                pathObj.altPath = PathFinderTD.ForceSearch(pathObj.startN, pathObj.endN, nextBuildNode, nodeGraph, footprint);

                if (pathObj.altPath.Count == 0)
                {
                    blocked = true;
                }

                if (blocked)
                {
                    break;
                }
            }
        }

        return(blocked);

        /*
         * float gridSize=BuildManager.GetGridSize();
         *
         * //check if the start of end has been blocked
         * if(Vector3.Distance(pos, startN.pos)<gridSize/2) return true;
         * if(Vector3.Distance(pos, endN.pos)<gridSize/2) return true;
         *
         * //check if the node is in currentPath, if not, then the node is buildable
         * //this is only applicable if pathSmoothing is off
         * if(!PathFinder.IsPathSmoothingOn()){
         *      bool InCurrentPath=false;
         *
         *      foreach(Vector3 pathPoint in currentPath){
         *              float dist=Vector3.Distance(pos, pathPoint);
         *              if(dist<gridSize/2){
         *                      InCurrentPath=true;
         *                      break;
         *              }
         *      }
         *
         *      if(!InCurrentPath) {
         *      //Debug.Log("not in current path ");
         *              return false;
         *      }
         * }
         *
         *
         * Debug.Log("calling pathfinder, check for block");
         *
         * //the current node is in current path, check to see if there's other alternative path if this one's blocked
         * //while getting another path, cache it so it can be used later without redo the search
         * nextBuildNode=PathFinder.GetNearestNode(pos, nodeGraph);
         * //use force instant search so the path is return immediately
         * altPath=PathFinder.ForceSearch(startN, endN, nextBuildNode, nodeGraph);
         *
         * if(altPath.Count>0) return false;
         */

        //return true;
    }