Camera.gd 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Licensed under the MIT License.
  2. # Copyright (c) 2018 Jaccomo Lorenz (Maujoe)
  3. extends Camera
  4. # User settings:
  5. # General settings
  6. export var enabled = true setget set_enabled
  7. export(int, "Visible", "Hidden", "Caputered, Confined") var mouse_mode = 2
  8. # Mouslook settings
  9. export var mouselook = true
  10. export (float, 0.0, 1.0) var sensitivity = 0.5
  11. export (float, 0.0, 0.999, 0.001) var smoothness = 0.5 setget set_smoothness
  12. export(NodePath) var privot setget set_privot
  13. export var distance = 5.0 setget set_distance
  14. export var rotate_privot = false
  15. export var collisions = true setget set_collisions
  16. export (int, 0, 360) var yaw_limit = 360
  17. export (int, 0, 360) var pitch_limit = 360
  18. # Movement settings
  19. export var movement = true
  20. export (float, 0.0, 1.0) var acceleration = 1.0
  21. export (float, 0.0, 0.0, 1.0) var deceleration = 0.1
  22. export var max_speed = Vector3(1.0, 1.0, 1.0)
  23. export var local = true
  24. export var forward_action = "ui_up"
  25. export var backward_action = "ui_down"
  26. export var left_action = "ui_left"
  27. export var right_action = "ui_right"
  28. export var up_action = "ui_page_up"
  29. export var down_action = "ui_page_down"
  30. # Intern variables.
  31. var _mouse_position = Vector2(0.0, 0.0)
  32. var _yaw = 0.0
  33. var _pitch = 0.0
  34. var _total_yaw = 0.0
  35. var _total_pitch = 0.0
  36. var _direction = Vector3(0.0, 0.0, 0.0)
  37. var _speed = Vector3(0.0, 0.0, 0.0)
  38. var _gui
  39. func _ready():
  40. if privot:
  41. privot = get_node(privot)
  42. else:
  43. privot = null
  44. set_enabled(enabled)
  45. var mouse_c=false
  46. var is_ui=false
  47. func set_ui_input(val):
  48. is_ui=val
  49. func _input(event):
  50. if event is InputEventMouseButton:
  51. mouse_c=!mouse_c
  52. if mouselook and mouse_c and !is_ui:
  53. if event is InputEventMouseMotion:
  54. _mouse_position = event.relative
  55. if movement:
  56. if event.is_action_pressed(forward_action):
  57. _direction.z = -1
  58. elif event.is_action_pressed(backward_action):
  59. _direction.z = 1
  60. elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
  61. _direction.z = 0
  62. if event.is_action_pressed(left_action):
  63. _direction.x = -1
  64. elif event.is_action_pressed(right_action):
  65. _direction.x = 1
  66. elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
  67. _direction.x = 0
  68. if event.is_action_pressed(up_action):
  69. _direction.y = 1
  70. if event.is_action_pressed(down_action):
  71. _direction.y = -1
  72. elif not Input.is_action_pressed(up_action) and not Input.is_action_pressed(down_action):
  73. _direction.y = 0
  74. func _process(delta):
  75. if privot:
  76. _update_distance()
  77. if mouselook:
  78. _update_mouselook()
  79. if movement:
  80. _update_movement(delta)
  81. func _physics_process(delta):
  82. # Called when collision are enabled
  83. _update_distance()
  84. if mouselook:
  85. _update_mouselook()
  86. var space_state = get_world().get_direct_space_state()
  87. var obstacle = space_state.intersect_ray(privot.get_translation(), get_translation())
  88. if not obstacle.empty():
  89. set_translation(obstacle.position)
  90. func _update_movement(delta):
  91. var offset = max_speed * acceleration * _direction
  92. _speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
  93. _speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
  94. _speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
  95. # Apply deceleration if no input
  96. if _direction.x == 0:
  97. _speed.x *= (1.0 - deceleration)
  98. if _direction.y == 0:
  99. _speed.y *= (1.0 - deceleration)
  100. if _direction.z == 0:
  101. _speed.z *= (1.0 - deceleration)
  102. if local:
  103. translate(_speed * delta)
  104. else:
  105. global_translate(_speed * delta)
  106. func _update_mouselook():
  107. _mouse_position *= sensitivity
  108. _yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
  109. _pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
  110. _mouse_position = Vector2(0, 0)
  111. if yaw_limit < 360:
  112. _yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
  113. if pitch_limit < 360:
  114. _pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
  115. _total_yaw += _yaw
  116. _total_pitch += _pitch
  117. if privot:
  118. var target = privot.get_translation()
  119. var offset = get_translation().distance_to(target)
  120. set_translation(target)
  121. rotate_y(deg2rad(-_yaw))
  122. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  123. translate(Vector3(0.0, 0.0, offset))
  124. if rotate_privot:
  125. privot.rotate_y(deg2rad(-_yaw))
  126. else:
  127. rotate_y(deg2rad(-_yaw))
  128. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  129. func _update_distance():
  130. var t = privot.get_translation()
  131. t.z -= distance
  132. set_translation(t)
  133. func _update_process_func():
  134. # Use physics process if collision are enabled
  135. if collisions and privot:
  136. set_physics_process(true)
  137. set_process(false)
  138. else:
  139. set_physics_process(false)
  140. set_process(true)
  141. func _check_actions(actions=[]):
  142. if OS.is_debug_build():
  143. for action in actions:
  144. if not InputMap.has_action(action):
  145. print('WARNING: No action "' + action + '"')
  146. func set_privot(value):
  147. privot = value
  148. # TODO: fix parenting.
  149. # if privot:
  150. # if get_parent():
  151. # get_parent().remove_child(self)
  152. # privot.add_child(self)
  153. _update_process_func()
  154. func set_collisions(value):
  155. collisions = value
  156. _update_process_func()
  157. func set_enabled(value):
  158. enabled = value
  159. if enabled:
  160. Input.set_mouse_mode(mouse_mode)
  161. set_process_input(true)
  162. _update_process_func()
  163. else:
  164. set_process(false)
  165. set_process_input(false)
  166. set_physics_process(false)
  167. func set_smoothness(value):
  168. smoothness = clamp(value, 0.001, 0.999)
  169. func set_distance(value):
  170. distance = max(0, value)