private IEnumerator ReverseRemoveCurrentNode(NodeRepresentation nodeRep, bool moveOther) { graphMain.UpdateCheckList(UtilGraph.LIST_VISUAL, false); if (!moveOther) { // Move element back into stack Vector3 onTopOfStack = NextElementPositionOnTopOfList() - oneListIndexUp; nodeRep.MoveNodeRepresentation(onTopOfStack); nodeRep.SetGravity(false); yield return(mediumDuration); nodeRep.SetGravity(true); } else { // Disable gravity of swapping node reps int lastNodeRep = nodeRepresentations.Count - 1; SetGravityForMultipleNodeReps(0, lastNodeRep, false); // First move all other node representations up yield return(MoveOtherNodes(0, lastNodeRep, false)); // Move element back into list nodeRep.MoveNodeRepresentation(spawnPointList.position); yield return(smallDuration); // Enable gravity of swapping node reps SetGravityForMultipleNodeReps(0, lastNodeRep, true); } graphMain.UpdateCheckList(UtilGraph.LIST_VISUAL, true); graphMain.WaitForSupportToComplete--; }
// Move other nodes up/down to make spot for <..>, changing their list index and position private IEnumerator MoveOtherNodes(int start, int end, bool increment) { // Fix gravity outside this method // Move all existing list elements up (from the point where we want to insert the new element) *** Copied - <--- Methods made for (int i = start + 1; i <= end; i++) //for (int i=listObjects.Count-1; i >= index; i--) { NodeRepresentation involvedNodeRep = nodeRepresentations[i]; // Find new position and move it Vector3 newPos = involvedNodeRep.transform.position + oneListIndexUp; // new Vector3(0f, 1f, 0f); <- oneListIndexUp involvedNodeRep.MoveNodeRepresentation(newPos); if (increment) { involvedNodeRep.ListIndex = i - 1; nodeRepresentations[i - 1] = involvedNodeRep; } else { if (i + 1 > nodeRepresentations.Count - 1) { Debug.Log("Stop!.."); continue; } involvedNodeRep.ListIndex = i + 1; nodeRepresentations[i + 1] = involvedNodeRep; } yield return(involvedNodeRep.HighlightNodeRepresentation(UtilGraph.DIST_UPDATE_COLOR, seconds)); // 1f); } }
// Swap 1 by 1: Not implemented/used private void SwapPositionsOf(NodeRepresentation np1, NodeRepresentation np2) { np1.SetGravity(false); np2.SetGravity(false); Debug.Log("Switching"); Vector3 np1MoveLeft = np1.transform.position + new Vector3(-0.5f, 0f, 0f); np1.MoveNodeRepresentation(np1MoveLeft); Vector3 np2MoveRight = np2.transform.position + new Vector3(0.5f, 0f, 0f); np2.MoveNodeRepresentation(np2MoveRight); Debug.Log("Done switching"); }
// Moves the removed element to the 'current node' spot private void MoveCurrentNode(NodeRepresentation currentNodeRep, bool moveOther) { UpdateListIndexes(); // Move object to current node location currentNodeRep.MoveNodeRepresentation(currentNodePoint.position); currentNodeRep.CurrentColor = UtilGraph.TRAVERSE_COLOR; currentNodeRep.name = name = "NodeRep " + currentNodeRep.Node.NodeAlphaID + " (X)"; if (moveOther) { // Move other elements in Queue/Priority list foreach (NodeRepresentation otherobj in nodeRepresentations) { otherobj.MoveNodeRepresentation(otherobj.transform.position - oneListIndexUp); //new Vector3(0f, 1f, 0f)); } } }
// Adding a visual representation of the node, with priority based on its distance value) public void PriorityAdd(Node node, int index) { UpdateListRoofPosition(); // Move all existing list elements up (from the point where we want to insert the new element) for (int i = 0; i < index; i++) { // Remove gravity to make it easier to move the objects nodeRepresentations[i].GetComponent <Rigidbody>().useGravity = false; // Find new position and move it Vector3 newPos = nodeRepresentations[i].transform.position + oneListIndexUp; // new Vector3(0f, 1f, 0f); nodeRepresentations[i].GetComponent <MoveObject>().SetDestination(newPos); } // Add new element into the open slot Vector3 pos = spawnPointList.position + oneListIndexUp * (nodeRepresentations.Count - index); // new Vector3(0f, 1f, 0f) <- oneListIndexUp NodeRepresentation newPrioNodeRep = CreateNodeRepresentation(node, pos, index); //newPrioNodeRep.GetComponent<TextHolder>().SetSurfaceText(node.NodeAlphaID, node.Dist); newPrioNodeRep.UpdateSurfaceText(UtilGraph.VISITED_COLOR); newPrioNodeRep.MoveNodeRepresentation(pos); nodeRepresentations.Insert(index, newPrioNodeRep); // Change color of the node representation we are looking for if (node.IsEndNode) { newPrioNodeRep.CurrentColor = UtilGraph.SHORTEST_PATH_COLOR; } // Enable gravity again and update indexes for (int i = 0; i < nodeRepresentations.Count; i++) { NodeRepresentation nodeRep = nodeRepresentations[i]; nodeRep.SetGravity(true); nodeRep.ListIndex = i; } }
// Updates value and position of one node representation and other involved node reps. public IEnumerator UpdateValueAndPositionOf(Node node, int index, bool increment) { graphMain.UpdateCheckList(UtilGraph.LIST_VISUAL, false); Color prevColor = node.CurrentColor; node.CurrentColor = UtilGraph.DIST_UPDATE_COLOR; // Node representation we want to move NodeRepresentation mainNodeRep = FindNodeRepresentation(node); // Update surface text mainNodeRep.UpdateSurfaceText(UtilGraph.DIST_UPDATE_COLOR); // Moving from index int mainNodeRepIndex = mainNodeRep.ListIndex; //Debug.Log("Updating value of node '" + node.NodeAlphaID + "'. Current index=" + currentNodeRepIndex + " == " + nodeRepresentations.IndexOf(nodeRep) + " ???"); // Check if index changed if (mainNodeRepIndex - index != 0) { // Disable gravity of swapping node reps SetGravityForMultipleNodeReps(0, index, false); // Move node left and down Vector3 moveLeft = mainNodeRep.transform.position + new Vector3(-1f, 0f, 0f); mainNodeRep.MoveNodeRepresentation(moveLeft); yield return(smallDuration); if (increment) { int moveDownYpos = mainNodeRepIndex - index; Vector3 moveDown = mainNodeRep.transform.position + new Vector3(-1f, moveDownYpos, 0f); mainNodeRep.MoveNodeRepresentation(moveDown); yield return(mediumDuration); // Move all the other involved up yield return(MoveOtherNodes(mainNodeRepIndex, index, true)); } else { // Backward step (Move node representation up) int moveUpYpos = index - mainNodeRepIndex; Vector3 moveUp = mainNodeRep.transform.position + new Vector3(-1f, moveUpYpos, 0f); mainNodeRep.MoveNodeRepresentation(moveUp); yield return(mediumDuration); // Move all the other involved down yield return(MoveOtherNodes(mainNodeRepIndex, index, false)); } // Move back into list Vector3 backInTheList = new Vector3(spawnPointList.position.x, mainNodeRep.transform.position.y, mainNodeRep.transform.position.z); mainNodeRep.MoveNodeRepresentation(backInTheList); //nodeRep.UpdateIndexPosition(index); mainNodeRep.ListIndex = index; nodeRepresentations[index] = mainNodeRep; yield return(mainNodeRep.HighlightNodeRepresentation(UtilGraph.DIST_UPDATE_COLOR, seconds)); // 1f); // Enable gravity again SetGravityForMultipleNodeReps(0, index, true); } node.CurrentColor = prevColor; graphMain.UpdateCheckList(UtilGraph.LIST_VISUAL, true); graphMain.WaitForSupportToComplete--; }