extends RefCounted ## The loading screen (LoadingScreen): built hidden; play() shows it from black, its bar fills from ## 0 to 1 (never backwards) and finished comes once, seconds later (not before); it keeps moving ## until it's faded out (then hidden) or stopped; a second play() runs it again from the start; and ## it goes on while the game is paused. const DT := 1.0 / 60.0 var r: GameTestRunner var screen: LoadingScreen var fired: Array[int] = [0] # (a lambda captures an int by value) func run(p_r: GameTestRunner) -> void: r = p_r var layer := CanvasLayer.new() r.root.add_child(layer) screen = LoadingScreen.new() layer.add_child(screen) screen.set_process(false) # stepped by hand screen.finished.connect(func() -> void: fired[0] += 1) r.check(not screen.visible and screen.process_mode == Node.PROCESS_MODE_ALWAYS, "it's built hidden, and runs while the game is paused") _run() _again() _fade_and_stop() await _paused() layer.queue_free() await r.process_frame ## One run of 3 s: from black, the bar 0 -> 1, finished once at 3 s (a tick of slack), not before. func _run() -> void: screen.play() r.check(screen.visible and screen.running and screen.progress() == 0.0 and screen.black() == 1.0 and fired[0] == 0, "play() shows it black, the bar empty") var last := 0.0 var backwards := false var early := false var at_half := 0.0 var ticks := 0 while ticks < 179: # 2.983 s screen.step(DT) ticks += 1 backwards = backwards or screen.progress() < last last = screen.progress() early = early or fired[0] > 0 if ticks == 90: at_half = screen.progress() r.check(not early and screen.running, "finished doesn't come before the 3 s are up") r.check(not backwards and last > 0.9 and last < 1.0, "the bar fills and never goes back (%.3f just before the end)" % last) r.check(at_half > 0.4 and at_half < 0.6, "about half full half way (%.3f)" % at_half) r.check(screen.black() == 0.0, "it's out of the black well before the end") var dots := {} var fired_at := 0 for i in 3: screen.step(DT) ticks += 1 dots[screen.loading_text()] = true if fired[0] > 0 and fired_at == 0: fired_at = ticks r.check(fired[0] == 1 and fired_at >= 180 and fired_at <= 181 and not screen.running and screen.progress() == 1.0, "finished comes once at 3 s (180 ticks, a tick of slack), the bar full (fired %d at tick %d, %.3f)" % [fired[0], fired_at, screen.progress()]) for i in 120: screen.step(DT) dots[screen.loading_text()] = true r.check(fired[0] == 1 and screen.visible and screen.progress() == 1.0, "it stays up and full after, and finished doesn't come again") r.check(dots.size() == 4 and dots.has("Loading") and dots.has("Loading..."), "'Loading' under the bar, its dots cycling (%s)" % [dots.keys()]) ## A second play() starts over: black, the bar empty, finished once more after its own seconds. func _again() -> void: screen.play(1.0) r.check(screen.running and screen.progress() == 0.0 and screen.black() == 1.0 and fired[0] == 1, "a second play() starts over from black, the bar empty") for i in 59: screen.step(DT) r.check(fired[0] == 1 and screen.progress() < 1.0, "a 1 s run doesn't finish before 1 s") for i in 2: screen.step(DT) r.check(fired[0] == 2 and screen.progress() == 1.0, "...and finishes once at 1 s (fired %d)" % fired[0]) ## fade_out() fades it (modulate) and then hides it (hidden); stop() hides it at once, without ## finished; fading out before the end cuts it short. func _fade_and_stop() -> void: var hid: Array[int] = [0] var on_hidden := func() -> void: hid[0] += 1 screen.hidden.connect(on_hidden) screen.fade_out(0.5) for i in 15: screen.step(DT) r.check(screen.visible and screen.modulate.a > 0.35 and screen.modulate.a < 0.65, "fade_out(0.5) is half way at 0.25 s (alpha %.2f)" % screen.modulate.a) for i in 20: screen.step(DT) r.check(not screen.visible and hid[0] == 1, "...and hidden after it (hidden emitted)") screen.play() r.check(screen.visible and screen.modulate.a == 1.0, "play() after a fade shows it fully again") for i in 30: screen.step(DT) screen.stop() for i in 200: screen.step(DT) r.check(not screen.visible and fired[0] == 2, "stop() hides it at once, and finished doesn't come") screen.play() screen.fade_out(0.2) for i in 200: screen.step(DT) r.check(not screen.visible and fired[0] == 2, "fading out before the end cuts it short (no finished)") screen.hidden.disconnect(on_hidden) ## It moves on in real frames while the tree is paused. func _paused() -> void: screen.play(60.0) screen.set_process(true) r.paused = true for i in 5: await r.process_frame r.check(screen.elapsed > 0.0, "it goes on while the game is paused (%.3f s)" % screen.elapsed) r.paused = false screen.set_process(false) screen.stop()