#32: animated loading screen

LoadingScreen (game/scripts/loading_screen.gd): splash-screen.png cover-scaled
with a slow Ken Burns pan and zoom under a vignette, "Harvest Season" in the
handwritten font with a pen underline written in, a sketched progress bar with
the "ufo" icon bobbing over its leading edge, and "Loading..." with cycling
dots. play(seconds) fades in from black and emits finished once at full
progress; fade_out(time) / stop() take it away. Runs while paused; step(delta)
drives it. test_loading covers timing, restart, fade, stop, and pause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-07 21:46:27 -04:00
parent 5dbad4c915
commit a4d60212c5
5 changed files with 407 additions and 0 deletions

View file

@ -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)