aboutsummaryrefslogtreecommitdiff
path: root/game/src/Game/LoadingScreen.gd
blob: c7dad9cc9e35f54347b54b647c08c27da787b6ec (plain) (blame)
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
extends Control

@export var quote_file_path : String = "res://common/quotes.txt"

@export_subgroup("Nodes")
@export var progress_bar: ProgressBar
@export var quote_label: Label
@export var animation_player: AnimationPlayer

var thread: Thread
var quotes: PackedStringArray = []

func start_loading_screen(thread_safe_function : Callable) -> void:
   if not is_node_ready():
      await ready
   # set first quote
   progress_bar.value = 0
   quote_label.text = quotes[randi() % quotes.size()]

   if thread != null and thread.is_started():
      thread.wait_to_finish()

   thread.start(thread_safe_function)
   Events.Loader.startup_load_begun.emit()

func try_update_loading_screen(percent_complete: float, quote_should_change = false):
   # forces the function to behave as if deferred
   await get_tree().process_frame
   progress_bar.value = percent_complete
   if quote_should_change:
      quote_label.text = quotes[randi() % quotes.size()]
   if is_equal_approx(percent_complete, 100):
      thread.wait_to_finish()
      Events.Loader.startup_load_ended.emit()
   else:
      Events.Loader.startup_load_changed.emit(percent_complete)

func _ready():
   if Engine.is_editor_hint(): return
   thread = Thread.new()
   # FS-3, UI-30, UIFUN-35
   var quotes_file := FileAccess.open(quote_file_path, FileAccess.READ).get_as_text()
   quotes = quotes_file.split("\n", false)
   if quotes.is_empty():
      quotes = [""]
   animation_player.play("loadingscreen_gear")

func _exit_tree():
   if thread != null and thread.is_started():
      thread.wait_to_finish()