public MovingTetromino(TetrisGameBoard gameBoard) { m_gameBoard = gameBoard; Rotation = TetrominoRotation.None; Column = 3; // starts in the middle Row = 0; DescriptiveGrid = null; }
public MovingTetromino(TetrisGameBoard gameBoard) { m_gameBoard = gameBoard; m_rotation = TetrominoRotation.None; Column = 3; // starts in the middle Row = 0; DescriptiveGrid = null; }
public bool TryMoveRight() { TetrominoRotation rot = Rotation; BrickType[,] descriptiveGrid = GetRotatedDescriptiveGrid(rot); int column = Column + 1, row = Row; return(TryMovement(descriptiveGrid, column, row, rot)); }
public bool TryRotateRight() { TetrominoRotation rot = Rotation; rot = (TetrominoRotation)((int)(rot + 3) % 4); // 3 == -1 mod 4 BrickType[,] descriptiveGrid = GetRotatedDescriptiveGrid(rot); int column = Column, row = Row; return(TryMovement(descriptiveGrid, column, row, rot)); }
public override int[] GetRotatedLowerHorizon(TetrominoRotation rotation) { switch (rotation) { case TetrominoRotation.None: case TetrominoRotation.UpsideDown: case TetrominoRotation.Left: case TetrominoRotation.Right: default: return(new int[] { 0, 0 }); } }
/// <summary> /// tries to fit the provided tetromino (represented by its descriptiveGrid) into the game board /// </summary> /// <param name="descriptiveGrid"></param> /// <param name="column"></param> /// <param name="row"></param> /// <param name="rotation"></param> /// <returns>true if the provided tetromino could be fit into the game board</returns> protected bool TryMovement(BrickType[,] descriptiveGrid, int column, int row, TetrominoRotation rotation) { bool bottomHit = !m_gameBoard.FitTetrominoToGameBoard(descriptiveGrid, ref column, ref row); if (!bottomHit && !m_gameBoard.IsTetrominoInCollision(descriptiveGrid, column, row)) { Rotation = rotation; DescriptiveGrid = descriptiveGrid; Column = column; Row = row; return(true); } return(false); }
protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation) { switch (rotation) { case TetrominoRotation.None: case TetrominoRotation.UpsideDown: return(gridRot0); case TetrominoRotation.Left: case TetrominoRotation.Right: default: return(gridRot90); } }
public override void UpdateGrid(TetrominoRotation rotation) { switch (rotation) { case TetrominoRotation.Initial: Grid = new int[, ] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; break; case TetrominoRotation.Right: Grid = new int[, ] { { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } }; break; case TetrominoRotation.Twice: Grid = new int[, ] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 } }; break; case TetrominoRotation.Left: Grid = new int[, ] { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } }; break; } }
protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation) { return gridRot0; }
/// <summary> /// tries to fit the provided tetromino (represented by its descriptiveGrid) into the game board /// </summary> /// <param name="descriptiveGrid"></param> /// <param name="column"></param> /// <param name="row"></param> /// <param name="rotation"></param> /// <returns>true if the provided tetromino could be fit into the game board</returns> protected bool TryMovement(BrickType[,] descriptiveGrid, int column, int row, TetrominoRotation rotation) { bool bottomHit = !m_gameBoard.FitTetrominoToGameBoard(descriptiveGrid, ref column, ref row); if (!bottomHit && !m_gameBoard.IsTetrominoInCollision(descriptiveGrid, column, row)) { Rotation = rotation; DescriptiveGrid = descriptiveGrid; Column = column; Row = row; return true; } return false; }
protected abstract BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation);
public abstract int[] GetRotatedLowerHorizon(TetrominoRotation rotation);
public bool CanMatch(TetrominoRotation rotation = TetrominoRotation.None) { return m_gameBoard.DoesBrickFitToHorizon(m_tetromino.GetRotatedLowerHorizon(rotation)); }
public abstract void UpdateGrid(TetrominoRotation rotation);
public bool CanMatch(TetrominoRotation rotation = TetrominoRotation.None) { return(m_gameBoard.DoesBrickFitToHorizon(m_tetromino.GetRotatedLowerHorizon(rotation))); }
protected abstract BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation);
public override int[] GetRotatedLowerHorizon(TetrominoRotation rotation) { switch (rotation) { case TetrominoRotation.None: case TetrominoRotation.UpsideDown: return new int[] { 1, 0, 0 }; case TetrominoRotation.Left: case TetrominoRotation.Right: default: return new int[] { 0, 1 }; } }
protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation) { return(gridRot0); }
public abstract int[] GetRotatedLowerHorizon(TetrominoRotation rotation);
protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation) { switch (rotation) { case TetrominoRotation.None: case TetrominoRotation.UpsideDown: return gridRot0; case TetrominoRotation.Left: case TetrominoRotation.Right: default: return gridRot90; } }
// Update is called once per frame void Update() { // Update timers _timer += Time.deltaTime; _moveTimer += Time.deltaTime; // Backup tetromino location and rotation int x = _tetromino.X; int y = _tetromino.Y; TetrominoRotation rotation = _tetromino.Rotation; bool merge = false; // If Collision without action -> gameover if (_tetromino.Collision(_gameBoard)) { _gameover = true; } if (_gameover) { return; } // Left movement if (_moveTimer > _moveTime && Input.GetKey(KeyCode.LeftArrow)) { _tetromino.X--; _moveTimer = 0; } // Right movement if (_moveTimer > _moveTime && Input.GetKey(KeyCode.RightArrow)) { _tetromino.X++; _moveTimer = 0; } // Counter-clockwise rotation if (Input.GetKeyDown(KeyCode.Q)) { _tetromino.RotateLeft(_gameBoard); } // Clockwise rotation if (Input.GetKeyDown(KeyCode.W)) { _tetromino.RotateRight(_gameBoard); } // Tetromino fall if (_timer > _dropTime / _dropLevel || (_timer > _quickDropTime && Input.GetKey(KeyCode.DownArrow))) { _tetromino.Y++; _timer = 0; if (_tetromino.Collision(_gameBoard)) { merge = true; } } // If collision while moving or falling, revert the last movement if (_tetromino.Collision(_gameBoard)) { _tetromino.X = x; _tetromino.Y = y; _tetromino.Rotation = rotation; } // If tetromino collision because of the fall, merge it into the // gameboard and spawn a new one if (merge) { _gameBoard.MergeTetromino(_tetromino); _gameBoard.UpdateTilesPositions(); List <int> fullLines = _gameBoard.CheckFullLines(); if (fullLines.Count > 0) { _gameBoard.RemoveLines(fullLines); UpdateLines(fullLines.Count); } SpawnNewTetromino(); } // Update tetromino tiles on screen _tetromino.UpdateTilesPositions(); }