Ejemplo n.º 1
0
 public MovingTetromino(TetrisGameBoard gameBoard)
 {
     m_gameBoard     = gameBoard;
     Rotation        = TetrominoRotation.None;
     Column          = 3; // starts in the middle
     Row             = 0;
     DescriptiveGrid = null;
 }
Ejemplo n.º 2
0
 public MovingTetromino(TetrisGameBoard gameBoard)
 {
     m_gameBoard = gameBoard;
     m_rotation = TetrominoRotation.None;
     Column = 3; // starts in the middle
     Row = 0;
     DescriptiveGrid = null;
 }
Ejemplo n.º 3
0
        public bool TryMoveRight()
        {
            TetrominoRotation rot = Rotation;

            BrickType[,] descriptiveGrid = GetRotatedDescriptiveGrid(rot);
            int column = Column + 1, row = Row;

            return(TryMovement(descriptiveGrid, column, row, rot));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
 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 });
     }
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        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;
            }
        }
Ejemplo n.º 9
0
 protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation)
 {
     return gridRot0;
 }
Ejemplo n.º 10
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;
 }
Ejemplo n.º 11
0
 protected abstract BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation);
Ejemplo n.º 12
0
 public abstract int[] GetRotatedLowerHorizon(TetrominoRotation rotation);
Ejemplo n.º 13
0
 public bool CanMatch(TetrominoRotation rotation = TetrominoRotation.None)
 {
     return m_gameBoard.DoesBrickFitToHorizon(m_tetromino.GetRotatedLowerHorizon(rotation));
 }
Ejemplo n.º 14
0
 public abstract void UpdateGrid(TetrominoRotation rotation);
Ejemplo n.º 15
0
 public bool CanMatch(TetrominoRotation rotation = TetrominoRotation.None)
 {
     return(m_gameBoard.DoesBrickFitToHorizon(m_tetromino.GetRotatedLowerHorizon(rotation)));
 }
Ejemplo n.º 16
0
 protected abstract BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation);
Ejemplo n.º 17
0
 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 };
     }
 }
Ejemplo n.º 18
0
 protected override BrickType[,] GetRotatedDescriptiveGrid(TetrominoRotation rotation)
 {
     return(gridRot0);
 }
Ejemplo n.º 19
0
 public abstract int[] GetRotatedLowerHorizon(TetrominoRotation rotation);
Ejemplo n.º 20
0
 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;
     }
 }
Ejemplo n.º 21
0
    // 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();
    }