-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path.gd
423 lines (367 loc) · 13.5 KB
/
.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
extends KinematicBody
class_name Player
export var acceleration_floor := 10
export var acceleration_air := 1
export var acceleration_glide := 3
export var angular_velocity := 30
export var gravity_default := 9.8
export var gravity_glide := 6
export var jump := 5
export var mouse_sense := 0.1
export var joystick_sense := 1
export var speed_default := 1
export var speed_run := 4
export var speed_glide := 12
export var gliding_factor := 2
enum states {IDLE, WALK, RUN, GLIDE, MIND, JUMP}
var snap
var direction = Vector3()
var velocity = Vector3()
var gravity_vector = Vector3()
var movement = Vector3()
var lymph: int = 0 setget set_lymph, get_lymph
signal in_mind
signal out_mind
signal change_cursor(cursor_anim,speed)
signal move_puzzle(position)
signal lymph_changed(lymph_count)
signal glide_started(glide_time)
signal glide_restarted(reset_time)
export var glide_max_time : float = 10
export var reset_max_time : float = 3
onready var acceleration := acceleration_floor
onready var speed := speed_default
onready var gravity := gravity_default
onready var body := $Body
onready var camera := $Camera
onready var glide_timer := $glide_timer
onready var glide_reset := $glide_reset
var puzzle_parent = null
var puzzle_transform : Transform = Transform(Vector3.ZERO,Vector3.ZERO,Vector3.ZERO,Vector3.ZERO)
var center := Vector2.ZERO
var last_intersection = null
var characterAnimationTree
var state
var character_version="v0"
var animation_state
var hovering_=false
func get_lymph() -> int:
return lymph
func set_lymph(new_lymph : int) -> void:
lymph = new_lymph
func on_lymph_picked() -> void:
lymph += 1
emit_signal("lymph_changed", lymph)
var playback_speed=1
var animationPlayer=null
func _ready() -> void:
glide_timer.wait_time = glide_max_time
glide_reset.wait_time = reset_max_time
Global.connect("lymph_picked", self, "on_lymph_picked")
Global.connect("puzzle_entered",self,"on_puzzle_entered")
Global.connect("puzzle_exited",self,"on_puzzle_exited")
center = get_viewport().size/2
state = states.IDLE
#remove ifs when definitive character is ready.
if get_node("PhilosopherBug") !=null:
body=$PhilosopherBug
characterAnimationTree=$PhilosopherBug/RootNode/AnimationTree
elif get_node("bugatti") !=null:
body=$bugatti
characterAnimationTree=$bugatti/AnimationTree
character_version="v0"
elif get_node("bugattiv1") !=null:
body=$bugattiv1
characterAnimationTree=$bugattiv1/AnimationTree
animationPlayer=$bugattiv1/AnimationPlayer
animation_state = characterAnimationTree.get("parameters/playback")
animation_state.start("Idle_loop")
character_version="v1"
body.set_as_toplevel(true)
func _process(delta: float) -> void:
# Translate body to follow KinematicBody
body.global_transform.origin = global_transform.origin
# Rotate body to point to the movement
if direction != Vector3.ZERO:
body.rotation.y = lerp_angle(body.rotation.y, atan2(-direction.x, -direction.z), angular_velocity * delta)
func _physics_process(delta: float) -> void:
# gravity = Vector3.DOWN # undefined?
#get keyboard input
direction = Vector3.ZERO
#TODO review move_right and left
var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
var direction_keys = Vector3(h_input, 0, f_input)
# Get radians of camera rotation around Y axis
var camera_rotation = camera.get_global_transformation().basis.get_euler().y
# Rotate keys around Y by camera rotation
direction = direction_keys.rotated(Vector3.UP, camera_rotation).normalized()
if not puzzle_parent == null and not state == states.MIND:
var angle = get_angle_to_puzzle(camera_rotation)
var rotation_speed = range_lerp(angle, 3.14, 0, 1, 4)
var space_state = get_world().direct_space_state
var rayOrigin = camera.camera.project_ray_origin(center)
var rayEnd = rayOrigin + camera.camera.project_ray_normal(center) * 200
var intersection = space_state.intersect_ray(rayOrigin, rayEnd)
if not intersection.empty() and intersection.collider == puzzle_parent:
emit_signal("change_cursor","hold_RMB",1)
if Input.is_action_just_pressed("mind_connection"):
emit_signal("in_mind")
emit_signal("change_cursor","connected",1)
animation_state.travel("Connect_loop")
state=states.MIND
else:
emit_signal("change_cursor","connection_zone",rotation_speed)
if is_on_floor():
snap = -get_floor_normal()
if glide_timer.is_stopped():
acceleration = acceleration_floor
gravity_vector = Vector3.ZERO
var velocity_=velocity
var state_machine = characterAnimationTree["parameters/playback"]
match state:
states.IDLE:
if Input.is_action_just_pressed("glide") and glide_reset.is_stopped():
glide_timer.start()
state = states.GLIDE
animation_state.travel("Hover_loop")
_on_glide()
elif Input.is_action_just_pressed("jump") and is_on_floor() and glide_timer.is_stopped():
snap = Vector3.ZERO
gravity_vector = Vector3.UP * jump
state=states.JUMP
animation_state.travel("Jump")
elif velocity_.length() >0:
state=states.WALK
states.GLIDE:
if Input.is_action_pressed("glide") and not glide_timer.is_stopped():
_on_glide()
elif Input.is_action_just_released("glide"):
if not glide_timer.is_stopped():
_restart_timer()
_out_of_glide()
state=states.WALK
else:
state=states.WALK
states.RUN:
speed = speed_run
if Input.is_action_just_pressed("jump") and is_on_floor() and glide_timer.is_stopped():
snap = Vector3.ZERO
gravity_vector = Vector3.UP * jump
state=states.JUMP
animation_state.travel("Jump")
elif Input.is_action_just_pressed("glide") and glide_reset.is_stopped():
glide_timer.start()
emit_signal("glide_started", glide_max_time)
state = states.GLIDE
animation_state.travel("Hover_loop")
_on_glide()
elif Input.is_action_just_released("run"):
state = states.IDLE
else:
# var state_machine = characterAnimationTree["parameters/playback"]
#print(velocity_.length())
if glide_timer.is_stopped() and is_on_floor():
if velocity_.length()>0:
state_machine.travel("Moving_loop")
characterAnimationTree.set("parameters/Moving_loop/blend_position",velocity_.length())
#print(state_machine.get_travel_path("Jump"))
#characterAnimationTree["parameters/blend_position"].x=velocity_.length()
else:
# state_machine.travel("Idle-loop")
#print("Loop")
state=states.IDLE
state_machine.travel("Idle_loop")
states.MIND:
body.rotation.y = camera_rotation
direction = Vector3.ZERO
if Input.is_action_pressed("mind_connection"): # and state == states.MIND:
AudioEngine.play_sfx("concentracion")
AudioEngine.eq_music()
var space_state = get_world().direct_space_state
var rayOrigin = camera.camera.project_ray_origin(center)
var rayEnd = rayOrigin + camera.camera.project_ray_normal(center) * 2000
var intersection = space_state.intersect_ray(rayOrigin, rayEnd)
if intersection.empty():
pass
"""if last_intersection != null:
last_intersection.collider.move(Vector3.ZERO)
last_intersection = null"""
elif intersection.collider.is_in_group("Puzzle"):
last_intersection = intersection
emit_signal("move_puzzle",intersection.position)
else:
if last_intersection != null:
emit_signal("move_puzzle",Vector3.ZERO)
last_intersection = null
else:
AudioEngine.stop_sfx()
AudioEngine.uneq_music()
emit_signal("move_puzzle",Vector3.ZERO)
emit_signal("out_mind")
state=states.IDLE
animation_state.travel("idle_loop")
"""if Input.is_action_just_released("mind_connection"):
if last_intersection != null:
emit_signal("move_puzzle",Vector3.ZERO)
last_intersection = null
if Input.is_action_just_pressed("mind_trick"):
state = states.IDLE
if last_intersection != null:
last_intersection.collider.move(Vector3.ZERO)
last_intersection = null
emit_signal("out_mind")"""
states.JUMP:
speed = speed_default
if is_on_floor():
state=states.WALK
# state_machine.travel("Mooving_loop")
elif Input.is_action_just_pressed("glide") and glide_reset.is_stopped():
glide_timer.start()
emit_signal("glide_started", glide_max_time)
state = states.GLIDE
animation_state.travel("Hover_loop")
_on_glide()
states.WALK:
speed = speed_default
if Input.is_action_just_pressed("jump") and is_on_floor() and glide_timer.is_stopped():
snap = Vector3.ZERO
gravity_vector = Vector3.UP * jump
state=states.JUMP
animation_state.travel("Jump")
elif Input.is_action_just_pressed("glide") and glide_reset.is_stopped():
emit_signal("glide_started", glide_max_time)
glide_timer.start()
state = states.GLIDE
animation_state.travel("Hover_loop")
_on_glide()
elif Input.is_action_pressed("run"):
state = states.RUN
else:
#print(velocity_.length())
if glide_timer.is_stopped() and is_on_floor():
if velocity_.length()>0:
if Input.is_action_pressed("run"):
speed=speed_run
else:
speed=speed_default
state_machine.travel("Moving_loop")
characterAnimationTree.set("parameters/Moving_loop/blend_position",velocity_.length())
#animationPlayer.playback_speed=2*velocity_.length()/speed_default
#print(state_machine.get_travel_path("Jump"))
#characterAnimationTree["parameters/blend_position"].x=velocity_.length()
else:
# state_machine.travel("Idle-loop")
#print("Loop")
state=states.IDLE
state_machine.travel("Idle_loop")
pass
#jumping and gravity
if is_on_floor():
pass
else:
"""var gliding_factor_=1
if Input.is_action_pressed("jump"):
gliding_factor_=gliding_factor
print("glide")"""
snap = Vector3.DOWN
if glide_timer.is_stopped():
acceleration = acceleration_air
gravity_vector += Vector3.DOWN * gravity * delta
#make it move
velocity = velocity.linear_interpolate(direction * speed, acceleration * delta)
# TODO use friction to stop the player from moving
# if direction == Vector3.ZERO:
# velocity = velocity.linear_interpolate(direction * speed, friction * delta)
movement = velocity + gravity_vector
move_and_slide_with_snap(movement, snap, Vector3.UP, false, 4, PI/4, false)
# if character_version=="v0":
# # the speed triggers the blended animation
# characterAnimationTree["parameters/blend_position"].x=velocity_.length()
# characterAnimationTree["parameters/blend_position"].y=gravity_vector.length()
# #print(velocity.length()/speed,velocity_.length())
# elif character_version=="v1":
# var state_machine = characterAnimationTree["parameters/playback"]
# #print(velocity_.length())
# if not hovering_ and is_on_floor():
# if velocity_.length()>0:
# state_machine.travel("Moving_loop")
# characterAnimationTree.set("parameters/Moving_loop/blend_position",velocity_.length())
# #print(state_machine.get_travel_path("Jump"))
# #characterAnimationTree["parameters/blend_position"].x=velocity_.length()
# else:
# # state_machine.travel("Idle-loop")
# #print("Loop")
# state_machine.travel("Idle_loop")
#func stop():
# $PhilosopherBug/RootNode/AnimationPlayer.play("idle")
# #$PhilosopherBug/RootNode/AnimationTree.
#
#func walk():
# $PhilosopherBug/RootNode/AnimationPlayer.play("walk")
#func change_state(next_state) -> void:
# var previous_state = state
#
# enter_state(next_state)
# clean(previous_state)
#
#func enter_state(next_state) -> void:
# state = next_state
# match next_state:
# states.GLIDE:
# acceleration = acceleration_glide
# speed = speed_glide
# gravity = gravity_glide
# snap = Vector3.DOWN
# hovering_=true
# animation_state.travel("Hover_loop")
#
#func clean(previous_state) -> void:
# match previous_state:
# states.GLIDE:
# speed = speed_default
# gravity = gravity_default
# hovering_=false
func _on_glide_timer_timeout() -> void:
glide_reset.start()
speed = speed_default
glide_reset.wait_time = reset_max_time
glide_reset.start()
emit_signal("glide_restarted", reset_max_time)
func _on_glide():
acceleration = acceleration_glide
speed = speed_glide
gravity = gravity_glide
snap = Vector3.DOWN
hovering_=true
animation_state.travel("Hover_loop")
func _restart_timer():
var proportion_left : float = glide_timer.time_left/glide_max_time
glide_timer.stop()
var reset_time : float = reset_max_time * (1 - proportion_left)
glide_reset.wait_time = reset_time
glide_reset.start()
emit_signal("glide_restarted", reset_time)
func _out_of_glide():
speed = speed_default
gravity = gravity_default
hovering_=false
#animation_state.travel("Moving_loop")
func get_angle_to_puzzle(camera_rotation) -> float:
var looking_at = Vector3(0,0,1).rotated(Vector3.UP,camera_rotation)
var from_puzzle_to_me = global_transform.origin - puzzle_transform.origin
return looking_at.angle_to(from_puzzle_to_me)
func on_puzzle_entered(puzzle_transform_new : Transform, parent) -> void:
puzzle_transform = puzzle_transform_new
puzzle_parent = parent
func on_puzzle_exited() -> void:
puzzle_parent = null
func move_player_to_lake()-> void:
transform = get_node("../RespawnLakepoint").global_transform
camera.rotation_degrees = Vector3(0,-70,0)
body.rotation.y = camera.get_global_transformation().basis.get_euler().y
#get_node("../RespawnLakepoint").global_transform.basis.get_euler()
#transform.origin = Vector3(88,0,-70)
#transform.basis.rotated(Vector3(0,1,0),deg2rad(-85))#rotation_degrees(Vector3(0,-85,0))
func _on_fade_in_tween_all_completed() -> void:
move_player_to_lake()