Camera.gd 5.7 KB

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