Advertisement
Guest User

camera_control.gd

a guest
Jun 11th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. # camera_control.gd. Modified by Tobias Lindström to make the GUI not appear
  2.  
  3. # Licensed under the MIT License.
  4. # Copyright (c) 2018 Jaccomo Lorenz (Maujoe)
  5.  
  6. extends Camera
  7.  
  8. # User settings:
  9. # General settings
  10. export var enabled = true setget set_enabled
  11. export(int, "Visible", "Hidden", "Caputered, Confined") var mouse_mode = 2
  12.  
  13. # Mouslook settings
  14. export var mouselook = true
  15. export (float, 0.0, 1.0) var sensitivity = 0.5
  16. export (float, 0.0, 0.999, 0.001) var smoothness = 0.5 setget set_smoothness
  17. export(NodePath) var privot setget set_privot
  18. export var distance = 5.0 setget set_distance
  19. export var rotate_privot = false
  20. export var collisions = true setget set_collisions
  21. export (int, 0, 360) var yaw_limit = 360
  22. export (int, 0, 360) var pitch_limit = 360
  23.  
  24. # Movement settings
  25. export var movement = true
  26. export (float, 0.0, 1.0) var acceleration = 1.0
  27. export (float, 0.0, 0.0, 1.0) var deceleration = 0.1
  28. export var max_speed = Vector3(25.0, 25.0, 25.0)
  29. export var local = true
  30. export var forward_action = "ui_up"
  31. export var backward_action = "ui_down"
  32. export var left_action = "ui_left"
  33. export var right_action = "ui_right"
  34. export var up_action = "ui_page_up"
  35. export var down_action = "ui_page_down"
  36.  
  37. # Gui settings
  38. export var use_gui = true
  39. export var gui_action = "ui_cancel"
  40.  
  41. # Intern variables.
  42. var _mouse_position = Vector2(0.0, 0.0)
  43. var _yaw = 0.0
  44. var _pitch = 0.0
  45. var _total_yaw = 0.0
  46. var _total_pitch = 0.0
  47.  
  48. var _direction = Vector3(0.0, 0.0, 0.0)
  49. var _speed = Vector3(0.0, 0.0, 0.0)
  50. var _gui
  51.  
  52.  
  53. func _ready():
  54. _check_actions([forward_action, backward_action, left_action, right_action, gui_action, up_action, down_action])
  55.  
  56. if privot:
  57. privot = get_node(privot)
  58. else:
  59. privot = null
  60.  
  61. set_enabled(enabled)
  62.  
  63. if use_gui:
  64. _gui = preload("camera_control_gui.gd")
  65. _gui = _gui.new(self, gui_action)
  66. add_child(_gui)
  67.  
  68. func _input(event):
  69.  
  70. if mouselook:
  71. if event is InputEventMouseMotion:
  72. _mouse_position = event.relative
  73.  
  74. if movement:
  75. if event.is_action_pressed(forward_action):
  76. _direction.z = -1
  77. elif event.is_action_pressed(backward_action):
  78. _direction.z = 1
  79. elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
  80. _direction.z = 0
  81.  
  82. if event.is_action_pressed(left_action):
  83. _direction.x = -1
  84. elif event.is_action_pressed(right_action):
  85. _direction.x = 1
  86. elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
  87. _direction.x = 0
  88.  
  89. if event.is_action_pressed(up_action):
  90. _direction.y = 1
  91. if event.is_action_pressed(down_action):
  92. _direction.y = -1
  93. elif not Input.is_action_pressed(up_action) and not Input.is_action_pressed(down_action):
  94. _direction.y = 0
  95.  
  96. func _process(delta):
  97. if privot:
  98. _update_distance()
  99. if mouselook:
  100. _update_mouselook()
  101. if movement:
  102. _update_movement(delta)
  103.  
  104. func _physics_process(delta):
  105. # Called when collision are enabled
  106. _update_distance()
  107. if mouselook:
  108. _update_mouselook()
  109.  
  110. var space_state = get_world().get_direct_space_state()
  111. var obstacle = space_state.intersect_ray(privot.get_translation(), get_translation())
  112. if not obstacle.empty():
  113. set_translation(obstacle.position)
  114.  
  115. func _update_movement(delta):
  116. var offset = max_speed * acceleration * _direction
  117.  
  118. _speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
  119. _speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
  120. _speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
  121.  
  122. # Apply deceleration if no input
  123. if _direction.x == 0:
  124. _speed.x *= (1.0 - deceleration)
  125. if _direction.y == 0:
  126. _speed.y *= (1.0 - deceleration)
  127. if _direction.z == 0:
  128. _speed.z *= (1.0 - deceleration)
  129.  
  130. if local:
  131. translate(_speed * delta)
  132. else:
  133. global_translate(_speed * delta)
  134.  
  135. func _update_mouselook():
  136. _mouse_position *= sensitivity
  137. _yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
  138. _pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
  139. _mouse_position = Vector2(0, 0)
  140.  
  141. if yaw_limit < 360:
  142. _yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
  143. if pitch_limit < 360:
  144. _pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
  145.  
  146. _total_yaw += _yaw
  147. _total_pitch += _pitch
  148.  
  149. if privot:
  150. var target = privot.get_translation()
  151. var offset = get_translation().distance_to(target)
  152.  
  153. set_translation(target)
  154. rotate_y(deg2rad(-_yaw))
  155. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  156. translate(Vector3(0.0, 0.0, offset))
  157.  
  158. if rotate_privot:
  159. privot.rotate_y(deg2rad(-_yaw))
  160. else:
  161. rotate_y(deg2rad(-_yaw))
  162. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  163.  
  164. func _update_distance():
  165. var t = privot.get_translation()
  166. t.z -= distance
  167. set_translation(t)
  168.  
  169. func _update_process_func():
  170. # Use physics process if collision are enabled
  171. if collisions and privot:
  172. set_physics_process(true)
  173. set_process(false)
  174. else:
  175. set_physics_process(false)
  176. set_process(true)
  177.  
  178. func _check_actions(actions=[]):
  179. if OS.is_debug_build():
  180. for action in actions:
  181. if not InputMap.has_action(action):
  182. print('WARNING: No action "' + action + '"')
  183.  
  184. func set_privot(value):
  185. privot = value
  186. # TODO: fix parenting.
  187. # if privot:
  188. # if get_parent():
  189. # get_parent().remove_child(self)
  190. # privot.add_child(self)
  191. _update_process_func()
  192.  
  193. func set_collisions(value):
  194. collisions = value
  195. _update_process_func()
  196.  
  197. func set_enabled(value):
  198. enabled = value
  199. if enabled:
  200. Input.set_mouse_mode(mouse_mode)
  201. set_process_input(true)
  202. _update_process_func()
  203. else:
  204. set_process(false)
  205. set_process_input(false)
  206. set_physics_process(false)
  207.  
  208. func set_smoothness(value):
  209. smoothness = clamp(value, 0.001, 0.999)
  210.  
  211. func set_distance(value):
  212. distance = max(0, value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement