public void Prebuild(Vector3 point, UnitTower tower) { if (nodeGraph == null || nodeGraph.Length == 0) { return; } NodeTD node = PathFinderTD.GetNearestNode(point, nodeGraph, 0); node.walkable = false; List <NodeTD> nodeList = PathFinderTD.GetNodeInFootprint(node, tower.GetFootprint()); foreach (NodeTD n in nodeList) { n.walkable = false; } tower.SetPlatform(this, node); foreach (PathOnPlatform pathObj in pathObjects) { queue.Add(pathObj.thisWP); PathFinderTD.GetPath(pathObj.startN, pathObj.endN, nodeGraph, this.SetPath); } }
public void InitNode(NodeTD[] nodeGraph) { Vector3 prevPoint; if (prevNeighbouringWP.platform != null) { Debug.Log("dong"); //place holder, platform to platform connection not supported yet prevPoint = prevNeighbouringWP.platform.thisT.position; } else { prevPoint = prevNeighbouringWP.pos; } startN = PathFinderTD.GetNearestNode(prevPoint, nodeGraph); Vector3 nextPoint; if (nextNeighbouringWP.platform != null) { Debug.Log("ding"); //place holder, platform to platform connection not supported yet nextPoint = nextNeighbouringWP.platform.thisT.position; } else { nextPoint = nextNeighbouringWP.pos; } endN = PathFinderTD.GetNearestNode(nextPoint, nodeGraph); //Debug.DrawLine(endN.pos, endN.pos+new Vector3(0, 2, 0), Color.red, 5); //Debug.DrawLine(startN.pos, startN.pos+new Vector3(0, 2, 0), Color.blue, 5); //Debug.DrawLine(nextNeighbouringWP.pos, nextNeighbouringWP.pos+new Vector3(0, 2, 0), Color.green, 5); }
public void SearchForNewPath(PathSection PS) { queue.Add(PS); /*Vector3 prevPoint; * if(prevNeighbouringWP.platform!=null){ * //place holder, platform to platform connection not supported yet * prevPoint=prevNeighbouringWP.pos; * } * else prevPoint=prevNeighbouringWP.pos; * startN=PathFinder.GetNearestNode(prevPoint, nodeGraph); * * Vector3 nextPoint; * if(nextNeighbouringWP.platform!=null){ * //place holder, platform to platform connection not supported yet * nextPoint=nextNeighbouringWP.pos; * } * else nextPoint=nextNeighbouringWP.pos; * endN=PathFinder.GetNearestNode(nextPoint, nodeGraph);*/ foreach (PathOnPlatform path in pathObjects) { //Debug.Log("init path ................."+path.path); if (path.thisWP == PS) { path.InitNode(nodeGraph); PathFinderTD.GetPath(path.startN, path.endN, nodeGraph, this.SetPath); } } //PathFinder.GetPath(startN, endN, nodeGraph, this.SetPath); }
static public void Init() { GameObject obj = new GameObject(); pathFinder = obj.AddComponent <PathFinderTD>(); obj.name = "PathFinder"; }
public void Build(Vector3 point, UnitTower tower) { //pathfinding related code, only call if this platform is walkable; if (walkable) { if (tower.type != _TowerType.Mine) { //if build on the node last check for block, use the cached node and path if (nextBuildNode != null && Vector3.Distance(nextBuildNode.pos, point) < BuildManager.GetGridSize() / 2) { //Debug.Log("use cached path"); nextBuildNode.walkable = false; List <NodeTD> nodeList = PathFinderTD.GetNodeInFootprint(nextBuildNode, tower.GetFootprint()); foreach (NodeTD node in nodeList) { node.walkable = false; } tower.SetPlatform(this, nextBuildNode); foreach (PathOnPlatform pathObj in pathObjects) { int rand = pathObj.pathID; while (rand == pathObj.pathID) { rand = Random.Range(-999999, 999999); } pathObj.SetPath(pathObj.altPath, rand); //forceSearch doesnt smooth path so path smoothing is call //smoothPath will only valid path smoothing is enable in PathFinder pathObj.SmoothPath(); } } //if build on node that is unchecked, find the block node and initiate a new path search else { //Debug.Log("unexpected build point, query for new path"); NodeTD node = PathFinderTD.GetNearestNode(point, nodeGraph); node.walkable = false; //~ Debug.Log(node.ID); List <NodeTD> nodeList = PathFinderTD.GetNodeInFootprint(node, tower.GetFootprint()); foreach (NodeTD n in nodeList) { n.walkable = false; } tower.SetPlatform(this, node); foreach (PathOnPlatform pathObj in pathObjects) { queue.Add(pathObj.thisWP); PathFinderTD.GetPath(pathObj.startN, pathObj.endN, nodeGraph, this.SetPath); } } } } }
//find a new independant subpath based on current pathSection's platform graph private void SearchSubPath() { currentPathID = currentPS.GetPathID(); Vector3 pos = thisT.TransformPoint(0, 0, BuildManager.GetGridSize()); PathFinderTD.GetPath(pos, subPath[subPath.Count - 1], currentPS.platform.GetNodeGraph(), this.SetSubPath); //PathFinder.GetPath(thisT.position, subPath[subPath.Count-1], currentPS.platform.GetNodeGraph(), this.SetSubPath); }
public void UnBuild(NodeTD node, int footprint) { footprint = 0; //Debug.Log("unbuild "+node.ID); node.walkable = true; List <NodeTD> nodeList = PathFinderTD.GetNodeInFootprint(node, footprint); foreach (NodeTD n in nodeList) { n.walkable = true; } foreach (PathOnPlatform pathObj in pathObjects) { //~ Debug.Log("update path"); queue.Add(pathObj.thisWP); PathFinderTD.GetPath(pathObj.startN, pathObj.endN, nodeGraph, this.SetPath); } }
public void SmoothPath() { currentPath = PathFinderTD.SmoothPath(currentPath); }
public NodeTD GetNearestNode(Vector3 point) { return(PathFinderTD.GetNearestNode(point, nodeGraph)); }
//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; }
void Awake() { pathFinder = this; }