コード例 #1
0
    void MovementLogic(float delta)
    {
        Vector3 MovementDir = new Vector3();
        Basis   AimBasis    = CameraNode.GlobalTransform.basis;

        // Forward
        if (Input.IsKeyPressed(GD.KEY_W))
        {
            MovementDir -= AimBasis.get_axis(2);
        }

        // Backward
        if (Input.IsKeyPressed(GD.KEY_S))
        {
            MovementDir += AimBasis.get_axis(2);
        }

        // Left
        if (Input.IsKeyPressed(GD.KEY_A))
        {
            MovementDir -= AimBasis.get_axis(0);
        }

        // Right
        if (Input.IsKeyPressed(GD.KEY_D))
        {
            MovementDir += AimBasis.get_axis(0);
        }

        // Normalize the direction
        MovementDir.y = 0.0f;
        MovementDir   = MovementDir.normalized();

        // Motion
        float   MoveSpeed  = 4.0f;
        Vector3 MoveMotion = MovementDir * MoveSpeed;

        MoveMotion.y = LinearVelocity.y;
        MoveMotion   = LinearVelocity.linear_interpolate(MoveMotion, MoveAcceleration * delta);

        // Is colliding with floor
        bool OnFloor = FloorRay.IsColliding();

        // Jump check
        if (Input.IsKeyPressed(GD.KEY_SPACE) && !IsJumpJustPressed)
        {
            if (OnFloor)
            {
                // Jump!
                MoveMotion.y = JumpForce;
                ViewModel.AddForce(new Vector2(0.0f, -0.006f));
            }

            IsJumpJustPressed = true;
        }

        // Reset key
        if (IsJumpJustPressed && !Input.IsKeyPressed(GD.KEY_SPACE))
        {
            IsJumpJustPressed = false;
        }


        // Set the velocity
        LinearVelocity = MoveMotion;

        // Check whether the player is moving
        IsMoving = (Mathf.abs(LinearVelocity.x) >= 0.2f || Mathf.abs(LinearVelocity.z) >= 0.2f);
    }