Files
godot_number_factory/main_game_camera.gd
T
bionickatana 2d2485910f
Build Godot Project (Linux) / build-linux (push) Successful in 30s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
mend
testing actions

Godot auto build

Godot auto build

Godot auto build

Godot auto build

Godot auto build

Finished adding items into the game along with auto build for linux.
2026-06-22 13:32:18 -06:00

73 lines
2.5 KiB
GDScript

extends Camera2D
# --- Movement Settings ---
@export var KEYBOARD_SPEED: float = 500.0 # Pixels per second using keys
@export var EDGE_SPEED: float = 600.0 # Pixels per second using mouse edge
@export var EDGE_MARGIN: float = 20.0 # How close to the screen edge (in pixels) to trigger scroll
# --- Zoom Settings ---
@export var MIN_ZOOM: float = 0.5 # Furthest zoomed out
@export var MAX_ZOOM: float = 3.0 # Closest zoomed in
@export var ZOOM_SPEED: float = 0.1 # How fast it zooms per scroll tick
var target_zoom: float = 1.0 # Smoothly interpolate to this value
func _ready() -> void:
# Ensures the camera uses its actual starting zoom
target_zoom = zoom.x
func _process(delta: float) -> void:
handle_keyboard_movement(delta)
handle_edge_scrolling(delta)
handle_smooth_zoom(delta)
# 1. ARROW KEYS / WASD MOVEMENT
func handle_keyboard_movement(delta: float) -> void:
var velocity = Vector2.ZERO
if Input.is_key_pressed(KEY_LEFT):
velocity.x -= 1
if Input.is_key_pressed(KEY_RIGHT):
velocity.x += 1
if Input.is_key_pressed(KEY_UP):
velocity.y -= 1
if Input.is_key_pressed(KEY_DOWN):
velocity.y += 1
# Move the camera based on direction, speed, and time passed
global_position += velocity.normalized() * KEYBOARD_SPEED * delta
# 2. MOUSE EDGE SCROLLING
func handle_edge_scrolling(delta: float) -> void:
var mouse_pos = get_viewport().get_mouse_position()
var window_size = get_viewport().get_visible_rect().size
var velocity = Vector2.ZERO
# Check Left/Right edges
if mouse_pos.x < EDGE_MARGIN:
velocity.x -= 1
elif mouse_pos.x > window_size.x - EDGE_MARGIN:
velocity.x += 1
# Check Top/Bottom edges
if mouse_pos.y < EDGE_MARGIN:
velocity.y -= 1
elif mouse_pos.y > window_size.y - EDGE_MARGIN:
velocity.y += 1
global_position += velocity.normalized() * EDGE_SPEED * delta
# 3. MOUSE WHEEL ZOOM (Detecting the inputs)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_WHEEL_UP and event.pressed:
target_zoom = clamp(target_zoom + ZOOM_SPEED, MIN_ZOOM, MAX_ZOOM)
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN and event.pressed:
target_zoom = clamp(target_zoom - ZOOM_SPEED, MIN_ZOOM, MAX_ZOOM)
# 4. SMOOTH ZOOMING ANIMATION
func handle_smooth_zoom(delta: float) -> void:
# Lerp smoothly transitions the current zoom to our target_zoom over time
var current_zoom = zoom.x
var new_zoom = lerp(current_zoom, target_zoom, 10.0 * delta)
zoom = Vector2(new_zoom, new_zoom)