71 lines
2.2 KiB
GDScript
71 lines
2.2 KiB
GDScript
extends Camera3D
|
|
|
|
@export var base_move_speed := 5.0
|
|
@export var min_move_speed := 1.0
|
|
@export var max_move_speed := 20.0
|
|
@export var rotation_speed := 0.002
|
|
|
|
var input_delay_over := false
|
|
var current_move_speed := base_move_speed
|
|
var mouse_delta := Vector2.ZERO
|
|
var mouse_pos := Vector2.ZERO
|
|
|
|
func _process(delta):
|
|
if Input.is_action_just_pressed("camera_control"):
|
|
mouse_pos = get_viewport().get_mouse_position()
|
|
input_delay_over = false
|
|
get_tree().create_timer(0.15).timeout.connect(
|
|
func():
|
|
if Input.is_action_pressed("camera_control"):
|
|
input_delay_over = true
|
|
)
|
|
if Input.is_action_pressed("camera_control") and input_delay_over:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
handle_movement(delta)
|
|
handle_rotation()
|
|
else:
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
if Input.is_action_just_released("camera_control") and input_delay_over:
|
|
Input.warp_mouse(mouse_pos)
|
|
|
|
func _unhandled_input(event):
|
|
if event is InputEventMouseButton and event.is_pressed():
|
|
handle_scroll_wheel(event)
|
|
if event is InputEventMouseMotion:
|
|
mouse_delta = event.relative
|
|
|
|
func handle_movement(delta: float) -> void:
|
|
var input_dir = Vector3.ZERO
|
|
if Input.is_key_pressed(KEY_W):
|
|
input_dir.z -= 1
|
|
if Input.is_key_pressed(KEY_S):
|
|
input_dir.z += 1
|
|
if Input.is_key_pressed(KEY_A):
|
|
input_dir.x -= 1
|
|
if Input.is_key_pressed(KEY_D):
|
|
input_dir.x += 1
|
|
if Input.is_key_pressed(KEY_E):
|
|
input_dir.y += 1
|
|
if Input.is_key_pressed(KEY_Q):
|
|
input_dir.y -= 1
|
|
|
|
if input_dir.length() > 0:
|
|
input_dir = input_dir.normalized()
|
|
translate(input_dir * current_move_speed * delta)
|
|
|
|
func handle_rotation() -> void:
|
|
var current_rotation_x = rotation.x - mouse_delta.y * rotation_speed
|
|
current_rotation_x = clamp(current_rotation_x, -1.5, 1.5)
|
|
var current_rotation_y = rotation.y - mouse_delta.x * rotation_speed
|
|
rotation = Vector3(current_rotation_x, current_rotation_y, 0)
|
|
mouse_delta = Vector2.ZERO
|
|
|
|
func handle_scroll_wheel(event: InputEventMouseButton ) -> void:
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
adjust_move_speed(1.0)
|
|
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
adjust_move_speed(-1.0)
|
|
|
|
func adjust_move_speed(delta: float) -> void:
|
|
current_move_speed = clamp(current_move_speed + delta, min_move_speed, max_move_speed)
|