Basically you want the camera to follow the mouse cursor while also making sure to keep the player on screen?
If we look at this line segment a-b, where (a) is the player and (b) is the cursor
--------a--------------------------------------------------------------b--------
< x----------------------Camera bounds----------------------------x>
As you can see, if we move the camera towards the right, point b(cursor),
Point a(player) will be off screen.
-----------------------------------------------------------b--------
< x----------------------Camera bounds----------------------------x>
Which means, to make sure that both point a and b are in the camera screen, we have to point the camera direction towards somewhere in the middle of point (a) and (b).
--------a-----------------------------x-------------------------------b--------
< x----------------------Camera bounds----------------------------x>
which can be expressed with the following code:
Camera2D.gd:
var interpolate_val = 1
func _physics_process(delta);
var target = player.global_position
var mid_x = (target.x + get_global_mouse_position.x) / 2
var mid_y = (target.y + get_global_mouse_position.y) / 2
global_position = global_position.linear_interpolate(Vector2(mid_x,mid_y), interpolate_val * delta)
The interpolate_val variable determines how fast the camera moves towards the targeted location, in this case the middle location between the cursor and player.
Incase your wondering, how does the math work, assuming point a=3, and point b=6
a(3) + b(6) = 9/2 = 4.5
if we look at a plot of points: 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0
we can confirm that 4.5 is indeed the middle of the two points. The code should also work the same for negative values or combination of both:
a(-3) + (-6) = -9 / 2 = -4.5
a(-2) + b(2) = 0 /2 = 0
And since the camera is always directed between the middle of the two points, point a and point b will always be within camera view.