Camera_main.gd 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # Licensed under the MIT License.
  2. # Copyright (c) 2018 Jaccomo Lorenz (Maujoe)
  3. # MODIFIED do not use in your projecct
  4. extends Camera
  5. # User settings:
  6. # General settings
  7. export var enabled = true setget set_enabled
  8. export(int, "Visible", "Hidden", "Caputered, Confined") var mouse_mode = 2
  9. # Mouslook settings
  10. export var mouselook = true
  11. export (float, 0.0, 1.0) var sensitivity = 0.5
  12. export (float, 0.0, 0.999, 0.001) var smoothness = 0.5 setget set_smoothness
  13. export(NodePath) var privot setget set_privot
  14. export var distance = 5.0 setget set_distance
  15. export var rotate_privot = false
  16. export var collisions = true setget set_collisions
  17. export (int, 0, 360) var yaw_limit = 360
  18. export (int, 0, 360) var pitch_limit = 360
  19. # Movement settings
  20. export var movement = true
  21. export (float, 0.0, 1.0) var acceleration = 1.0
  22. export (float, 0.0, 1.0) var deceleration = 0.1
  23. export var max_speed = Vector3(1.0, 1.0, 1.0)
  24. export var local = true
  25. export var forward_action = "ui_up"
  26. export var backward_action = "ui_down"
  27. export var left_action = "ui_left"
  28. export var right_action = "ui_right"
  29. # Intern variables.
  30. var _mouse_position = Vector2(0.0, 0.0)
  31. var _yaw = 0.0
  32. var _pitch = 0.0
  33. var _total_yaw = 0.0
  34. var _total_pitch = 0.0
  35. var loaded=false
  36. var tp_frame=false
  37. var local_sp=Vector3(0,0,0)
  38. var _direction = Vector3(0.0, 0.0, 0.0)
  39. var _speed = Vector3(0.0, 0.0, 0.0)
  40. var _gui
  41. onready var cam2
  42. onready var arc
  43. onready var woolf
  44. var start_pos=Vector3(0,0,0)
  45. func _ready():
  46. if privot:
  47. privot = get_node(privot)
  48. else:
  49. privot = null
  50. set_enabled(enabled)
  51. cam2=get_node("../edge/Camera")
  52. start_pos=self.translation
  53. arc=get_node("../arc")
  54. woolf=get_node("../woolf")
  55. var mouse_c=false
  56. var is_ui=false
  57. func set_ui_input(event):
  58. if event is InputEventMouseButton:
  59. is_ui=!is_ui
  60. func _input(event):
  61. if(!loaded):
  62. return
  63. if event is InputEventMouseButton:
  64. mouse_c=!mouse_c
  65. if mouselook and mouse_c and !is_ui:
  66. if event is InputEventMouseMotion:
  67. _mouse_position = event.relative
  68. if movement:
  69. if event.is_action_pressed(forward_action):
  70. _direction.z = -1
  71. elif event.is_action_pressed(backward_action):
  72. _direction.z = 1
  73. elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
  74. _direction.z = 0
  75. if event.is_action_pressed(left_action):
  76. _direction.x = -1
  77. elif event.is_action_pressed(right_action):
  78. _direction.x = 1
  79. elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
  80. _direction.x = 0
  81. func _process(delta):
  82. if(!loaded):
  83. return
  84. arc.update_ppos(Vector2(-self.translation.x+10,-self.translation.z+10))
  85. arc.update_ppos2(Vector2(-woolf.translation.x+10,-woolf.translation.z+10))
  86. if privot:
  87. _update_distance()
  88. if mouselook:
  89. _update_mouselook()
  90. if movement:
  91. _update_movement(delta)
  92. cam2.transform=self.transform
  93. func _physics_process(delta):
  94. # Called when collision are enabled
  95. _update_distance()
  96. if mouselook:
  97. _update_mouselook()
  98. var space_state = get_world().get_direct_space_state()
  99. var obstacle = space_state.intersect_ray(privot.get_translation(), get_translation())
  100. if not obstacle.empty():
  101. set_translation(obstacle.position)
  102. var tmovtime=0
  103. var tpf=0
  104. const tpfs=100
  105. var lastd=0
  106. func _update_movement(delta):
  107. var offset = max_speed * acceleration * _direction
  108. _speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
  109. _speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
  110. _speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
  111. # Apply deceleration if no input
  112. if _direction.x == 0:
  113. _speed.x *= (1.0 - deceleration)
  114. if _direction.y == 0:
  115. _speed.y *= (1.0 - deceleration)
  116. if _direction.z == 0:
  117. _speed.z *= (1.0 - deceleration)
  118. if(!tp_frame):
  119. var ot=self.translation
  120. if local:
  121. translate(_speed * delta)
  122. else:
  123. global_translate(_speed * delta)
  124. var rangex=0.75
  125. if(tmovtime<4):
  126. var stx=(self.translation.y-ot.y)*0.45
  127. local_sp=self.translation-ot
  128. var drx=self.translation.y-start_pos.y
  129. lastd=drx
  130. if(rangex+abs(drx+stx)>rangex+abs(drx)):
  131. self.translation.y=ot.y+(1-min(abs(drx/rangex),1))*stx*(1-smoothstep(rangex/2,rangex,abs(drx)))
  132. else:
  133. self.translation.y=ot.y+stx
  134. # if((_direction.length()<0.1)):
  135. # tmovtime+=delta
  136. # if(tmovtime>4):
  137. # var st=self.translation.y-ot.y
  138. # local_sp=self.translation-ot
  139. # var dr=self.translation.y-start_pos.y
  140. # var dt=1-min(abs(dr/rangex)*1.5,1)
  141. # var ttr=Vector3(self.translation.x,start_pos.y,self.translation.z)
  142. # self.translation.y=self.translation.slerp(ttr,delta*(0.8+2.5*dt)*smoothstep(4,5.0,tmovtime)).y
  143. # else:
  144. # tmovtime=0
  145. else:
  146. tpf+=1
  147. if(tpf>tpfs):
  148. tpf=0
  149. tp_frame=false
  150. func _update_mouselook():
  151. _mouse_position *= sensitivity
  152. _yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
  153. _pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
  154. _mouse_position = Vector2(0, 0)
  155. if yaw_limit < 360:
  156. _yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
  157. if pitch_limit < 360:
  158. _pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
  159. _total_yaw += _yaw
  160. _total_pitch += _pitch
  161. if privot:
  162. var target = privot.get_translation()
  163. var offset = get_translation().distance_to(target)
  164. set_translation(target)
  165. rotate_y(deg2rad(-_yaw))
  166. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  167. translate(Vector3(0.0, 0.0, offset))
  168. if rotate_privot:
  169. privot.rotate_y(deg2rad(-_yaw))
  170. else:
  171. rotate_y(deg2rad(-_yaw))
  172. rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
  173. func _update_distance():
  174. var t = privot.get_translation()
  175. t.z -= distance
  176. set_translation(t)
  177. func _update_process_func():
  178. # Use physics process if collision are enabled
  179. if collisions and privot:
  180. set_physics_process(true)
  181. set_process(false)
  182. else:
  183. set_physics_process(false)
  184. set_process(true)
  185. func _check_actions(actions=[]):
  186. if OS.is_debug_build():
  187. for action in actions:
  188. if not InputMap.has_action(action):
  189. print('WARNING: No action "' + action + '"')
  190. func set_privot(value):
  191. privot = value
  192. # TODO: fix parenting.
  193. # if privot:
  194. # if get_parent():
  195. # get_parent().remove_child(self)
  196. # privot.add_child(self)
  197. _update_process_func()
  198. func set_collisions(value):
  199. collisions = value
  200. _update_process_func()
  201. func set_enabled(value):
  202. enabled = value
  203. if enabled:
  204. Input.set_mouse_mode(mouse_mode)
  205. set_process_input(true)
  206. _update_process_func()
  207. else:
  208. set_process(false)
  209. set_process_input(false)
  210. set_physics_process(false)
  211. func set_smoothness(value):
  212. smoothness = clamp(value, 0.001, 0.999)
  213. func set_distance(value):
  214. distance = max(0, value)