Handle capture mode toggle in _input

This commit is contained in:
Jarrod Doyle 2024-02-23 21:27:16 +00:00
parent 4d8a0fec10
commit e3f652e6b1
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 10 additions and 10 deletions

View File

@ -61,7 +61,6 @@ enum MovementType { VERTICAL, LATERAL }
@export var max_iteration_count := 4 @export var max_iteration_count := 4
var jump_pressed := false var jump_pressed := false
var escape_pressed_prev := false
var gravity := 9.8 var gravity := 9.8
var _velocity: Vector3 = Vector3() var _velocity: Vector3 = Vector3()
@ -93,26 +92,27 @@ func unlock_mouse() -> void:
# TODO should this have an action associated? # TODO should this have an action associated?
# TODO should it be in unhandled? # TODO should it be in unhandled?
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
# Rotate body/camera
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
var motion := event as InputEventMouseMotion var motion := event as InputEventMouseMotion
body.rotate_y(-motion.relative.x * mouse_sensitivity) body.rotate_y(-motion.relative.x * mouse_sensitivity)
head.rotate_x(-motion.relative.y * mouse_sensitivity) head.rotate_x(-motion.relative.y * mouse_sensitivity)
head.rotation.x = clamp(head.rotation.x, -1.4, 1.4) head.rotation.x = clamp(head.rotation.x, -1.4, 1.4)
# Toggle mouse capture mode
if event is InputEventKey && event.is_pressed():
var key_event := event as InputEventKey
if key_event.keycode == KEY_ESCAPE:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
unlock_mouse()
else:
lock_mouse()
# TODO should this be in unhandled input? # TODO should this be in unhandled input?
# TODO this should return void and just set the appropriate fields (input_dir!!) # TODO this should return void and just set the appropriate fields (input_dir!!)
# Input buffering? lol lmao # Input buffering? lol lmao
func get_input() -> Vector2: func get_input() -> Vector2:
# Toggle mouse capture mode
var escape_pressed_curr := Input.is_key_pressed(KEY_ESCAPE)
if escape_pressed_curr && !escape_pressed_prev:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
unlock_mouse()
else:
lock_mouse()
escape_pressed_prev = escape_pressed_curr
# We don't want to be moving if the mouse isn't captured! # We don't want to be moving if the mouse isn't captured!
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED: if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
return Vector2(0, 0) return Vector2(0, 0)