private void Awake() { //initialize reference to self self = this; visualizer = GetComponent <Pathfinding_Visualizer>(); grid = new Node[widthSize, heightSize, widthSize]; for (int x = 0; x < widthSize; x++) { for (int y = 0; y < heightSize; y++) { for (int z = 0; z < widthSize; z++) { grid[x, y, z] = new Node(x, y, z); //later zou ik de nodes mogelijk kunnen resetten } } } }
private void Awake() { pathfinding = (PathFinding)target; pathVisualizer = pathfinding.GetComponent <Pathfinding_Visualizer>(); }
//bugs //when out of bounds, will travel to last known destination //when out of range, will repeat same checks and slow down game //bake is raar met specifieke objecten //z werkt niet goed met pathfinding private IEnumerator CalculatePath(Vector3 position, Callable callable) //omhoog / omlaag moet ook werken. hiervoor moet schuin gaan kunnen { PathFinding.Node destination = p.GetNodeFromVector(position); //pathfinding tools open = new List <Node>(); closed = new List <PathFinding.Node>(); Pathfinding_Visualizer pV = PathFinding.visualizer; PathFinding.Node start = p.GetNodeFromVector(transform.position); PathFinding.Node reference = start; int nodesBetween = 0; int y; if (start != null) { while (nodesBetween < maxNodesBetweenGroundAndTarget) { y = reference.y - nodesBetween; if (!(y > 0 && y < p.grid.GetLength(1))) { nodesBetween++; continue; } reference = p.grid[start.x, y, start.z]; nodesBetween++; if (reference.filled && reference.bakeType == PathFinding.BakeType.Object) { break; } } if (start.filled) { open.Add(new Node(reference)); } else { Debug.Log("There is nothing walkable around the start point."); } } else { Debug.Log("Currently not in a walkable area, unable to create a path."); } if (start != null) { reference = destination; nodesBetween = -maxNodesBetweenGroundAndTargetReverse; y = 0; if (destination != null) { while (nodesBetween < maxNodesBetweenGroundAndTarget) { y = reference.y - nodesBetween; if (!(y > 0 && y < p.grid.GetLength(1))) { nodesBetween++; continue; } reference = p.grid[start.x, y, start.z]; //BUG nodesBetween++; if (reference.filled && reference.bakeType == PathFinding.BakeType.Object) { break; } } } } //cost calculation if (start != null) { origin = new Vector3(start.x, start.y, start.z); } if (destination != null) { dest = new Vector3(destination.x, destination.y, destination.z); } int checks = 0; //main loop if (destination != null) { while (open.Count > 0 && closed.Count < maxNodesCheckable) //hij gaat nu via het gevulde een pad zoeken ipv bovenop het pad { open.Sort(); PathFinding.Node node; node = open[0].node; if (closed.Contains(node)) { open.RemoveAt(0); continue; } if (p.visualize) { //in dit geval weet je al dat +1 bestaat omdat dat een check gaat worden pV.ChangeColorGridPart(p.grid[node.x, node.y + 1, node.z], Color.green); } checks++; if (checks >= checksPerFrame) { checks = 0; yield return(null); } adjecentNodes = new List <Node>(); nodeToCheck = open[0]; n = nodeToCheck.node; //check if destination if (Vector3.Distance(dest, new Vector3(n.x, n.y, n.z)) <= stoppingNodeDistance) { break; } open.RemoveAt(0); closed.Add(nodeToCheck.node); //front PrepareNode(n.x, n.y, n.z + 1); //back PrepareNode(n.x, n.y, n.z - 1); //right PrepareNode(n.x + 1, n.y, n.z); //left PrepareNode(n.x - 1, n.y, n.z); if (!_2d) { //top checks PrepareNode(n.x, n.y + 1, n.z + 1); PrepareNode(n.x + 1, n.y + 1, n.z + 1); PrepareNode(n.x + 1, n.y + 1, n.z); PrepareNode(n.x + 1, n.y + 1, n.z - 1); PrepareNode(n.x, n.y + 1, n.z - 1); PrepareNode(n.x - 1, n.y + 1, n.z - 1); PrepareNode(n.x - 1, n.y + 1, n.z); PrepareNode(n.x - 1, n.y + 1, n.z + 1); //bottom checks PrepareNode(n.x, n.y - 1, n.z + 1); PrepareNode(n.x + 1, n.y - 1, n.z + 1); PrepareNode(n.x + 1, n.y - 1, n.z); PrepareNode(n.x + 1, n.y - 1, n.z - 1); PrepareNode(n.x, n.y - 1, n.z - 1); PrepareNode(n.x - 1, n.y - 1, n.z - 1); PrepareNode(n.x - 1, n.y - 1, n.z); PrepareNode(n.x - 1, n.y - 1, n.z + 1); } foreach (Node _node in adjecentNodes) { open.Add(_node); } } } //check if path has been found //if curnode != null //if curnode == destination //convert everything to a vector3 list List <Vector3> _path = new List <Vector3>(); Node curNode; Vector3 pos; if (open.Count > 0) { curNode = open[0]; open.RemoveAt(0); while (curNode.parent != null) { //convert pos = p.GetVectorFromNode(curNode.node); pos.y += p.heightSizeNode; if (p.visualize) { pV.ChangeColorGridPart(curNode.node, Color.red); } _path.Add(pos); curNode = curNode.parent; } _path.Add(p.GetVectorFromNode(curNode.node)); } calculate = null; if (_path.Count == 0) { Debug.Log("No path could be found."); } if (callable != null) { callable(_path); } }
private void Awake() { visualizer = GetComponent <Pathfinding_Visualizer>(); Bake(); }