scene_cam.gd 5.6 KB

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