observer.gd 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. extends CharacterBody3D
  2. const STATE_MENU = 0
  3. const STATE_GRAB = 1
  4. var r_pos = Vector2()
  5. var state = STATE_MENU
  6. var initial_viewport_height = ProjectSettings.get_setting("display/window/size/viewport_height")
  7. @onready var camera = $Camera3D
  8. func _process(delta):
  9. if state != STATE_GRAB:
  10. return
  11. var x_movement = Input.get_axis(&"move_left", &"move_right")
  12. var z_movement = Input.get_axis(&"move_forward", &"move_backwards")
  13. var dir = direction(Vector3(x_movement, 0, z_movement))
  14. transform.origin += dir * 10 * delta
  15. var d = delta * 0.1 # Scale the input, easiest to do by scaling the delta.
  16. rotate(Vector3.UP, d * r_pos.x) # Yaw
  17. camera.transform = camera.transform.rotated(Vector3.RIGHT, d * r_pos.y) # Pitch
  18. r_pos = Vector2.ZERO # We've dealt with all the input, so set it to zero.
  19. func _input(event):
  20. if event is InputEventMouseMotion:
  21. # Scale mouse sensitivity according to resolution, so that effective mouse sensitivity
  22. # doesn't change depending on the viewport size.
  23. r_pos = -event.relative * (get_viewport().size.y / initial_viewport_height)
  24. if event.is_action("ui_cancel") and event.is_pressed() and not event.is_echo():
  25. if state == STATE_GRAB:
  26. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  27. state = STATE_MENU
  28. else:
  29. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  30. state = STATE_GRAB
  31. func direction(vector):
  32. var v = camera.get_global_transform().basis * vector
  33. return v.normalized()
  34. func _on_transparent_check_button_toggled(button_pressed):
  35. get_viewport().transparent_bg = button_pressed
  36. DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_TRANSPARENT, button_pressed)