Esempio n. 1
0
        private IEnumerator MoveTetromino()
        {
            bool isTetrominoLocked = false;
            bool activateDropHard  = false;
            bool useHoldPiece      = false;

            while (!isTetrominoLocked && !useHoldPiece) // while current tetromino is not locked or while player has not activated hold piece
            {
                // if drop hard was activated, we skip tetromino control
                if (!activateDropHard)
                {
                    // Control tetromino
                    yield return(StartCoroutine(ControlTetromino()));
                }

                // Check if player has clicked the drop hard button while controlling the tetromino
                if (_inputController.DropHard)
                {
                    activateDropHard = true;
                }

                ClearTetromino(_ghostTetromino);
                ClearTetromino(_currentTetromino);

                if (_inputController.HoldPiece && !_isHoldPiece)
                {
                    useHoldPiece = true;
                }
                else
                {
                    // Apply gravity
                    _currentTetromino.CurrentLine += 1;

                    if (!CanPlaceTetromino(_currentTetromino))
                    {
                        // rollback and lock the tetromino
                        _currentTetromino.CurrentLine -= 1;
                        isTetrominoLocked              = true;
                    }

                    UpdateGhostTetromino(_currentTetromino, _ghostTetromino);
                    DrawGhostTetromino(_ghostTetromino);
                    DrawTetromino(_currentTetromino);
                }
            }

            if (!useHoldPiece)
            {
                List <int> clearedLines = GetClearedLines();
                if (clearedLines.Count > 0)
                {
                    yield return(StartCoroutine(AnimateClearedLines(clearedLines)));

                    RemoveClearedLines(clearedLines);
                    _levelController.AddClearedLines(clearedLines.Count);
                }
                else
                {
                    _soundController.PlayPlaceTetromino();

                    // Add a frame to refresh input
                    yield return(null);
                }

                if (IsPlayerInDanger())
                {
                    _soundController.SetFastBgMusicPitch();
                }
                else
                {
                    _soundController.SetDefaultBgMusicPitch();
                }
            }

            yield return(StartCoroutine(SpawnTetromino(useHoldPiece)));
        }