diff --git a/game/scripts/loading_screen.gd b/game/scripts/loading_screen.gd new file mode 100644 index 0000000..8d1a4da --- /dev/null +++ b/game/scripts/loading_screen.gd @@ -0,0 +1,279 @@ +class_name LoadingScreen +extends Control +## The loading screen between the opening cutscene and the first morning. It loads nothing: it's +## an animated title card. The splash art (assets/pictures/screens/splash-screen.png) covers the +## screen (cropped, never letterboxed) under a soft vignette, panning and zooming slowly (Ken +## Burns); "Harvest Season" stands over the sky in the handwritten font (Sketch), its pen +## underline written in under it; a hand-drawn progress bar (its pen lines "boiling", redrawn a +## little differently a few times a second) fills near the bottom, with a sketch UFO bobbing and +## wobbling over its leading edge, its beam on it and speed lines behind it; "Loading..." is +## written under it, the dots cycling. +## play(seconds) shows it from black (it fades in) and emits finished once, when the bar is full +## (seconds after play()). It stays up, still moving, until the caller takes it away: fade_out(time) +## fades it (modulate) and then hides it (CanvasItem.hidden is emitted), or stop() hides it at +## once. So: play(3.0), await finished, put the farm right under it, fade_out(0.6). +## Full-rect: the caller puts it on its own CanvasLayer. Works while the game is paused; step(delta) +## (from _process) drives it, so tests can step it by hand. + +signal finished + +const ART := "res://assets/pictures/screens/splash-screen.png" +const TITLE := "Harvest Season" +## The fade in from black, in seconds (at most a third of a short run). +const FADE_IN := 0.5 +## The Ken Burns move: zoom (times the cover scale) and the point of the picture held in view +## (0..1 across the slack), from the start to the end; the move lasts the run plus PAN_TAIL s, so +## it's still going while the caller fades the screen out. +const ZOOM_FROM := 1.04 +const ZOOM_TO := 1.14 +const FOCUS_FROM := Vector2(0.28, 0.25) +const FOCUS_TO := Vector2(0.64, 0.6) +const PAN_TAIL := 1.5 +## The title's size and top (a share of the screen's height); the bar's height, its widest, and its +## gap above the bottom, the fill's inset in it; the words under it. +const TITLE_SIZE := 168 +const TITLE_TOP := 0.075 +const BAR_HEIGHT := 42.0 +const BAR_INSET := 5.0 +const BAR_WIDTH := 920.0 +const BAR_BOTTOM := 200.0 +const WORDS_SIZE := 54 +## The UFO's size, and the hand-drawn "boil" (outlines redrawn a little differently) per second. +const UFO_SIZE := 172.0 +const BOIL_FPS := 6.0 +const FILL := Color(0.97, 0.75, 0.24) +const BEAM := Color(0.7, 1.0, 0.5) +const SEED := 1201.0 + +## The run's length (play()), the seconds since play(), and whether it's still filling. +var seconds: float = 3.0 +var elapsed: float = 0.0 +var running := false + +var _art: Texture2D +var _vignette: Texture2D +var _ufo: Control +var _black: ColorRect +var _fade_time: float = 0.0 # > 0 while fading out + + +func _ready() -> void: + set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) + process_mode = Node.PROCESS_MODE_ALWAYS + mouse_filter = Control.MOUSE_FILTER_STOP # nothing under it takes a click + _art = load(ART) as Texture2D + var g := Gradient.new() + g.offsets = PackedFloat32Array([0.0, 0.55, 1.0]) + g.colors = PackedColorArray([Color(0.0, 0.0, 0.0, 0.0), Color(0.0, 0.0, 0.0, 0.08), Color(0.02, 0.02, 0.01, 0.6)]) + var v := GradientTexture2D.new() + v.gradient = g + v.fill = GradientTexture2D.FILL_RADIAL + v.fill_from = Vector2(0.5, 0.5) + v.fill_to = Vector2(1.0, 1.0) + v.width = 256 + v.height = 256 + _vignette = v + _ufo = Control.new() + _ufo.mouse_filter = Control.MOUSE_FILTER_IGNORE + _ufo.size = Vector2(UFO_SIZE, UFO_SIZE) + _ufo.pivot_offset = Vector2(UFO_SIZE * 0.5, UFO_SIZE * 0.56) # turns about the saucer's middle + _ufo.draw.connect(func() -> void: Sketch.icon(_ufo, "ufo", Rect2(Vector2.ZERO, _ufo.size))) + add_child(_ufo) + _black = ColorRect.new() + _black.color = Color.BLACK + _black.mouse_filter = Control.MOUSE_FILTER_IGNORE + add_child(_black) + _black.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) + visible = false + + +## Shows it from black and runs it again from the start; finished comes p_seconds from now. +func play(p_seconds: float = 3.0) -> void: + seconds = maxf(p_seconds, 0.0) + elapsed = 0.0 + running = true + _fade_time = 0.0 + modulate.a = 1.0 + visible = true + _place() + + +## Fades it out over time seconds (as it keeps moving), then hides it. Fading out before the bar +## is full cuts it short: once it's hidden, finished doesn't come. +func fade_out(time: float = 0.5) -> void: + if not visible: + return + if time <= 0.0: + stop() + return + _fade_time = time + + +## Hides it at once (no finished). +func stop() -> void: + running = false + _fade_time = 0.0 + visible = false + + +## How full the bar is: 0 at play(), 1 once finished. +func progress() -> float: + if seconds <= 0.0: + return 1.0 if elapsed > 0.0 else 0.0 + var t := clampf(elapsed / seconds, 0.0, 1.0) + return clampf(t + sin(t * TAU * 2.0) * 0.025, 0.0, 1.0) # a little lurch, never backwards + + +## How black it still is (the fade in): 1 at play(), 0 after FADE_IN. +func black() -> float: + var fade := minf(FADE_IN, seconds / 3.0) + return 0.0 if fade <= 0.0 else clampf(1.0 - elapsed / fade, 0.0, 1.0) + + +## The words under the bar now: "Loading" and zero to three dots. +func loading_text() -> String: + return "Loading" + ".".repeat(int(elapsed * 3.0) % 4) + + +func _process(delta: float) -> void: + step(delta) + + +## Moves it on delta seconds: the bar fills (finished when it's full), the picture pans, the UFO +## bobs, and a fade out goes on. +func step(delta: float) -> void: + if not visible: + return + elapsed += delta + if running and elapsed >= seconds - 0.0001: + running = false + finished.emit() + if _fade_time > 0.0: + modulate.a = move_toward(modulate.a, 0.0, delta / _fade_time) + if modulate.a <= 0.0: + stop() + return + _place() + + +func _place() -> void: + _black.color.a = black() + var bar := _bar_rect() + var t := elapsed + var edge := Vector2(bar.position.x + BAR_INSET + (bar.size.x - 2.0 * BAR_INSET) * progress(), bar.position.y) + var bob := sin(t * 5.2) * 6.0 + sin(t * 8.3) * 2.0 + var drift := sin(t * 2.7) * 5.0 # it hunts back and forth over the edge + # The beam's foot (y 98 of the icon's 100) just reaches the bar's top. + _ufo.position = edge + Vector2(-UFO_SIZE * 0.5 + drift, -UFO_SIZE * 0.95 + bob) + _ufo.rotation = sin(t * 3.4) * 0.1 + sin(t * 7.9) * 0.035 + queue_redraw() + + +func _bar_rect() -> Rect2: + var w := minf(BAR_WIDTH, size.x * 0.6) + return Rect2((size.x - w) * 0.5, size.y - BAR_BOTTOM - BAR_HEIGHT, w, BAR_HEIGHT) + + +func _draw() -> void: + var r := Rect2(Vector2.ZERO, size) + draw_rect(r, Color.BLACK) + if _art != null: + draw_texture_rect(_art, _art_rect(), false) + draw_texture_rect(_vignette, r, false) + _shade(0.0, size.y * 0.42, 0.5, 0.0) # behind the title + _shade(size.y * 0.45, size.y * 0.74, 0.0, 0.62) # behind the bar + _shade(size.y * 0.74, size.y, 0.62, 0.85) + _draw_title() + _draw_bar() + + +## The picture covering the screen at this point of the Ken Burns move. +func _art_rect() -> Rect2: + var tex := _art.get_size() + var cover := maxf(size.x / tex.x, size.y / tex.y) + var u := clampf(elapsed / (seconds + PAN_TAIL), 0.0, 1.0) + u = sin(u * PI * 0.5) # slows to a stop rather than halting + var drawn := tex * cover * lerpf(ZOOM_FROM, ZOOM_TO, u) + var slack := drawn - size + return Rect2(-slack * FOCUS_FROM.lerp(FOCUS_TO, u), drawn) + + +## A dark band across the screen from y0 to y1, its alpha a0 at the top and a1 at the bottom. +func _shade(y0: float, y1: float, a0: float, a1: float) -> void: + var c0 := Color(0.02, 0.02, 0.01, a0) + var c1 := Color(0.02, 0.02, 0.01, a1) + draw_polygon(PackedVector2Array([Vector2(0.0, y0), Vector2(size.x, y0), Vector2(size.x, y1), Vector2(0.0, y1)]), + PackedColorArray([c0, c0, c1, c1])) + + +## The title, tilted a hair as if lettered by hand, over a soft shadow, and its gold pen underline +## written in left to right as the screen comes out of black. +func _draw_title() -> void: + var font_size := int(TITLE_SIZE * minf(size.y / 1080.0, size.x / 1920.0 * 1.2)) + var w := Sketch.text_width(TITLE, font_size) + var top := size.y * TITLE_TOP + var center := Vector2(size.x * 0.5, top + font_size * 0.6) + var outline := maxi(int(font_size * 0.14), 4) + draw_set_transform(center, -0.035 + sin(elapsed * 1.3) * 0.006, Vector2.ONE * (1.0 + sin(elapsed * 1.7) * 0.008)) + var at := Vector2(0.0, -font_size * 0.6) + Sketch.text(self, at + Vector2(6.0, 10.0), TITLE, font_size, Color(0.0, 0.0, 0.0, 0.4), HORIZONTAL_ALIGNMENT_CENTER, outline) + Sketch.text(self, at, TITLE, font_size, Sketch.GOLD, HORIZONTAL_ALIGNMENT_CENTER, outline) + var y := at.y + Sketch.font().get_ascent(font_size) + font_size * 0.2 # under the letters + var pts := Sketch.line_points(Vector2(-w * 0.4, y + font_size * 0.01), Vector2(w * 0.46, y - font_size * 0.02), SEED + 3.0, + font_size * 0.05) + var k := clampf((elapsed - 0.25) / 0.7, 0.0, 1.0) + var n := int(roundf(lerpf(1.0, pts.size(), 1.0 - pow(1.0 - k, 2.0)))) + if k > 0.0 and n >= 2: + var part := pts.slice(0, n) + var thick := maxf(font_size * 0.036, 3.0) + draw_polyline(part, Sketch.INK, thick + outline * 0.4, true) + draw_polyline(part, Sketch.GOLD, thick, true) + draw_set_transform_matrix(Transform2D.IDENTITY) + + +## The bar: a dark trough in a sketched ink frame (its pen lines boiling), filled up to progress() +## with painted straw-gold and pencil hatching, the beam's glow on its leading edge, and the +## words under it. +func _draw_bar() -> void: + var bar := _bar_rect() + var boil := float(int(elapsed * BOIL_FPS) % 3) * 17.0 + Sketch.box(self, bar, Color(0.05, 0.06, 0.04, 0.78), Sketch.INK, 0.0, SEED) + var p := progress() + var inner := bar.grow(-BAR_INSET) + var fill_w := inner.size.x * p + if fill_w > 2.0: + var fill := Rect2(inner.position, Vector2(fill_w, inner.size.y)) + Sketch.box(self, fill, FILL, Sketch.INK, 0.0, SEED + 1.0) + draw_rect(Rect2(fill.position + Vector2(3.0, 3.0), Vector2(maxf(fill_w - 6.0, 0.0), fill.size.y * 0.28)), + Color(1.0, 0.93, 0.62, 0.55)) + var hatch := Color(FILL.darkened(0.35), 0.7) + var slant := inner.size.y * 0.5 + var x := fill.position.x + 6.0 + var i := 0 + while x + slant <= fill.end.x - 3.0: + Sketch.line(self, Vector2(x, fill.end.y - 3.0), Vector2(x + slant, fill.position.y + 4.0), hatch, 2.0, SEED + 40.0 + i, 0.8) + x += 15.0 + i += 1 + Sketch.line(self, Vector2(fill.end.x, fill.position.y - 2.0), Vector2(fill.end.x, fill.end.y + 2.0), Sketch.INK, 3.0, SEED + 7.0 + boil, 1.2) + var frame := Sketch.corners(bar, SEED) + Sketch.outline(self, frame, Sketch.INK, 7.0, SEED + 5.0 + boil, 1.6, 1) + Sketch.outline(self, frame, Color(Sketch.TEXT, 0.92), 2.6, SEED + 5.0 + boil, 1.6) + if running: + _draw_trail(boil) + var glow_at := Vector2(inner.position.x + fill_w, bar.position.y + 2.0) + draw_colored_polygon(Sketch.blob(glow_at, 40.0, 13.0, SEED + 9.0), Color(BEAM, 0.28 + 0.08 * sin(elapsed * 9.0))) + draw_colored_polygon(Sketch.blob(glow_at, 20.0, 7.0, SEED + 10.0), Color(BEAM.lightened(0.5), 0.4)) + var words_size := int(WORDS_SIZE * minf(size.y / 1080.0, 1.0)) + var left := size.x * 0.5 - Sketch.text_width("Loading...", words_size) * 0.5 # the dots don't shift it + Sketch.text(self, Vector2(left, bar.end.y + 16.0), loading_text(), words_size, Sketch.TEXT, HORIZONTAL_ALIGNMENT_LEFT, 9) + + +## Pen speed lines trailing the UFO while the bar is still filling. +func _draw_trail(boil: float) -> void: + var mid := _ufo.position + Vector2(-UFO_SIZE * 0.01, UFO_SIZE * 0.55) + for k in 3: + var y := mid.y + (k - 1) * UFO_SIZE * 0.09 + var long := UFO_SIZE * (0.22 + 0.08 * absf(Sketch.noise(SEED + 60.0 + k + boil))) + var x := mid.x - (absf(k - 1.0) * 0.06 + 0.02) * UFO_SIZE + Sketch.line(self, Vector2(x - long, y), Vector2(x, y), Sketch.INK, 6.0, SEED + 50.0 + k + boil, 1.2) + Sketch.line(self, Vector2(x - long, y), Vector2(x, y), Color(Sketch.TEXT, 0.9), 2.5, SEED + 50.0 + k + boil, 1.2) diff --git a/game/scripts/loading_screen.gd.uid b/game/scripts/loading_screen.gd.uid new file mode 100644 index 0000000..7cad8d5 --- /dev/null +++ b/game/scripts/loading_screen.gd.uid @@ -0,0 +1 @@ +uid://do5ggbaxlgfq1 diff --git a/game/tests/run_tests.gd b/game/tests/run_tests.gd index 32bed4a..22b73b7 100644 --- a/game/tests/run_tests.gd +++ b/game/tests/run_tests.gd @@ -25,6 +25,7 @@ const SUITES := [ "res://game/tests/test_coop.gd", "res://game/tests/test_look.gd", "res://game/tests/test_dialogue.gd", + "res://game/tests/test_loading.gd", ] const TICK := 1.0 / 60.0 ## Points on the farm's edge (FarmZones.edge) for tests that send an enemy in from a known side: diff --git a/game/tests/test_loading.gd b/game/tests/test_loading.gd new file mode 100644 index 0000000..3c6b92b --- /dev/null +++ b/game/tests/test_loading.gd @@ -0,0 +1,125 @@ +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() diff --git a/game/tests/test_loading.gd.uid b/game/tests/test_loading.gd.uid new file mode 100644 index 0000000..e85ea09 --- /dev/null +++ b/game/tests/test_loading.gd.uid @@ -0,0 +1 @@ +uid://dcdysm47p1w6g