Camera.gd 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #onready var sky_b=get_tree().get_root().get_node("scene/Sky")
  40. func _ready():
  41. #var iChannel=sky_b.get_viewport().get_texture()
  42. #self.environment.background_sky.set_panorama(iChannel)
  43. if privot:
  44. privot = get_node(privot)
  45. else:
  46. privot = null
  47. set_enabled(enabled)
  48. var mouse_c=false
  49. var is_ui=false
  50. func set_ui_input(event):
  51. if event is InputEventMouseButton:
  52. is_ui=!is_ui
  53. func _input(event):
  54. if event is InputEventMouseButton:
  55. mouse_c=!mouse_c
  56. if mouselook and mouse_c and !is_ui:
  57. if event is InputEventMouseMotion:
  58. _mouse_position = event.relative
  59. if movement:
  60. if event.is_action_pressed(forward_action):
  61. _direction.z = -1
  62. elif event.is_action_pressed(backward_action):
  63. _direction.z = 1
  64. elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
  65. _direction.z = 0
  66. if event.is_action_pressed(left_action):
  67. _direction.x = -1
  68. elif event.is_action_pressed(right_action):
  69. _direction.x = 1
  70. elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
  71. _direction.x = 0
  72. if event.is_action_pressed(up_action):
  73. _direction.y = 1
  74. if event.is_action_pressed(down_action):
  75. _direction.y = -1
  76. elif not Input.is_action_pressed(up_action) and not Input.is_action_pressed(down_action):
  77. _direction.y = 0
  78. func _process(delta):
  79. if privot:
  80. _update_distance()
  81. if mouselook:
  82. _update_mouselook()
  83. if movement:
  84. _update_movement(delta)
  85. func _physics_process(delta):
  86. # Called when collision are enabled
  87. _update_distance()
  88. if mouselook:
  89. _update_mouselook()
  90. var space_state = get_world().get_direct_space_state()
  91. var obstacle = space_state.intersect_ray(privot.get_translation(), get_translation())
  92. if not obstacle.empty():
  93. set_translation(obstacle.position)
  94. func _update_movement(delta):
  95. var offset = max_speed * acceleration * _direction
  96. _speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
  97. _speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
  98. _speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
  99. # Apply deceleration if no input
  100. if _direction.x == 0:
  101. _speed.x *= (1.0 - deceleration)
  102. if _direction.y == 0:
  103. _speed.y *= (1.0 - deceleration)
  104. if _direction.z == 0:
  105. _speed.z *= (1.0 - deceleration)
  106. if local:
  107. translate(_speed * delta)
  108. else:
  109. global_translate(_speed * delta)
  110. func _update_mouselook():
  111. _mouse_position *= sensitivity
  112. _yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
  113. _pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
  114. _mouse_position = Vector2(0, 0)
  115. if yaw_limit < 360:
  116. _yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
  117. if pitch_limit < 360:
  118. _pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
  119. _total_yaw += _yaw
  120. _total_pitch += _pitch
  121. if privot:
  122. var target = privot.get_translation()
  123. var offset = get_translation().distance_to(target)
  124. set_translation(target)
  125. rotate_y(deg2rad(-_yaw))
  126. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  127. translate(Vector3(0.0, 0.0, offset))
  128. if rotate_privot:
  129. privot.rotate_y(deg2rad(-_yaw))
  130. else:
  131. rotate_y(deg2rad(-_yaw))
  132. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  133. func _update_distance():
  134. var t = privot.get_translation()
  135. t.z -= distance
  136. set_translation(t)
  137. func _update_process_func():
  138. # Use physics process if collision are enabled
  139. if collisions and privot:
  140. set_physics_process(true)
  141. set_process(false)
  142. else:
  143. set_physics_process(false)
  144. set_process(true)
  145. func _check_actions(actions=[]):
  146. if OS.is_debug_build():
  147. for action in actions:
  148. if not InputMap.has_action(action):
  149. print('WARNING: No action "' + action + '"')
  150. func set_privot(value):
  151. privot = value
  152. # TODO: fix parenting.
  153. # if privot:
  154. # if get_parent():
  155. # get_parent().remove_child(self)
  156. # privot.add_child(self)
  157. _update_process_func()
  158. func set_collisions(value):
  159. collisions = value
  160. _update_process_func()
  161. func set_enabled(value):
  162. enabled = value
  163. if enabled:
  164. Input.set_mouse_mode(mouse_mode)
  165. set_process_input(true)
  166. _update_process_func()
  167. else:
  168. set_process(false)
  169. set_process_input(false)
  170. set_physics_process(false)
  171. func set_smoothness(value):
  172. smoothness = clamp(value, 0.001, 0.999)
  173. func set_distance(value):
  174. distance = max(0, value)