コード例 #1
0
ファイル: GameLogic.cs プロジェクト: mod6991/UnityTetris
    // 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();
    }