Exemple #1
0
    private void Update()
    {
        switch (_state)
        {
        case State.Free: {
            if (_inputHandler.Left() || _inputHandler.Right() || _inputHandler.Down())
            {
                _state = State.Selected;
            }

            if (_inputHandler.Action())
            {
                Rotate();
            }

            break;
        }

        case State.Selected: {
            if (_inputHandler.HoldingLeft())
            {
                _position = new Vector3(_position.x - BLOCK_SIZE, transform.position.y);
                _state    = State.MovingLeft;
                _rigidbody2D.MovePosition(new Vector2(_position.x, transform.position.y));
                break;
            }

            if (_inputHandler.HoldingRight())
            {
                _position = new Vector3(_position.x + BLOCK_SIZE, transform.position.y);
                _state    = State.MovingRight;
                _rigidbody2D.MovePosition(new Vector2(_position.x, transform.position.y));
                break;
            }

            if (_inputHandler.ReleaseLeft() || _inputHandler.ReleaseRight())
            {
                ResetBlockState();
            }

            if (_inputHandler.HoldingDown())
            {
                _position = new Vector3(_position.x, transform.position.y - BLOCK_SIZE);
                _state    = State.MovingDown;
                _rigidbody2D.MovePosition(new Vector2(transform.position.x, _position.y));
                break;
            }

            if (_inputHandler.Action())
            {
                Rotate();
            }

            break;
        }

        case State.MovingLeft:
            _currentHoldingTime += Time.deltaTime * _speed;
            if (_currentHoldingTime >= _currentSpeed)
            {
                _position           = new Vector3(_position.x - BLOCK_SIZE, transform.position.y);
                _currentSpeed      -= 0.2f;
                _currentHoldingTime = 0;
                _rigidbody2D.MovePosition(new Vector2(_position.x, transform.position.y));
            }

            if (_inputHandler.ReleaseLeft() || _inputHandler.ReleaseRight())
            {
                ResetBlockState();
            }

            break;

        case State.MovingRight:
            _currentHoldingTime += Time.deltaTime * _speed;
            if (_currentHoldingTime >= _currentSpeed)
            {
                _position           = new Vector3(_position.x + BLOCK_SIZE, transform.position.y);
                _currentSpeed      -= 0.2f;
                _currentHoldingTime = 0;
                _rigidbody2D.MovePosition(new Vector2(_position.x, transform.position.y));
            }

            if (_inputHandler.ReleaseLeft() || _inputHandler.ReleaseRight())
            {
                ResetBlockState();
            }
            break;

        case State.MovingDown:
            _currentHoldingTime += Time.deltaTime * _speed;
            if (_currentHoldingTime >= _currentSpeed)
            {
                _position           = new Vector3(_position.x, transform.position.y - BLOCK_SIZE);
                _currentSpeed      -= 0.2f;
                _currentHoldingTime = 0;
                _rigidbody2D.MovePosition(new Vector2(transform.position.x, _position.y));
            }

            if (_inputHandler.ReleaseLeft() || _inputHandler.ReleaseRight() || _inputHandler.ReleaseDown())
            {
                ResetBlockState();
            }
            break;

        default:
            throw new ArgumentOutOfRangeException();
        }
    }