public void Setup(GameManager.BoardState boardState) { // Reset the game tiles so they can be selected again Reset(); // Make sure the tile lists are cleared letterTiles.Clear(); selectedLetterTiles.Clear(); // Set the size of the current board currentBoardSize = boardState.wordBoardSize; // Get the maximum width and height a tile can be for this board without overflowing the container float maxTileWidth = ((letterTileContainer.transform as RectTransform).rect.width - (boardState.wordBoardSize - 1) * tileSpacing) / boardState.wordBoardSize; float maxTileHeight = ((letterTileContainer.transform as RectTransform).rect.height - (boardState.wordBoardSize - 1) * tileSpacing) / boardState.wordBoardSize; // The final tile size will be the minimum between the max width/height so that the tiles do not overflow out of the containers bounds currentTileSize = Mathf.Min(maxTileWidth, maxTileHeight); letterTileContainer.cellSize = new Vector2(currentTileSize, currentTileSize); letterTileContainer.spacing = new Vector2(tileSpacing, tileSpacing); letterTileContainer.constraint = GridLayoutGroup.Constraint.FixedColumnCount; letterTileContainer.constraintCount = boardState.wordBoardSize; // Place all the tiles on the board for (int i = 0; i < boardState.wordBoardSize; i++) { for (int j = 0; j < boardState.wordBoardSize; j++) { int tileIndex = i * boardState.wordBoardSize + j; // Create a GameObject that will go in the grid and be the parent for any LetterTile that needs to go in its place GameObject gridGameObject = new GameObject("grid_object", typeof(RectTransform)); gridGameObject.transform.SetParent(letterTileContainer.transform, false); gridGameObjects.Add(gridGameObject); switch (boardState.tileStates[tileIndex]) { case GameManager.BoardState.TileState.UsedButNotFound: LetterTile letterTile = GameManager.Instance.LetterTilePool.GetObject().GetComponent <LetterTile>(); letterTile.TileIndex = tileIndex; letterTile.Letter = boardState.tileLetters[tileIndex]; letterTile.LetterText.text = letterTile.Letter.ToString(); // Set it as a child of the gridGameObject we created before (so its in the correct position) letterTile.transform.SetParent(gridGameObject.transform, false); letterTile.transform.localPosition = Vector3.zero; letterTile.gameObject.SetActive(true); // Now we need to scale the LetterTile so its size is relative to currentTileSize. We can't just set the size of the RectTransform // because then the font on the Text component for the letter might be to big and the letter will disappear float scale = currentTileSize / (letterTile.transform as RectTransform).rect.width; letterTile.transform.localScale = new Vector3(scale, scale, 1f); letterTiles.Add(letterTile); break; default: // We add null so when we are selecting tiles we can easily determine what tiles are beside each other by checking the indexes in boardTiles. letterTiles.Add(null); break; } } } for (int i = 0; i < boardState.words.Length; i++) { currentWords.Add(boardState.words[i]); } }
public void Setup(GameManager.BoardState boardState) { Reset(); bool wordAddedToRow = false; float currentRowWidth = 0f; GameObject currentTileRow = CreateNewTileRow(); // Go through every word that is on the board, we need to add a tilePrefab for each letter in each word for (int i = 0; i < boardState.words.Length; i++) { // Get the word we are adding tiles for and the space those tiles will take up string word = boardState.words[i]; float wordWidth = word.Length * tileSize + (word.Length - 1) * spaceBetweenLetters; // If a word has already been added to the current row, then we need to account for the spacing between words if (wordAddedToRow) { wordWidth += spaceBetweenWords; } // Check if the adding the current word to the current row will make the row larger that the width of the overall container bool rowToLarge = (currentRowWidth + wordWidth > tileContainer.rect.width); // If the current row is now wider than the container then we need to add a new row if (rowToLarge) { // Check if we havent added a word to the current row yet, if we havent then that means the tiles for the single word are larger than the container // If this happens then we will add the word anyway but the tiles will be squised and when the word is revealed the tiles will overlap. // To prevent this from happeneing make sure the largest word in the game using the given tileSize does not cause this error to appear. If it does // then reduce the tileSize of reduce the max number of letters for a word if (!wordAddedToRow) { Debug.LogWarningFormat("The word \"{0}\" is to large to fit in the tileContainer using a size of {1}", word, tileSize); } else { // Create a new row and set the wordAddedToRow and currentRowWidth values back to default currentTileRow = CreateNewTileRow(); wordAddedToRow = false; currentRowWidth = 0f; } } // If we added a word to the row already then we need to add a space GameObject if (wordAddedToRow) { // Create the space GameObject GameObject wordSpaceObject = new GameObject("word_space"); // Add a LayoutElement to it and give it a preferred width equal to spaceBetweenWords LayoutElement le = wordSpaceObject.AddComponent <LayoutElement>(); le.preferredWidth = spaceBetweenWords; // Add the space GameObject to the row wordSpaceObject.transform.SetParent(currentTileRow.transform, false); } // Need to create a new list of GridTiles for the tiles we are about to Instantiate List <GridTile> gridTiles = new List <GridTile>(); // Add a new tile to the row for every letter in the word for (int j = 0; j < word.Length; j++) { // Get a new tile object, set its parent to the current row, and active it GameObject gridTileObject = tilePool.GetObject(); gridTileObject.transform.SetParent(currentTileRow.transform, false); gridTileObject.transform.localScale = Vector3.one; gridTileObject.gameObject.SetActive(true); // If the tile didnt have a LayoutElement on it, add one and set the preferred size if (gridTileObject.GetComponent <LayoutElement>() == null) { AddTileLayoutElement(gridTileObject); } // Create a new GridTile and set the references GridTile gridTile = new GridTile(); gridTile.gridTileObject = gridTileObject; gridTile.letter = word[j]; gridTiles.Add(gridTile); } // Add the list of GridTiles to the dictionary indexed by the word they are for allGridTiles.Add(word, gridTiles); currentWords.Add(word); wordAddedToRow = true; currentRowWidth += wordWidth; } // Display all the found words for (int i = 0; i < boardState.words.Length; i++) { if (boardState.foundWords[i]) { // Get the word we need to display then the list of GridTiles for that word string word = boardState.words[i]; List <GridTile> gridTiles = allGridTiles[word]; // Loop through each grid tile and display the letter for it for (int j = 0; j < gridTiles.Count; j++) { DisplayLetter(gridTiles[j]); } } } // Display all the letters that have been show as a hint for (int i = 0; i < boardState.hintLettersShown.Count; i++) { int[] indexes = boardState.hintLettersShown[i]; string word = currentWords[indexes[0]]; List <GridTile> gridTiles = allGridTiles[word]; DisplayLetter(gridTiles[indexes[1]]); } }