brackeys-12/camera.gd

47 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-09-17 20:37:19 +00:00
extends Camera2D
# Variables to control zoom limits and speed
var zoom_min = 0.5
var zoom_max = 2.0
var zoom_speed = 0.1
# Variables for panning
var is_panning = false
var last_mouse_position = Vector2()
2024-09-23 23:59:35 +00:00
var pan_speed = 1000
@onready var camera_target = position
func _process(delta: float) -> void:
position = position.move_toward(camera_target, delta * pan_speed)
2024-09-17 20:37:19 +00:00
func _input(event):
if event is InputEventMouseButton:
# Zoom in (scroll up)
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
zoom -= Vector2(zoom_speed, zoom_speed)
# Zoom out (scroll down)
elif event.button_index == MOUSE_BUTTON_WHEEL_UP:
zoom += Vector2(zoom_speed, zoom_speed)
# Clamp the zoom value to stay within zoom_min and zoom_max
zoom.x = clamp(zoom.x, zoom_min, zoom_max)
zoom.y = clamp(zoom.y, zoom_min, zoom_max)
# Handle panning with right click
if event.button_index == MOUSE_BUTTON_RIGHT:
if event.pressed:
is_panning = true
2024-09-23 23:59:35 +00:00
last_mouse_position = Cursor.mouse_control.position
2024-09-17 20:37:19 +00:00
else:
is_panning = false
# Handle panning movement
if is_panning and event is InputEventMouseMotion:
2024-09-23 23:59:35 +00:00
var mouse_movement = last_mouse_position - Cursor.mouse_control.position
camera_target += mouse_movement / zoom.x
#global_position += mouse_movement * zoom.x # Adjust movement based on zoom level
last_mouse_position = Cursor.mouse_control.position