Exemplo n.º 1
0
    void DrawTetrimino()
    {
        ClearTetrimino();
        Tetrimino temp_tetrimino = new Tetrimino(tetriminos[current_tetrimino - 1]);
        //rotate tetrimino
        int direction_count = 0;

        while (direction_count != direction)
        {
            if (direction_count < direction)
            {
                temp_tetrimino.Rotate(true);
                direction_count++;
            }
            else if (direction_count > direction)
            {
                temp_tetrimino.Rotate(false);
                direction_count--;
            }
        }
        //determine the actual bounds of the tetrimino
        int top_bound    = 0;
        int bottom_bound = 0;
        int left_bound   = 0;
        int right_bound  = 0;

        //loop to find the top bound
        for (int y = 0; y < temp_tetrimino.GetData().GetLength(0); y++)
        {
            bool valid = true;
            for (int x = 0; x < temp_tetrimino.GetData().GetLength(1); x++)
            {
                if (temp_tetrimino.GetData()[y, x] == 0)
                {
                    if (x == temp_tetrimino.GetData().GetLength(1) - 1)
                    {
                        break;
                    }
                    continue;
                }
                else
                {
                    valid     = false;
                    top_bound = y;
                    break;
                }
            }
            if (!valid)
            {
                break;
            }
        }

        //loop to find the bottom bound
        for (int y = temp_tetrimino.GetData().GetLength(1) - 1; y > -1; y--)
        {
            bool valid = true;
            for (int x = 0; x < temp_tetrimino.GetData().GetLength(1); x++)
            {
                if (temp_tetrimino.GetData()[y, x] == 0)
                {
                    if (x == temp_tetrimino.GetData().GetLength(1) - 1)
                    {
                        break;
                    }
                    continue;
                }
                else
                {
                    valid        = false;
                    bottom_bound = y;
                    break;
                }
            }
            if (!valid)
            {
                break;
            }
        }

        //loop to find the left bound
        for (int x = 0; x < temp_tetrimino.GetData().GetLength(1); x++)
        {
            bool valid = true;
            for (int y = 0; y < temp_tetrimino.GetData().GetLength(0); y++)
            {
                if (temp_tetrimino.GetData()[y, x] == 0)
                {
                    if (y == temp_tetrimino.GetData().GetLength(0) - 1)
                    {
                        break;
                    }
                    continue;
                }
                else
                {
                    valid      = false;
                    left_bound = x;
                    break;
                }
            }
            if (!valid)
            {
                break;
            }
        }

        //loop to find the right bound
        for (int x = temp_tetrimino.GetData().GetLength(1) - 1; x > -1; x--)
        {
            bool valid = true;
            for (int y = 0; y < temp_tetrimino.GetData().GetLength(0); y++)
            {
                if (temp_tetrimino.GetData()[y, x] == 0)
                {
                    if (y == temp_tetrimino.GetData().GetLength(0) - 1)
                    {
                        break;
                    }
                    continue;
                }
                else
                {
                    valid       = false;
                    right_bound = x;
                    break;
                }
            }
            if (!valid)
            {
                break;
            }
        }

        //set the true x of the block position
        block_position = new Vector2Int(Mathf.Clamp(block_position.x, 0, board_data.GetLength(1) - 1 - (right_bound - left_bound)), Mathf.Clamp(block_position.y, 0 + bottom_bound, board_data.GetLength(0) - 1));

        //print the block at the correct position
        for (int y = top_bound; y < bottom_bound + 1; y++)
        {
            for (int x = left_bound; x < right_bound + 1; x++)
            {
                if (temp_tetrimino.GetData()[y, x] == 0)
                {
                    continue;
                }
                Vector2Int tile_pos = Array2Tile(new Vector2Int(x - left_bound + block_position.x, y - bottom_bound + block_position.y));
                board.SetTile(new Vector3Int(tile_pos.x, tile_pos.y, 0), tetrimino_tiles[temp_tetrimino.GetData()[y, x]]);
                tetrimino_positions.Add(tile_pos);
            }
        }
    }