/// <summary> /// Sets the position of a <see cref="NodeObject"/> in the world, and all its children, recursively <para/> /// This method is an <see cref="IEnumerator"/> so the tree is given time to be created, instead of the program freezing whilst it creates the tree in one frame /// </summary> /// <param name="node">The starting node to set the position of</param> IEnumerator SetNodePosition(NodeObject node) { node.SetPosition(visualisationType); nodesGenerated++; foreach (Node child in node.Children) { yield return(new WaitForSeconds(.1f)); StartCoroutine(SetNodePosition((NodeObject)child)); } }
/// <summary> /// If the user has started running MCTS, then display information about it to the UI whilst it generates <para/> /// When the MCTS has finished generating, set the position of each <see cref="NodeObject"/> so that they can be rendered on-screen <para/> /// When each nodes position has been set, switch to the tree navigation UI /// </summary> void Update() { //Don't do anything until the user has started running the MCTS if (mcts == null) { return; } //While the MCTS is still running, display progress information about the time remaining and the amounts of nodes created to the user if (!mcts.Finished) { timeLeft -= Time.deltaTime; if (timeLeft <= 0) { mcts.Finish(); } TreeUIController.UpdateProgressBar((1 - (timeLeft / timeToRunFor)) / 2, "Running MCTS " + mcts.UniqueNodes + " nodes " + timeLeft.ToString("0.0") + "s/" + timeToRunFor.ToString("0.0") + "s"); } //Return if the MCTS has not finished being created if (!mcts.Finished) { return; } //If the MCTS has finished being computed, start to create gameobjects for each node in the tree if (!startedVisualisation) { rootNodeObject = (NodeObject)mcts.Root; rootNodeObject.SetPosition(visualisationType); StartCoroutine(SetNodePosition(rootNodeObject)); startedVisualisation = true; } //Display information on the progress bar about how many node objects have been created, until every node in the tree has its own gameobject if (!allNodesGenerated) { if (nodesGenerated < mcts.UniqueNodes) { TreeUIController.UpdateProgressBar(0.5f + ((float)nodesGenerated / mcts.UniqueNodes / 2), "Creating node objects: " + nodesGenerated + "/" + mcts.UniqueNodes); } else if (nodesGenerated == mcts.UniqueNodes) { //If every node has had a gameobject created for it, then switch to the navigation UI and start to render the game tree TreeUIController.SwitchToNavigationUI(); Camera.main.GetComponent <LineDraw>().linesVisible = true; Camera.main.GetComponent <TreeCameraControl>().CurrentNode = rootNodeObject; TreeUIController.DisplayNodeInfo(mcts.Root); allNodesGenerated = true; LineDraw.SelectNode(rootNodeObject); } } }