Exemplo n.º 1
0
        public List <__Tile> possibleTiles = new List <__Tile>();//TODO fill it with tiles in the tile folder automatically

#if UNITY_EDITOR
#if ODIN_INSPECTOR
        /// <summary>
        /// Function used to find the position of the new tile to be added to the level. Do not check every conditions here.
        /// </summary>
        /// <param name="newTile">The new <see cref="__Tile"/> to be added</param>
        /// <param name="position">the position to align with, from the first tile</param>
        /// <param name="index">the chosen index, use to find the side to be appended</param>
        private static void SetNewTilePosition(__Tile newTile, Vector3 position, int index)
        {
            Vector3 positionToAlign;

            switch (index)
            {
            case 0:
                positionToAlign = newTile.freePositionsDown[Random.Range(0, newTile.freePositionsDown.Count)];
                break;

            case 1:
                positionToAlign = newTile.freePositionsUp[Random.Range(0, newTile.freePositionsUp.Count)];
                break;

            case 2:
                positionToAlign = newTile.freePositionsRight[Random.Range(0, newTile.freePositionsRight.Count)];
                break;

            default:
                positionToAlign = newTile.freePositionsLeft[Random.Range(0, newTile.freePositionsLeft.Count)];
                break;
            }

            var transform1 = newTile.transform;

            transform1.position = position - (transform1.position + positionToAlign);
        }
Exemplo n.º 2
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;
            }
        }