]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/Jump/subsystem_jump.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / Modules / Jump / subsystem_jump.gd
1 extends DialogicSubsystem
2
3 ## Subsystem that holds methods for jumping to specific labels, or return to the previous jump.
4
5 signal switched_timeline(info:Dictionary)
6 signal jumped_to_label(info:Dictionary)
7 signal returned_from_jump(info:Dictionary)
8 signal passed_label(info:Dictionary)
9
10
11 #region STATE
12 ####################################################################################################
13
14 func clear_game_state(_clear_flag:=DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
15         dialogic.current_state_info['jump_stack'] = []
16         dialogic.current_state_info.erase("last_label")
17
18
19 func load_game_state(_load_flag:=LoadFlags.FULL_LOAD) -> void:
20         if not 'jump_stack' in dialogic.current_state_info:
21                 dialogic.current_state_info['jump_stack'] = []
22
23 #endregion
24
25
26 #region MAIN METHODS JUMP
27 ####################################################################################################
28
29 func jump_to_label(label:String) -> void:
30         if label.is_empty():
31                 dialogic.current_event_idx = 0
32                 jumped_to_label.emit({'timeline':dialogic.current_timeline, 'label':"TOP"})
33                 return
34
35         var idx: int = -1
36         while true:
37                 idx += 1
38                 var event: Variant = dialogic.current_timeline.get_event(idx)
39                 if not event:
40                         idx = dialogic.current_event_idx
41                         break
42                 if event is DialogicLabelEvent and event.name == label:
43                         break
44         dialogic.current_event_idx = idx-1
45         jumped_to_label.emit({'timeline':dialogic.current_timeline, 'label':label})
46
47
48 func push_to_jump_stack() -> void:
49         dialogic.current_state_info['jump_stack'].push_back({'timeline':dialogic.current_timeline, 'index':dialogic.current_event_idx, 'label':dialogic.current_timeline_events[dialogic.current_event_idx].label_name})
50
51
52 func resume_from_last_jump() -> void:
53         var sub_timeline: DialogicTimeline = dialogic.current_timeline
54         var stack_info: Dictionary = dialogic.current_state_info['jump_stack'].pop_back()
55         dialogic.start_timeline(stack_info.timeline, stack_info.index+1)
56         returned_from_jump.emit({'sub_timeline':sub_timeline, 'label':stack_info.label})
57
58
59 func is_jump_stack_empty() -> bool:
60         return len(dialogic.current_state_info['jump_stack']) < 1
61
62 #endregion
63
64
65 #region MAIN MEHTODS LABELS
66 ####################################################################################################
67
68 func _ready() -> void:
69         passed_label.connect(_on_passed_label)
70
71
72 func _on_passed_label(info:Dictionary) -> void:
73         dialogic.current_state_info["last_label"] = info
74
75
76 ## Returns the identifier name of the last passed label
77 func get_last_label_identifier() -> String:
78         if not dialogic.current_state_info.has("last_label"):
79                 return ""
80
81         return dialogic.current_state_info["last_label"].identifier
82
83
84 ## Returns the display name of the last passed label (translated if translation are enabled)
85 func get_last_label_name() -> String:
86         if not dialogic.current_state_info.has("last_label"):
87                 return ""
88
89         return dialogic.current_state_info["last_label"].display_name
90 #endregion