Exemplo n.º 1
0
        /// <summary>
        /// The main function to add tiles.
        /// </summary>
        private void AddTile()
        {
            while (true) // Run this as long as there are no tiles added
            {
                if (firstTile == null)
                {
                    firstTile = Instantiate(possibleTiles[Random.Range(0, possibleTiles.Count)]); // if no first tile, create one and exit the function
                }
                else
                {
                    var newTile  = Instantiate(possibleTiles[Random.Range(0, possibleTiles.Count)]); // Create new Tile in the level scene
                    var position = firstTile.transform.position;

                    var index = Random.Range(0, 5);
                    switch (index) //index is here to define the side to be appended
                    {
                    case 0:        //Up side
                    {
                        SetNewTilePosition(newTile, position + firstTile.freePositionsUp[Random.Range(0, firstTile.freePositionsUp.Count)], index);

                        if (!firstTile.CheckNewTile(newTile))     //checking if the new tile is okay or not if false try with another new tile
                        {
                            DestroyImmediate(newTile.gameObject, true);
                            continue;
                        }
                        break;
                    }

                    case 1:     //Down side
                        SetNewTilePosition(newTile, position + firstTile.freePositionsDown[Random.Range(0, firstTile.freePositionsDown.Count)], index);

                        if (!firstTile.CheckNewTile(newTile))     //checking if the new tile is okay or not if false try with another new tile
                        {
                            DestroyImmediate(newTile.gameObject, true);
                            continue;
                        }
                        break;

                    case 2:     //Left side
                        SetNewTilePosition(newTile, position + firstTile.freePositionsLeft[Random.Range(0, firstTile.freePositionsLeft.Count)], index);

                        if (!firstTile.CheckNewTile(newTile))     //checking if the new tile is okay or not if false try with another new tile
                        {
                            DestroyImmediate(newTile.gameObject, true);
                            continue;
                        }
                        break;

                    default:     //Right side
                        SetNewTilePosition(newTile, position + firstTile.freePositionsRight[Random.Range(0, firstTile.freePositionsRight.Count)], index);

                        if (!firstTile.CheckNewTile(newTile))     //checking if the new tile is okay or not if false try with another new tile
                        {
                            DestroyImmediate(newTile.gameObject, true);
                            continue;
                        }
                        break;
                    }
                }

                break;
            }
        }