-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_control.gd
232 lines (187 loc) · 6.1 KB
/
camera_control.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Licensed under the MIT License.
# Copyright (c) 2018 Jaccomo Lorenz (Maujoe)
extends Camera3D
class_name UserCamera
# User settings:
# General settings
@export var enabled = true: set=set_enabled
# Mouslook settings
@export var mouselook = true
@export_range(0.0, 1.0) var sensitivity:float = 0.5
@export_range( 0.0, 0.999, 0.001) var smoothness:float = 0.5: set=set_smoothness
@export var privot_path:NodePath
@export var privot:Node: set=set_privot
@export var distance = 5.0: set=set_distance
@export var rotate_privot = false
@export var collisions = true: set=set_collisions
@export_range(0, 360) var yaw_limit = 360 #todo set to int
@export_range(0, 360) var pitch_min = 0
@export_range(0, 360) var pitch_max = 360
# Movement settings
@export var movement = true
@export_range(0.0, 1.0) var acceleration:float = 1.0
@export_range(0.0, 0.0, 1.0) var deceleration:float = 0.1
@export var max_speed = Vector3(1.0, 1.0, 1.0)
@export var local = true
@export var forward_action = "ui_up"
@export var backward_action = "ui_down"
@export var left_action = "ui_left"
@export var right_action = "ui_right"
@export var up_action = "ui_page_up"
@export var down_action = "ui_page_down"
# Gui settings
@export var use_gui = false
@export var gui_action = "ui_cancel"
# Intern variables.
var _mouse_position = Vector2(0.0, 0.0)
var _yaw = 0.0
var _pitch = 0.0
var _total_yaw = 0.0
var _total_pitch = 0.0
var _direction = Vector3(0.0, 0.0, 0.0)
var _speed = Vector3(0.0, 0.0, 0.0)
var _gui
func _ready():
_check_actions([forward_action, backward_action, left_action, right_action, gui_action, up_action, down_action])
pitch_min = deg_to_rad(pitch_min)
pitch_max = deg_to_rad(pitch_max)
yaw_limit = deg_to_rad(yaw_limit)
if privot_path:
privot = get_node(privot_path)
else:
privot = null
set_enabled(enabled)
if use_gui:
pass
#_gui = preload("camera_control_gui.gd")
#_gui = _gui.new(self, gui_action)
#add_child(_gui)
func _input(event):
if mouselook:
if event is InputEventMouseMotion:
_mouse_position = event.relative
if movement:
if event.is_action_pressed(forward_action):
_direction.z = -1
elif event.is_action_pressed(backward_action):
_direction.z = 1
elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
_direction.z = 0
if event.is_action_pressed(left_action):
_direction.x = -1
elif event.is_action_pressed(right_action):
_direction.x = 1
elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
_direction.x = 0
if event.is_action_pressed(up_action):
_direction.y = 1
if event.is_action_pressed(down_action):
_direction.y = -1
elif not Input.is_action_pressed(up_action) and not Input.is_action_pressed(down_action):
_direction.y = 0
func _process(delta):
if privot:
_update_distance()
if mouselook and g.in_game:
_update_mouselook()
if movement:
_update_movement(delta)
func _physics_process(delta):
# Called when collision are enabled
_update_distance()
if mouselook:
_update_mouselook()
var space_state = get_world_3d().get_direct_space_state()
var params:PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.create(privot.get_position(), get_position())
var obstacle = space_state.intersect_ray(params).collider
if not obstacle.empty():
set_position(obstacle.position)
func _update_movement(delta):
var offset = max_speed * acceleration * _direction
_speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
_speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
_speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
# Apply deceleration if no input
if _direction.x == 0:
_speed.x *= (1.0 - deceleration)
if _direction.y == 0:
_speed.y *= (1.0 - deceleration)
if _direction.z == 0:
_speed.z *= (1.0 - deceleration)
if local:
translate(_speed * delta)
else:
global_translate(_speed * delta)
func _update_mouselook():
_mouse_position *= sensitivity
_yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
_pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
_mouse_position = Vector2(0, 0)
if yaw_limit < 2*PI:
_yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
if pitch_max < 2*PI:
#pitch_limit
_pitch = clamp(_pitch, pitch_min - _total_pitch, pitch_max - _total_pitch)
_total_yaw += _yaw
_total_pitch += _pitch
if privot:
var target = privot.get_translation()
var offset = get_position().distance_to(target)
set_position(target)
#comment this if controlling dir with mouse
#rotate_y(deg_to_rad(-_yaw))
rotate_object_local(Vector3(1,0,0), deg_to_rad(-_pitch))
translate(Vector3(0.0, 0.0, offset))
if rotate_privot:
privot.rotate_y(deg_to_rad(-_yaw))
else:
#comment this if controlling dir with mouse
#rotate_y(deg_to_rad(-_yaw))
rotate_object_local(Vector3(1,0,0), deg_to_rad(-_pitch))
#get_parent_node_3d().rotation = rotation
var parent:Marker3D = get_parent_node_3d()
parent.rotation.x = _total_pitch
parent.rotation.y = -_total_yaw
#g.player.rotate_y(deg_to_rad(-_yaw))
func _update_distance():
var t = privot.get_translation()
t.z -= distance
set_position(t)
func _update_process_func():
# Use physics process if collision are enabled
if collisions and privot:
set_physics_process(true)
set_process(false)
else:
set_physics_process(false)
set_process(true)
func _check_actions(actions=[]):
if OS.is_debug_build():
for action in actions:
if not InputMap.has_action(action):
print('WARNING: No action "' + action + '"')
func set_privot(value):
privot = value
# TODO: fix parenting.
# if privot:
# if get_parent():
# get_parent().remove_child(self)
# privot.add_child(self)
_update_process_func()
func set_collisions(value):
collisions = value
_update_process_func()
func set_enabled(value):
enabled = value
if enabled:
#Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
set_process_input(true)
_update_process_func()
else:
set_process(false)
set_process_input(false)
set_physics_process(false)
func set_smoothness(value):
smoothness = clamp(value, 0.001, 0.999)
func set_distance(value):
distance = max(0, value)