private void FindPath(Vector3 startPosition, Vector3 targetPosition) { Node startNode = grid.ConvertToGridPosition(startPosition); Node targetNode = grid.ConvertToGridPosition(targetPosition); List <Node> openList = new List <Node>(); List <Node> closedList = new List <Node>(); openList.Add(startNode); while (openList.Count > 0 && !closedList.Contains(targetNode)) { Node currentNode = openList[0]; for (int i = 1; i < openList.Count; i++) { if (openList[i].weight <= currentNode.weight) { currentNode = openList[i]; } } openList.Remove(currentNode); closedList.Add(currentNode); if (currentNode == targetNode) { RetracePath(startNode, targetNode); return; } foreach (Node connectedNodes in currentNode.connections) { if (!connectedNodes.nodeBlocked || closedList.Contains(connectedNodes)) { //Do Nothing continue; } int newWeightForConnection = currentNode.weight + GetDistance(currentNode, connectedNodes); if (newWeightForConnection < connectedNodes.weight && !openList.Contains(connectedNodes)) { connectedNodes.weight = newWeightForConnection; connectedNodes.parent = currentNode; if (!openList.Contains(connectedNodes)) { openList.Add(connectedNodes); } } } } }
private void SetWall() { RaycastHit hitInfo; //Place an obstacle Ray intoScreen = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(intoScreen, out hitInfo, 1000000, floorOnly) && numWallsCanPlace > 0 && playerTurn) { Node newWall = grid.ConvertToGridPosition(new Vector3(hitInfo.point.x, 0, hitInfo.point.z)); if (newWall.nodeBlocked == false) { wallPlaceholder = Instantiate(wall); wallPlaceholder.transform.position = newWall.nodePosition; grid.checkWall = true; numWallsCanPlace--; } } }
private void FindPath(Vector3 startPosition, Vector3 targetPosition) { Node startNode = grid.ConvertToGridPosition(startPosition); Node targetNode = grid.ConvertToGridPosition(targetPosition); if (onlyOnce) { pathToFollow = new List <Node>(); onlyOnce = false; } List <Node> openList = new List <Node>(); List <Node> closedList = new List <Node>(); print("The start of the open list count: " + openList.Count); print("The start of the closed list count: " + closedList.Count); openList.Add(startNode); while (openList.Count > 0) { Node currentNode = openList[0]; for (int i = 1; i < openList.Count; i++) { if (openList[i].weight <= currentNode.weight) { currentNode = openList[i]; } } openList.Remove(currentNode); closedList.Add(currentNode); grid.checkClosed = closedList; if (currentNode == targetNode) { RetracePath(startNode, targetNode); return; } foreach (Node connectedNodes in currentNode.connections) { if (closedList.Contains(connectedNodes)) { //Do Nothing continue; } float oldDistance = Vector3.Distance(currentNode.nodePosition, targetNode.nodePosition); float newDistance = Vector3.Distance(connectedNodes.nodePosition, targetNode.nodePosition); if (!openList.Contains(connectedNodes)) { if (newDistance < oldDistance) { connectedNodes.weight = currentNode.weight; connectedNodes.parent = currentNode; if (!openList.Contains(connectedNodes)) { openList.Add(connectedNodes); } } } } } }