//Regular Unity Update method //Responsable for counting down and calling Step //Also responsable for gathering users input public void Update() { //if (!running) checkToStart(); if (mGameIsOver || mCurrentTetrimino == null) { return; } mTimer += Time.deltaTime; time += Time.deltaTime; int sec = (int)(time % 60); int min = (int)((time / 60) % 60); int hour = (int)((time / 60) / 60); timerDisplay.text = $"Time: {hour}:{min}:{sec}"; if (!pv.IsMine) { mTimer = mTimer / PhotonNetwork.PlayerList.Length; } if (mTimer > timeToStep && mPlayfield != null) { mTimer = 0; mPlayfield.Step(); } if (!pv.IsMine) { return; } /* * Originally the code just did a bunch of if checks and the piece operations were isolated here * I split it up into individual methods so they can be called from external sources if need be and so it is not running * a multi-line if check */ if (!gameIsPaused) { //Rotate Right if (Input.GetKeyDown(mGameSettings.rotateRightKey)) { rightRotate(); } //Rotate Left if (Input.GetKeyDown(mGameSettings.rotateLeftKey)) { leftRotate(); } //Move piece to the left if (Input.GetKeyDown(mGameSettings.moveLeftKey)) { moveLeft(); } //Move piece to the right if (Input.GetKeyDown(mGameSettings.moveRightKey)) { moveRight(); } //Make the piece fall faster //this is the only input with GetKey instead of GetKeyDown, because most of the time, users want to keep this button pressed and make the piece fall if (Input.GetKeyDown(mGameSettings.moveDownKey)) { moveDown(); } if (Input.GetKeyDown(mGameSettings.discardPieceKey)) { discardPiece(); } } if (Input.GetKeyDown(mGameSettings.pauseKey) && PhotonNetwork.CurrentRoom.MaxPlayers == 1) { pauseGame(); } //This part is responsable for rendering the preview piece in the right position if (mRefreshPreview) { var y = mCurrentTetrimino.currentPosition.y; while (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { y++; } mPreview.ForcePosition(mCurrentTetrimino.currentPosition.x, y - 1); mRefreshPreview = false; } }
//Regular Unity Update method //Responsable for counting down and calling Step //Also responsable for gathering users input public void Update() { if (mGameIsOver) { return; } mTimer += Time.deltaTime; if (mTimer > timeToStep) { mTimer = 0; mPlayfield.Step(); } if (mCurrentTetrimino == null) { return; } gyro = j.GetGyro(); orientation = j.GetVector(); if (gyro.x <= -12 && !_isMoving) { Debug.Log(gyro.x); StartCoroutine(TurnLeft()); } if (gyro.x >= 12 && !_isMoving) { Debug.Log(gyro.x); StartCoroutine(TurnRight()); } if (gyro.z <= -5 && !_isMoving) { Debug.Log(gyro.z); StartCoroutine(MoveLeft()); } if (gyro.z >= 5 && !_isMoving) { Debug.Log(gyro.z); StartCoroutine(MoveRight()); } //Rotate Right if (j.GetButtonDown(Joycon.Button.SR)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.NextRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.NextRotation; mRefreshPreview = true; } } //Rotate Left if (j.GetButtonDown(Joycon.Button.SL)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.PreviousRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.PreviousRotation; mRefreshPreview = true; } } //Move piece to the left if (j.GetButtonDown(Joycon.Button.DPAD_DOWN)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x - 1, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x - 1, mCurrentTetrimino.currentPosition.y); mRefreshPreview = true; } } //Move piece to the right if (j.GetButtonDown(Joycon.Button.DPAD_UP)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x + 1, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x + 1, mCurrentTetrimino.currentPosition.y); mRefreshPreview = true; } } //Make the piece fall faster //this is the only input with GetKey instead of GetKeyDown, because most of the time, users want to keep this button pressed and make the piece fall if (j.GetButton(Joycon.Button.DPAD_RIGHT) || (orientation.w < 0.2 && !_isMoving)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y + 1, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y + 1); } } //This part is responsable for rendering the preview piece in the right position if (mRefreshPreview) { var y = mCurrentTetrimino.currentPosition.y; while (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { y++; } mPreview.ForcePosition(mCurrentTetrimino.currentPosition.x, y - 1); mRefreshPreview = false; } }
//Regular Unity Update method //Responsable for counting down and calling Step //Also responsable for gathering users input public void Update() { if (!pv.IsMine) { return; } mTimer += Time.deltaTime; if (mTimer > timeToStep) { mTimer = 0; mPlayfield.Step(); } if (mCurrentTetrimino == null) { return; } //Rotate Right if (Input.GetKeyDown(mGameSettings.rotateRightKey)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.NextRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.NextRotation; mRefreshPreview = true; } } //Rotate Left if (Input.GetKeyDown(mGameSettings.rotateLeftKey)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.PreviousRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.PreviousRotation; mRefreshPreview = true; } } //Move piece to the left if (Input.GetKeyDown(mGameSettings.moveLeftKey)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x - 1, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x - 1, mCurrentTetrimino.currentPosition.y); mRefreshPreview = true; } } //Move piece to the right if (Input.GetKeyDown(mGameSettings.moveRightKey)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x + 1, mCurrentTetrimino.currentPosition.y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x + 1, mCurrentTetrimino.currentPosition.y); mRefreshPreview = true; } } //Make the piece fall faster //this is the only input with GetKey instead of GetKeyDown, because most of the time, users want to keep this button pressed and make the piece fall if (Input.GetKey(mGameSettings.moveDownKey)) { if (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y + 1, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(mCurrentTetrimino.currentPosition.x, mCurrentTetrimino.currentPosition.y + 1); } } //This part is responsable for rendering the preview piece in the right position if (mRefreshPreview) { var y = mCurrentTetrimino.currentPosition.y; while (mPlayfield.IsPossibleMovement(mCurrentTetrimino.currentPosition.x, y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { y++; } mPreview.ForcePosition(mCurrentTetrimino.currentPosition.x, y - 1); mRefreshPreview = false; } }
//Regular Unity Update method //Responsable for counting down and calling Step //Also responsable for gathering users input public void Update() { if (mGameIsOver) { return; } mTimer += Time.deltaTime; if (mTimer > timeToStep) { mTimer = 0; mPlayfield.Step(); } if (mCurrentTetrimino == null) { return; } int x = mCurrentTetrimino.currentPosition.x; int y = mCurrentTetrimino.currentPosition.y; // Rotates Right if (Input.GetKeyDown(mGameSettings.rotateRightKey)) { Debug.Log(mTimer); music.Play("flip_shape"); if (mPlayfield.IsPossibleMovement(x, y, mCurrentTetrimino, mCurrentTetrimino.NextRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.NextRotation; mRefreshPreview = true; } } // Rotates Left if (Input.GetKeyDown(mGameSettings.rotateLeftKey)) { music.Play("flip_shape"); if (mPlayfield.IsPossibleMovement(x, y, mCurrentTetrimino, mCurrentTetrimino.PreviousRotation)) { mCurrentTetrimino.currentRotation = mCurrentTetrimino.PreviousRotation; mRefreshPreview = true; } } // Moves piece to the left if (Input.GetKeyDown(mGameSettings.moveLeftKey)) { music.Play("move_shape_left"); if (mPlayfield.IsPossibleMovement(x - 1, y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(x - 1, y); mRefreshPreview = true; } } // Moves piece to the right if (Input.GetKeyDown(mGameSettings.moveRightKey)) { music.Play("move_shape_right"); if (mPlayfield.IsPossibleMovement(x + 1, y, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(x + 1, y); mRefreshPreview = true; } } // Accelerates fall. // Using GetKey instead of GetKeyDown, because most of the time, users want to keep this button pressed and make the piece fall if (Input.GetKey(mGameSettings.moveDownKey)) { if (mPlayfield.IsPossibleMovement(x, y + 1, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { mCurrentTetrimino.currentPosition = new Vector2Int(x, y + 1); } } // Renders preview tetris piece. if (mRefreshPreview) { int tempX = mCurrentTetrimino.currentPosition.x; int tempY = mCurrentTetrimino.currentPosition.y; while (mPlayfield.IsPossibleMovement(tempX, tempY, mCurrentTetrimino, mCurrentTetrimino.currentRotation)) { tempY++; } mPreview.ForcePosition(tempX, tempY - 1); mRefreshPreview = false; } }