protected virtual void OnEnable() { if (_levelController == null) { // Initializes level controller _levelController = GetComponent <ILevelController>(); _levelController.AddClearedLines(0); } if (_boardController == null) { _boardController = GetComponent <IBoardController>(); _boardController.Initialize(); } _holdInputMaxTime = _levelController.GravityInterval / _boardController.BoardModel.NumColumns; }
public void Initialize() { if (!IsInitialized) { // Initialize board model and board view if (_boardFactory == null) { _boardFactory = GetComponent <IBoardFactory>(); } (BoardModel, BoardView) = _boardFactory.GetBoard(_blockPrefab, _blocks, _blockScale, _blocksParent, _maxNumLines, _maxNumColumns); // Initialized hold input max time if (_inputController == null) { _inputController = GetComponent <IInputController>(); } if (_levelController == null) { _levelController = GetComponent <ILevelController>(); _levelController.AddClearedLines(0); } if (_holdController == null) { _holdController = GetComponent <IHoldController>(); } if (_soundController == null) { _soundController = GetComponent <ISoundController>(); } _pausePanel.SetActive(IsPaused); IsInitialized = true; } }
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))); }