-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsurfacer_character.gd
1126 lines (901 loc) · 40.4 KB
/
surfacer_character.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
tool
class_name SurfacerCharacter, \
"res://addons/scaffolder/assets/images/editor_icons/scaffolder_character.png"
extends ScaffolderCharacter
## The main character class for Surfacer.[br]
## - This defines how your character reacts to input and decides when and were
## to navigate within the level.[br]
## - You should extend this with a sub-class for your specific character.[br]
## - You should then attach your sub-class to a scene for your character.[br]
## - You should then add a few important child scenes:[br]
## - MovementParameters[br]
## - ScaffolderCharacterAnimator[br]
## - CollisionShape2D[br]
## - (Optional) ProximityDetector[br]
signal behavior_changed(behavior, previous_behavior)
## - If true, then the character's movement will include jumps in place of
## walking, when possible.
export var is_bouncy := false
## If true, then the character sprite rotation will be updated to match the
## current surface-contact normal.
export var rotates_to_match_surface_normal := true
var movement_params: ReadonlyMovementParametersProxy
# Dictionary<Surface, Surface>
var possible_surfaces_set: Dictionary
var reachable_basis_surface: Surface
# Array<Surface>
var reachable_surfaces := []
# Array<Surface>
var reversibly_reachable_surfaces := []
var start_surface: Surface
var start_position_along_surface: PositionAlongSurface
var _start_attachment_surface_side_or_position = null
var just_triggered_jump := false
var is_rising_from_jump := false
var jump_count := 0
var is_dashing: bool setget ,_get_is_dashing
var _current_max_horizontal_speed_multiplier := 1.0
var _can_dash := true
var _actions_from_previous_frame := CharacterActionState.new()
var actions := CharacterActionState.new()
var surface_state := CharacterSurfaceState.new(self)
var navigation_state: CharacterNavigationState
# Array<KinematicCollision2DCopy>
var collisions := []
var graph: PlatformGraph
var surface_store: SurfaceStore
var navigator: SurfaceNavigator
var prediction: CharacterPrediction
var touch_listener: PlayerTouchListener
var behavior: Behavior setget _set_behavior
var default_behavior: Behavior
var previous_behavior: Behavior
# Dictionary<Script, Behavior>
var _behaviors_by_class := {}
# Array<Behavior>
var _behaviors_list := []
# Array<CharacterActionSource>
var _action_sources := []
# Dictionary<String, bool>
var _previous_actions_handlers_this_frame := {}
var _player_action_source: PlayerActionSource
var _dash_cooldown_timeout: int
var _dash_fade_tween: ScaffolderTween
var _was_activated_on_touch_down := false
var is_waiting_to_stop_on_surface := false
func _init() -> void:
self.add_to_group(Sc.characters.GROUP_NAME_CHARACTERS)
func _ready() -> void:
surface_state.previous_center_position = self.position
surface_state.center_position = self.position
# Start facing right.
surface_state.horizontal_facing_sign = 1
animator.face_right()
if Engine.editor_hint:
return
if can_be_player_character and \
!is_instance_valid(_player_action_source):
_initialize_player_character_functionality()
if movement_params.can_dash:
# Set up a Tween for the fade-out at the end of a dash.
_dash_fade_tween = ScaffolderTween.new(self)
if movement_params.bypasses_runtime_physics:
set_collision_mask_bit(
Su.WALLS_AND_FLOORS_COLLISION_MASK_BIT, false)
set_collision_mask_bit(
Su.FALL_THROUGH_FLOORS_COLLISION_MASK_BIT, false)
set_collision_mask_bit(
Su.WALK_THROUGH_WALLS_COLLISION_MASK_BIT, false)
if Sc.annotators.is_annotator_enabled(
ScaffolderAnnotatorTypes.PATH_PRESELECTION) and \
(can_be_player_character and \
Sc.annotators.params.is_player_prediction_shown or \
!can_be_player_character and \
Sc.annotators.params.is_npc_prediction_shown):
prediction = CharacterPrediction.new()
prediction.set_up(self)
_attach_prediction()
_init_platform_graph()
surface_state.update_for_initial_surface_attachment(
_start_attachment_surface_side_or_position)
_init_navigator()
_parse_behavior_children()
# Set up some annotators to help with debugging.
set_is_sprite_visible(false)
Sc.annotators.create_character_annotator(self)
func _destroy() -> void:
if is_instance_valid(prediction):
prediction.queue_free()
._destroy()
func _on_annotators_ready() -> void:
._on_annotators_ready()
_attach_prediction()
func _attach_prediction() -> void:
if !Sc.levels.session.has_started:
return
if is_instance_valid(prediction) and \
is_instance_valid(Sc.annotators.path_preselection_annotator):
Sc.annotators.path_preselection_annotator.add_prediction(prediction)
func _update_editor_configuration_debounced() -> void:
._update_editor_configuration_debounced()
if _configuration_warning != "":
return
if Engine.editor_hint:
# Validate MovementParameters from scene configuration.
var movement_params_matches: Array = Sc.utils.get_children_by_type(
self,
MovementParameters)
if is_instance_valid(category):
if movement_params_matches.size() > 0:
_set_configuration_warning(
("Must not define a MovementParameters child node, " +
"since this character is registered in a " +
"character-category in the app manifest: %s.") % \
category.name)
return
else:
if movement_params_matches.size() > 1:
_set_configuration_warning(
"Must only define a single MovementParameters child node.")
return
elif movement_params_matches.size() < 1:
_set_configuration_warning(
"Must define a MovementParameters child node.")
return
# Validate Behaviors from scene configuration.
var behaviors: Array = \
Sc.utils.get_children_by_type(self, Behavior)
var behavior_names := {}
for behavior in behaviors:
if behavior_names.has(behavior.behavior_name):
_set_configuration_warning(
("Must not define more than one Behavior " +
"of type %s.") % behavior.behavior_name)
behavior_names[behavior.behavior_name] = true
var default_behavior: Behavior
if behavior.is_active_at_start:
if is_instance_valid(default_behavior):
_set_configuration_warning(
"Only one Behavior should be marked " +
"as `is_active_at_start`.")
default_behavior = behavior
if !is_instance_valid(default_behavior):
_set_configuration_warning(
"One Behavior should be marked as " +
"`is_active_at_start`.")
_initialize_child_movement_params()
_set_configuration_warning("")
func _initialize_child_movement_params() -> void:
if is_instance_valid(movement_params):
return
# Get MovementParameters from the scene configuration.
var backing_movement_params: MovementParameters = \
Su.movement.character_movement_params[character_name]
backing_movement_params.call_deferred("_parse_shape_from_parent")
movement_params = ReadonlyMovementParametersProxy.new()
add_child(movement_params)
movement_params.set_backer(backing_movement_params)
movement_params.call_deferred("_parse_shape_from_parent")
func set_can_be_player_character(value: bool) -> void:
.set_can_be_player_character(value)
if can_be_player_character:
if _is_ready:
_initialize_player_character_functionality()
func _initialize_player_character_functionality() -> void:
_init_player_controller_action_source()
if Su.movement.uses_point_and_click_navigation:
assert(get_behavior(PlayerNavigationBehavior) == null)
var player_navigation_behavior := PlayerNavigationBehavior.new()
add_behavior(player_navigation_behavior)
_initialize_touch_listener()
func _initialize_touch_listener() -> void:
self.touch_listener = PlayerTouchListener.new(self)
add_child(touch_listener)
func set_is_player_control_active(
value: bool,
should_also_update_level := true) -> void:
var is_new_activation: bool = \
value and Sc.level.active_player_character != self
.set_is_player_control_active(value, should_also_update_level)
if Su.movement.uses_point_and_click_navigation:
if value and !(behavior is PlayerNavigationBehavior):
get_behavior(PlayerNavigationBehavior).is_active = true
if !value:
_was_activated_on_touch_down = false
if is_new_activation:
touch_listener.on_character_player_control_activated(
_was_activated_on_touch_down)
func _init_player_controller_action_source() -> void:
assert(!is_instance_valid(_player_action_source))
self._player_action_source = PlayerActionSource.new(self, true)
_action_sources.push_back(_player_action_source)
func _init_platform_graph() -> void:
var backer_graph: PlatformGraph = \
Sc.level.graph_parser.platform_graphs[character_name]
assert(backer_graph != null)
graph = ReadonlyPlatformGraphProxy.new()
graph.set_backer(backer_graph)
graph.movement_params_override = movement_params
graph.connect(
"surface_exclusion_changed",
self,
"_on_surface_exclusion_changed")
surface_store = Sc.level.surface_store
possible_surfaces_set = graph.surfaces_set
func _init_navigator() -> void:
navigator = SurfaceNavigator.new(self, graph)
navigator.interruption_resolution_mode = \
movement_params.default_nav_interrupt_resolution_mode
navigation_state = navigator.navigation_state
navigator.connect(
"navigation_ended",
self,
"_on_surfacer_character_navigation_ended")
_action_sources.push_back(navigator.instructions_action_source)
func _on_physics_process(delta: float) -> void:
var delta_scaled: float = Sc.time.scale_delta(delta)
collisions.clear()
_apply_movement()
_match_expected_navigation_trajectory()
_maintain_collisions()
collisions.invert()
_update_actions(delta_scaled)
surface_state.clear_just_changed_state()
_update_surface_state()
for behavior in _behaviors_list:
behavior._on_physics_process(delta_scaled)
_update_navigator(delta_scaled)
if surface_state.just_left_air and \
is_waiting_to_stop_on_surface:
_stop_nav_immediately()
actions.log_new_presses_and_releases(self)
# Flip the horizontal direction of the animation according to which way the
# character is facing.
if surface_state.horizontal_facing_sign == 1:
animator.face_right()
elif surface_state.horizontal_facing_sign == -1:
animator.face_left()
_process_actions()
_process_animation()
_process_sounds()
_update_collision_mask()
if surface_state.did_move_last_frame and \
is_instance_valid(touch_listener):
touch_listener.on_character_moved()
func _apply_movement() -> void:
if movement_params.bypasses_runtime_physics or \
velocity == Vector2.ZERO:
return
# Since move_and_slide automatically accounts for delta, we need to
# compensate for that in order to support our modified framerate.
var modified_velocity: Vector2 = velocity * Sc.time.get_combined_scale()
move_and_slide(
modified_velocity,
Sc.geometry.UP,
movement_params.stops_on_slope,
4,
Sc.geometry.FLOOR_MAX_ANGLE + Sc.geometry.WALL_ANGLE_EPSILON)
_record_collisions()
func _match_expected_navigation_trajectory() -> void:
if navigation_state.just_reached_destination:
_match_expected_path_end_trajectory()
elif navigation_state.just_started_edge:
_match_expected_edge_start_trajectory()
elif is_instance_valid(navigator.edge):
_match_expected_edge_trajectory()
assert(!Sc.geometry.is_point_partial_inf(velocity))
func _match_expected_path_end_trajectory() -> void:
if movement_params.forces_character_position_to_match_path_at_end:
position = navigator.previous_path.destination.target_point
if movement_params.forces_character_velocity_to_zero_at_path_end:
velocity = Vector2.ZERO
func _match_expected_edge_start_trajectory() -> void:
if movement_params.forces_character_position_to_match_edge_at_start:
position = navigator.edge.get_start()
if movement_params.forces_character_velocity_to_match_edge_at_start:
velocity = navigator.edge.velocity_start
surface_state.horizontal_acceleration_sign = 0
func _match_expected_edge_trajectory() -> void:
var playback_elapsed_time: float = \
navigator.playback.get_elapsed_time_scaled()
var progress_in_current_trajectory_frame := \
fmod(playback_elapsed_time, ScaffolderTime.PHYSICS_TIME_STEP) / \
ScaffolderTime.PHYSICS_TIME_STEP
if movement_params.syncs_character_position_to_edge_trajectory:
var current_trajectory_position := navigator.edge.get_position_at_time(
playback_elapsed_time)
var next_trajectory_position := navigator.edge.get_position_at_time(
playback_elapsed_time + ScaffolderTime.PHYSICS_TIME_STEP)
var interpolated_position: Vector2 = lerp(
current_trajectory_position,
next_trajectory_position,
progress_in_current_trajectory_frame)
var is_movement_beyond_expected_trajectory := \
current_trajectory_position == Vector2.INF
if !is_movement_beyond_expected_trajectory:
position = interpolated_position
if movement_params.syncs_character_velocity_to_edge_trajectory:
var current_trajectory_velocity := navigator.edge.get_velocity_at_time(
playback_elapsed_time)
var next_trajectory_velocity := navigator.edge.get_velocity_at_time(
playback_elapsed_time + ScaffolderTime.PHYSICS_TIME_STEP)
var interpolated_velocity: Vector2 = lerp(
current_trajectory_velocity,
next_trajectory_velocity,
progress_in_current_trajectory_frame)
var is_movement_beyond_expected_trajectory := \
current_trajectory_velocity == Vector2.INF
if !is_movement_beyond_expected_trajectory:
velocity = interpolated_velocity
func _match_expected_navigation_surface_state(
edge: Edge = null,
edge_time := INF) -> void:
if !is_instance_valid(edge):
edge = navigator.edge
if is_inf(edge_time):
edge_time = navigator.playback.get_elapsed_time_scaled()
edge.sync_expected_surface_state(surface_state, edge_time)
# - The move_and_slide system depends on some velocity always pushing the
# character into the floor (or other touched surface).
# - If we just zero this out, move_and_slide will produce false-negatives for
# collisions.
func _maintain_collisions() -> void:
if movement_params.syncs_character_position_to_edge_trajectory and \
navigation_state.is_currently_navigating and \
is_instance_valid(navigator.edge.trajectory):
var playback_elapsed_time: float = \
navigator.playback.get_elapsed_time_scaled()
var trajectory_index := \
int(playback_elapsed_time / ScaffolderTime.PHYSICS_TIME_STEP)
var is_at_end_of_edge := \
trajectory_index >= \
navigator.edge.trajectory \
.frame_continuous_positions_from_steps.size()
if is_at_end_of_edge and \
is_instance_valid(navigator.edge.get_end_surface()) and \
!(navigator.edge is IntraSurfaceEdge):
_trigger_collision_at_end_of_edge(navigator.edge)
return
_maintain_preexisting_collisions()
func _trigger_collision_at_end_of_edge(edge: Edge) -> void:
var surface := edge.get_end_surface()
var normal := surface.normal
var maintain_collision_velocity: Vector2 = \
MovementParameters.STRONG_SPEED_TO_MAINTAIN_COLLISION * \
-normal
var max_slides := 1
var position_before_move := position
var collision_count_before_move := collisions.size()
# Trigger another move_and_slide.
# - This will maintain collision state within Godot's collision system.
# - This will also ensure the character snaps to the surface.
move_and_slide(
maintain_collision_velocity,
Sc.geometry.UP,
movement_params.stops_on_slope,
max_slides,
Sc.geometry.FLOOR_MAX_ANGLE + Sc.geometry.WALL_ANGLE_EPSILON)
_record_collisions()
var details := (
"new_collisions=%d; " +
"previous_p=%s; " +
"ending_edge=%s; " +
"v=%s"
) % [
collisions.size() - collision_count_before_move,
Sc.utils.get_vector_string(position_before_move, 1),
EdgeType.get_prefix(navigator.edge.edge_type),
Sc.utils.get_vector_string(maintain_collision_velocity, 1),
]
_log("E end trig c",
details,
CharacterLogType.SURFACE,
true)
func _maintain_preexisting_collisions() -> void:
if movement_params.bypasses_runtime_physics or \
!surface_state.is_grabbing_surface or \
(surface_state.is_triggering_wall_release and \
surface_state.is_grabbing_wall) or \
(surface_state.is_triggering_ceiling_release and \
surface_state.is_grabbing_ceiling) or \
(surface_state.is_triggering_fall_through and \
surface_state.is_grabbing_floor) or \
actions.just_pressed_jump:
return
# TODO: We could use surface_state.grab_normal here, if we wanted to walk
# more slowly down hills.
var normal := surface_state.grabbed_surface.normal
var maintain_collision_velocity: Vector2 = \
MovementParameters.STRONG_SPEED_TO_MAINTAIN_COLLISION * \
-normal
var max_slides := 1
# Also maintain wall collisions.
if !surface_state.is_grabbing_wall and \
surface_state.is_touching_wall and \
!surface_state.is_triggering_wall_release and \
!surface_state.is_pressing_away_from_wall:
maintain_collision_velocity.x = \
MovementParameters.STRONG_SPEED_TO_MAINTAIN_COLLISION * \
surface_state.toward_wall_sign
max_slides = 2
var position_before_move := position
# Trigger another move_and_slide.
# - This will maintain collision state within Godot's collision system.
# - This will also ensure the character snaps to the surface.
move_and_slide(
maintain_collision_velocity,
Sc.geometry.UP,
movement_params.stops_on_slope,
max_slides,
Sc.geometry.FLOOR_MAX_ANGLE + \
Sc.geometry.WALL_ANGLE_EPSILON)
if navigator.edge is FallFromFloorEdge:
# Fall-from-floor edge trajectories assume that the character doesn't
# start descending until after entirely clearing the edge of the
# surface, so we need to undo any potential downward displacement from
# the curve of the character collider.
position = position_before_move
_record_collisions()
func _record_collisions() -> void:
var new_collision_count := get_slide_count()
var old_collision_count := collisions.size()
collisions.resize(old_collision_count + new_collision_count)
for i in new_collision_count:
collisions[old_collision_count + i] = \
KinematicCollision2DCopy.new(get_slide_collision(i))
func _update_navigator(delta_scaled: float) -> void:
navigator._update()
func _update_actions(delta_scaled: float) -> void:
# Record actions for the previous frame.
_actions_from_previous_frame.copy(actions)
# Clear actions for the current frame.
actions.clear()
actions.delta_scaled = delta_scaled
# Update actions for the current frame.
for action_source in _action_sources:
action_source.update(
actions,
_actions_from_previous_frame,
Sc.time.get_scaled_play_time(),
delta_scaled,
navigation_state)
actions.start_dash = \
Sc.level_button_input.is_action_just_pressed("dash") and \
movement_params.can_dash and \
_can_dash
CharacterActionSource.update_for_implicit_key_events(
actions,
_actions_from_previous_frame)
# Updates physics and character states in response to the current actions.
func _process_actions() -> void:
_previous_actions_handlers_this_frame.clear()
for action_handler in movement_params.action_handlers:
var is_action_relevant_for_surface: bool = \
action_handler.type == surface_state.surface_type or \
action_handler.type == SurfaceType.OTHER or \
(action_handler.is_jump and \
actions.just_pressed_jump and \
action_handler.type == \
surface_state.get_previous_surface_type() and \
Sc.time.get_scaled_play_time() <= \
surface_state.last_in_air_toggle_time + \
ScaffolderTime.PHYSICS_TIME_STEP)
var is_action_relevant_for_physics_mode: bool = \
!movement_params.bypasses_runtime_physics or \
!action_handler.uses_runtime_physics
if is_action_relevant_for_surface and \
is_action_relevant_for_physics_mode:
var executed: bool = action_handler.process(self)
_previous_actions_handlers_this_frame[action_handler.name] = \
executed
# TODO: This is sometimes useful for debugging.
# if executed and \
# action_handler.name != AllDefaultAction.NAME and \
# action_handler.name != CapVelocityAction.NAME and \
# action_handler.name != FloorDefaultAction.NAME and \
# action_handler.name != FloorFrictionAction.NAME and \
# action_handler.name != FloorWalkAction.NAME and \
# action_handler.name != AirDefaultAction.NAME:
# var name_str: String = Sc.utils.resize_string(
# action_handler.name,
# 20)
# _log(name_str,
# "",
# CharacterLogType.ACTION,
# true)
assert(!Sc.geometry.is_point_partial_inf(velocity))
func _process_animation() -> void:
if rotates_to_match_surface_normal:
animator.sync_position_rotation_for_contact_normal(
position,
collider,
surface_state.grabbed_surface,
surface_state.grab_position,
surface_state.grab_normal)
if surface_state.get_just_changed_to_neighbor_surface() and \
!surface_state.get_is_previous_surface_collinear_neighbor():
# TODO: Improve on this.
# - Right now, this skip is prevents an issue with the next animation
# starting its blend with the previous animation at a what is now a
# non-sense rotation.
# - However, it would be nice to still have a blend!
# - Maybe this is the answer?
# - Update animator to track current blend duration.
# - Then, add a Tween for updating the position and rotation in
# this case.
animator.skip_current_blend()
match surface_state.surface_type:
SurfaceType.FLOOR:
if actions.pressed_left or actions.pressed_right:
animator.play("Walk")
else:
animator.play("Rest")
SurfaceType.WALL:
if processed_action("WallClimbAction"):
if actions.pressed_up:
animator.play("ClimbUp")
elif actions.pressed_down:
animator.play("ClimbDown")
else:
Sc.logger.error("SurfacerCharacter._process_animation")
else:
animator.play("RestOnWall")
SurfaceType.CEILING:
if actions.pressed_left or actions.pressed_right:
animator.play("CrawlOnCeiling")
else:
animator.play("RestOnCeiling")
SurfaceType.AIR:
if velocity.y > 0:
animator.play("JumpFall")
else:
animator.play("JumpRise")
_:
Sc.logger.error("SurfacerCharacter._process_animation")
func _process_sounds() -> void:
if just_triggered_jump:
Sc.audio.play_sound("test_character_jump")
if surface_state.just_left_air:
Sc.audio.play_sound("land")
elif surface_state.just_touched_surface:
Sc.audio.play_sound("land")
func processed_action(name: String) -> bool:
return _previous_actions_handlers_this_frame.get(name) == true
func _update_surface_state() -> void:
surface_state.update()
if surface_state.just_changed_surface and \
surface_state.is_grabbing_surface:
_update_reachable_surfaces(surface_state.grabbed_surface)
if surface_state.just_entered_air:
_log("Released",
"grab_p=%s, %s" % [
Sc.utils.get_vector_string(surface_state.grab_position, 1),
surface_state.previous_grabbed_surface.to_string(false),
],
CharacterLogType.SURFACE,
false)
elif surface_state.just_changed_surface:
_log("Grabbed",
"grab_p=%s, %s" % [
Sc.utils.get_vector_string(surface_state.grab_position, 1),
surface_state.grabbed_surface.to_string(false),
],
CharacterLogType.SURFACE,
false)
elif surface_state.just_touched_surface:
var side_prefixes := []
if surface_state.is_touching_floor:
side_prefixes.push_back("F")
elif surface_state.is_touching_ceiling:
side_prefixes.push_back("C")
elif surface_state.is_touching_left_wall:
side_prefixes.push_back("LW")
elif surface_state.is_touching_right_wall:
side_prefixes.push_back("RW")
_log("Touched",
"side=%s" % Sc.utils.join(side_prefixes),
CharacterLogType.SURFACE,
false)
# Update whether or not we should currently consider collisions with
# fall-through floors and walk-through walls.
func _update_collision_mask() -> void:
set_collision_mask_bit(
Su.FALL_THROUGH_FLOORS_COLLISION_MASK_BIT,
!surface_state.is_descending_through_floors and \
velocity.y > 0)
set_collision_mask_bit(
Su.FALL_THROUGH_FLOORS_COLLISION_MASK_BIT,
!surface_state.is_ascending_through_ceilings and \
velocity.y < 0)
set_collision_mask_bit(
Su.WALK_THROUGH_WALLS_COLLISION_MASK_BIT,
surface_state.is_grabbing_walk_through_walls)
func _on_surfacer_character_navigation_ended(
did_navigation_finish: bool,
triggered_by_player: bool) -> void:
for behavior in _behaviors_list:
behavior._on_navigation_ended(did_navigation_finish)
# "Finished" means that the behavior ended itself, so there shouldn't be
# another behavior being triggered somewhere.
func _on_behavior_finished(behavior: Behavior) -> void:
# FIXME: LEFT OFF HERE: ------------------------
# - Not sure why behavior.next_behavior could be null, but let's fix it
# later.
if !is_instance_valid(behavior.next_behavior):
behavior.next_behavior = get_behavior(StaticBehavior)
if behavior != behavior.next_behavior:
behavior.next_behavior.trigger(false)
else:
Sc.logger.error(
("Behavior finished, but next behavior is the same: " +
"behavior=%s, character=%s, position=%s") % [
behavior.behavior_name,
character_name,
Sc.utils.get_vector_string(position),
],
false)
func _on_behavior_error(behavior: Behavior) -> void:
if behavior != behavior.next_behavior:
behavior.next_behavior.trigger(false)
else:
Sc.logger.error(
("Behavior errored, but next behavior is the same: " +
"behavior=%s, character=%s, position=%s") % [
behavior.behavior_name,
character_name,
Sc.utils.get_vector_string(position),
],
false)
func _on_behavior_move_target_destroyed(behavior: Behavior) -> void:
_on_behavior_finished(behavior)
func start_dash(horizontal_acceleration_sign: int) -> void:
if !_can_dash or \
!movement_params.can_dash:
return
var duration: float = \
movement_params.dash_fade_duration / \
Sc.time.get_combined_scale()
var delay: float = \
(movement_params.dash_duration -
movement_params.dash_fade_duration) / \
Sc.time.get_combined_scale()
_current_max_horizontal_speed_multiplier = \
movement_params.dash_speed_multiplier
velocity.x = get_current_air_max_horizontal_speed() * horizontal_acceleration_sign
velocity.y += movement_params.dash_vertical_boost
_dash_fade_tween.stop_all()
_dash_fade_tween.interpolate_property(
self,
"_current_max_horizontal_speed_multiplier",
_current_max_horizontal_speed_multiplier,
1.0,
duration,
"ease_in",
delay,
TimeType.PLAY_RENDER_SCALED)
_dash_fade_tween.start()
Sc.time.clear_timeout(_dash_cooldown_timeout)
_dash_cooldown_timeout = Sc.time.set_timeout(
self,
"set",
movement_params.dash_cooldown,
["_can_dash", true])
if horizontal_acceleration_sign > 0:
animator.face_right()
else:
animator.face_left()
_can_dash = false
func force_boost(boost: Vector2) -> void:
surface_state.clear_current_state()
velocity = boost
surface_state.velocity = velocity
position += Vector2(0.0, -1.0)
surface_state.center_position = position
surface_state.center_position_along_surface \
.match_current_grab(null, position)
navigator.stop(true)
## Triggers navigation and replaces preexisting behavior.
func navigate_imperatively(destination: PositionAlongSurface) -> bool:
var navigation_override_behavior: NavigationOverrideBehavior = \
get_behavior(NavigationOverrideBehavior)
if !is_instance_valid(navigation_override_behavior):
navigation_override_behavior = NavigationOverrideBehavior.new()
add_behavior(navigation_override_behavior)
navigation_override_behavior.destination = destination
navigation_override_behavior.trigger(false)
return navigation_state.is_currently_navigating
func stop_on_surface() -> void:
is_waiting_to_stop_on_surface = true
if surface_state.is_grabbing_surface:
_stop_nav_immediately()
func _stop_nav_immediately() -> void:
is_waiting_to_stop_on_surface = false
navigator.stop(false)
func set_start_attachment_surface_side_or_position(
surface_side_or_position) -> void:
_start_attachment_surface_side_or_position = surface_side_or_position
func get_intended_position(type: int) -> PositionAlongSurface:
match type:
IntendedPositionType.CENTER_POSITION:
return surface_state.center_position_along_surface if \
surface_state.is_grabbing_surface else \
PositionAlongSurfaceFactory.create_position_without_surface(
surface_state.center_position)
IntendedPositionType.CENTER_POSITION_ALONG_SURFACE:
return surface_state.center_position_along_surface
IntendedPositionType.LAST_POSITION_ALONG_SURFACE:
return surface_state.last_position_along_surface
IntendedPositionType.CLOSEST_SURFACE_POSITION:
var result := \
surface_state.center_position_along_surface if \
surface_state.is_grabbing_surface else \
SurfaceFinder.find_closest_position_on_a_surface(
surface_state.center_position,
self,
SurfaceReachability.ANY)
return result if \
is_instance_valid(result) else \
surface_state.last_position_along_surface
IntendedPositionType.EDGE_ORIGIN:
return navigator.edge.start_position_along_surface if \
navigation_state.is_currently_navigating else \
null
IntendedPositionType.EDGE_DESTINATION:
return navigator.edge.end_position_along_surface if \
navigation_state.is_currently_navigating else \
null
IntendedPositionType.PATH_ORIGIN:
return navigator.path.origin if \
navigation_state.is_currently_navigating else \
null
IntendedPositionType.PATH_DESTINATION:
return navigator.path.destination if \
navigation_state.is_currently_navigating else \
null
_:
Sc.logger.error("Invalid IntendedPositionType: %d" % type)
return null
func get_current_animation_state(
result: SurfacerCharacterAnimationState) -> void:
result.character_position = position
result.animation_name = animator.get_current_animation_name()
result.animation_position = \
animator.animation_player.current_animation_position
result.facing_left = surface_state.horizontal_facing_sign == -1
func _parse_behavior_children() -> void:
var behaviors: Array = \
Sc.utils.get_children_by_type(self, Behavior)
var projected_behaviors := []
for behavior in behaviors:
if behavior.owner != self:
projected_behaviors.push_back(behavior)
# If there are any projected behavior nodes, then disregard all owned
# behaviors, and only use the projected ones instead.
if !projected_behaviors.empty():
behaviors = projected_behaviors
default_behavior = null
for behavior in behaviors:
var script: Script = behavior.get_script()
assert(behavior is PlayerNavigationBehavior or \
get_behavior(script) == null)
_behaviors_by_class[script] = behavior
_behaviors_list.push_back(behavior)
behavior.is_enabled = true
if behavior.is_active_at_start:
default_behavior = behavior
_add_return_behavior_if_needed(behavior)
# Automatically add a StaticBehavior if no other behavior has been
# configured as active-at-start.
if default_behavior == null:
default_behavior = StaticBehavior.new()
default_behavior.is_active_at_start = true
add_behavior(default_behavior)
default_behavior.trigger(false)
func add_behavior(
behavior: Behavior,
is_default := false) -> void:
var script: Script = behavior.get_script()
assert(get_behavior(script) == null)
_behaviors_by_class[script] = behavior
_behaviors_list.push_back(behavior)
if Engine.editor_hint:
return
if is_default:
behavior.is_active_at_start = true
if is_instance_valid(default_behavior):