public void PaintNodeType(NodeBehaviour node) { if (node) { node.GetComponentInChildren <MeshRenderer>().material.color = node.IsVisitable ? visitableColor : unvisitableColor; } }
public void PaintPathNode(NodeBehaviour node) { if (node) { node.GetComponentInChildren <MeshRenderer>().material.color = paintPathColor; } }
//Starts the pathfinding algorithm public void StartPathfinding() { //You could create any pathfinder: //var myPathFinder = new BFS(); / new GreedyBFS(); / new Dijkstra(); / new AStar(); //and calculate the path: //INode[] path = myPathFinder.CalculatePath(start,end); //Create pathfinder by the selected value on the Algorithm enum IPathFinder pathFinder = CreatePathfinder(pathfindingAlgorithm); if (pathFinder == null) { Debug.Log("Unsupported algorithm: " + pathfindingAlgorithm.ToString()); return; } //Check for start and end nodes NodeBehaviour start = null; NodeBehaviour end = null; RaycastHit hitInfo; if (Physics.Raycast(startPoint.position, Vector3.down, out hitInfo, Mathf.Infinity)) { start = hitInfo.collider.transform.parent.GetComponent <NodeBehaviour>(); } if (Physics.Raycast(endPoint.position, Vector3.down, out hitInfo, Mathf.Infinity)) { end = hitInfo.collider.transform.parent.GetComponent <NodeBehaviour>(); } //If everything is ready, calculates the path if (start != null && end != null) { CleanPath(); //Clean previous painted path lastPath = pathFinder.CalculatePath(start, end); if (lastPath != null && lastPath.Length > 1) { if (paintCoroutine != null) { StopCoroutine(paintCoroutine); } paintCoroutine = StartCoroutine(PaintPathRoutine(lastPath, paintTimeDelay)); //Paint the path } else { Debug.LogWarning("There is not any path connecting start and end nodes."); } } else { Debug.LogWarning("Can not find a path. Start (green) and End (red) points should be above a Node."); } }
//Use a raycast to check whether or not the mouse is pointing on a node before moving the object private void OnMouseDrag() { RaycastHit hitInfo; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity)) { if (hitInfo.collider.transform.parent != null) { //Only move the object if it is on a node NodeBehaviour node = hitInfo.collider.transform.parent.GetComponent <NodeBehaviour>(); if (node != null && node.IsVisitable) { int x = Mathf.RoundToInt(node.transform.position.x); int z = Mathf.RoundToInt(node.transform.position.z); transform.position = new Vector3(x, transform.position.y, z); } } } }