Example #1
0
    //Loop through the crossword data and create the necessary tiles
    void CreateGrid(int[] scoreMults)
    {
        float size      = GetTileSize();
        float tileScale = size / tilePrefab.GetComponent <RectTransform>().rect.width;

        TileLogic[] tiles = new TileLogic[crossword.size.rows * crossword.size.cols];
        int         index = 0;

        int[] lastColIndices = new int[crossword.size.cols];
        for (int k = 0; k < lastColIndices.Length; k++)
        {
            lastColIndices[k] = -1;
        }

        for (int row = 0; row < crossword.size.rows; row++)
        {
            int lastNumberedAcrossClueIndex = -1;
            for (int col = 0; col < crossword.size.cols; col++)
            {
                index = IndexForRowAndCol(row, col);

                bool isBlank = IsGridSpotBlank(row, col);
                if (isBlank)
                {
                    lastNumberedAcrossClueIndex = -1;
                    lastColIndices[col]         = -1;
                    tiles[index] = null;
                    continue;
                }

                tiles[index] = CreateTile(tileScale, row, col, size);

                TileLogic tileLogic = tiles[index];
                if (index < scoreMults.Length)
                {
                    tileLogic.SetScoreMult(scoreMults[index]); // set the randomly generated score multiplier for the tile
                }
                if (tileLogic.acrossAnswer != null)            // if the clue has an across answer, set it as the most recent
                {
                    lastNumberedAcrossClueIndex = index;
                }
                else if (lastNumberedAcrossClueIndex > -1)// if the clue does not have an across answer, set it's parent across answer to be the most recent across
                {
                    tileLogic.parentAcrossIndex = lastNumberedAcrossClueIndex;
                }

                //now do the same for the down answers
                if (tileLogic.downAnswer != null)
                {
                    lastColIndices[col] = index;
                }
                else if (lastColIndices[col] > -1)
                {
                    tileLogic.parentDownIndex = lastColIndices[col];
                }
            }
        }

        GameManager.instance.SetTileArray(tiles); // store the tile array in the game manager
    }
    //Create a tile from the prefab and set the size and properties
    TileLogic CreateTile(float tileScale, int row, int col, float tileSize, int[] scoreMults)
    {
        GameObject tile = Instantiate(tilePrefab, gridParent.transform, false);

        TileLogic tileLogic = tile.GetComponent <TileLogic>();

        if (tileLogic == null)
        {
            return(null);
        }

        int index = IndexForRowAndCol(row, col);

        if (index < scoreMults.Length)
        {
            tileLogic.SetScoreMult(scoreMults[index]); // set the randomly generated score multiplier for the tile
        }
        tileLogic.ScaleTile(tileScale, tileSize, true);

        float a = row - (int)(crossword.size.rows / 2) + ((crossword.size.rows % 2 + 1) % 2) * .5f; //set the position of the tile in the grid based on the row and col
        float b = col - (int)(crossword.size.cols / 2) + ((crossword.size.cols % 2 + 1) % 2) * .5f;

        tile.transform.localPosition = new Vector2(b * tileSize, -(a * tileSize));


        string downAnswer   = GetClueOrAnswer(false, false, index);
        string downClue     = GetClueOrAnswer(true, false, index);
        string acrossAnswer = GetClueOrAnswer(false, true, index);
        string acrossClue   = GetClueOrAnswer(true, true, index);

        tileLogic.SetupTile(crossword.grid[index], crossword.gridnums[index], acrossClue, acrossAnswer, downClue, downAnswer, index);

        return(tileLogic);
    }