// This is called when we release a click from connecting dots. public IEnumerator DotsScored() { // If we have not connected enough dots to score, we simply clear the list of connected dots. if (ConnectedDots.Count < 2) { ConnectedDots.Clear(); } else { // If we have connected enough dots, we loop through each connected dot... int clearedDotCounter = 0; while (clearedDotCounter <= ConnectedDots.Count) { foreach (Transform ClearedDots in ConnectedDots) { // ...and move them to the dot scored zone (which is offscreen). ClearedDots.SetParent(DotScoredZone); ClearedDots.position = DotScoredZone.position; foreach (Transform RemainingDots in GameBoardParent) { // We then change the coordinate values of both the scored dots and remaining dots that are above them which would be affected by "gravity". if (RemainingDots.GetComponent <IndividualDot>().Coordinates.x == ClearedDots.GetComponent <IndividualDot>().Coordinates.x&& RemainingDots.GetComponent <IndividualDot>().Coordinates.y > ClearedDots.GetComponent <IndividualDot>().Coordinates.y) { RemainingDots.GetComponent <IndividualDot>().Coordinates.y--; ClearedDots.GetComponent <IndividualDot>().Coordinates.y++; } } } clearedDotCounter++; yield return(null); } // Have the remaining dots fall into place if their position does not match their coordinates. foreach (Transform RemainingDots in GameBoardParent) { if (RemainingDots.transform.position != new Vector3(RemainingDots.GetComponent <IndividualDot>().Coordinates.x, RemainingDots.GetComponent <IndividualDot>().Coordinates.y, 0)) { StartCoroutine(RemainingDots.GetComponent <IndividualDot>().FallIntoPlace()); } } RepopulateDots(); } }
// We then add the cleared dots back to the grid. private void RepopulateDots() { // For all of the connected dots, we add them back to the game board and set their position above the gameboard. foreach (Transform ClearedDots in ConnectedDots) { ClearedDots.SetParent(GameBoardParent); ClearedDots.position = new Vector3(ClearedDots.GetComponent <IndividualDot>().Coordinates.x, rows * 2, 0); // We then assign these dots a new color. AssignDots(ClearedDots, (int)ClearedDots.GetComponent <IndividualDot>().Coordinates.y); } // Clear the list of connected dots. ConnectedDots.Clear(); StartCoroutine(ReorganizeHierarchy()); }