//creates grid and populates it with random tokens void MakeGrid() { grid = new GameObject("TokenGrid"); for (int x = 0; x < gridWidth; x++) { for (int y = 0; y < gridHeight; y++) { AddTokenToPosInGrid(x, y, grid); } } matchManager.GridHasMatch(); }
//Repopulate grid at the top public virtual void AddNewTokensToRepopulateGrid() { for (int x = 0; x < gameManager.gridWidth; x++) //looping through the grid { GameObject token = gameManager.gridArray[x, gameManager.gridHeight - 1]; //find token in position if (token == null) //if there's no token there, add a token { gameManager.AddTokenToPosInGrid(x, gameManager.gridHeight - 1, gameManager.grid); } } //Check if the grid has a match when repopulate it. while (matchManager.GridHasMatch()) { } ; }
// This function is responsible for moving the tokens and resetting the variables if there was a match, and putting them back into their previous positions // if there was not a match. public virtual void ExchangeTokens() { // Remember the starting and ending position for each token. Vector3 startPos = gameManager.GetWorldPositionFromGridPosition((int)exchangeGridPos1.x, (int)exchangeGridPos1.y); Vector3 endPos = gameManager.GetWorldPositionFromGridPosition((int)exchangeGridPos2.x, (int)exchangeGridPos2.y); // Get the new position for each token by lerping between startPos and endPos. Vector3 movePos1 = Vector3.Lerp(startPos, endPos, lerpPercent); Vector3 movePos2 = Vector3.Lerp(endPos, startPos, lerpPercent); // Update the position of each token object. exchangeToken1.transform.position = movePos1; exchangeToken2.transform.position = movePos2; // If both tokens have reached their final position: if (lerpPercent == 1) { // Tell the grid array the new positions of each token. gameManager.gridArray[(int)exchangeGridPos2.x, (int)exchangeGridPos2.y] = exchangeToken1; gameManager.gridArray[(int)exchangeGridPos1.x, (int)exchangeGridPos1.y] = exchangeToken2; Step--; StepText.gameObject.GetComponent <TextMesh> ().text = "Step: " + Step; // If this exchange has not created a match, move the tokens back. if (!matchManager.GridHasMatch() && userSwap) { SetupTokenExchange(exchangeToken1, exchangeGridPos2, exchangeToken2, exchangeGridPos1, false); // If this exchange has created a match, reset all variables and set movement to false. } else { exchangeToken1 = null; exchangeToken2 = null; move = false; } } }