extends RefCounted ## The talking-head conversations on the real farm: every conversation saved in game/resources/ ## dialogue/ is sound; the box (DialogueBox) writes a line letter by letter (a beat after a full ## stop), goes on and skips by input, styles the words (speaker, line, BBCode), and puts each ## speaker's head on their side; the director (DialogueDirector) plays a conversation at its ## night's phase and delay, pauses the farm for it and lets it go after, puts the morning sign ## aside meanwhile, doesn't replay it on a retry but does on a new vacation, ## takes it down on a new morning, plays several in order, waits under a menu, and lets one that ## doesn't pause the game go on by itself. The opening intro's conversations (dialogue/intro/) ## have their lines and speakers and never play by themselves; the farmer's and his wife's heads ## (Sketch icons) draw. const P := CampaignController.Phase const DT := 1.0 / 60.0 const INTRO := "res://game/resources/dialogue/intro/" const SPEAKERS := "res://game/resources/dialogue/speakers/" ## The intro's conversations: who says each line, in order. const INTRO_LINES := { "departure": ["Farmer", "Farmer's Wife", "Farmer"], "flight": ["Little Green Alien", "Big Green Alien", "Little Green Alien", "Big Green Alien", "Little Green Alien"], "uh_oh": ["Big Green Alien", "Little Green Alien"], } var r: GameTestRunner var sim: FarmSim var c: CampaignController var director: DialogueDirector var box: DialogueBox var alien: DialogueSpeaker var scarecrow: DialogueSpeaker func run(p_r: GameTestRunner) -> void: r = p_r _saved() _intro() await _heads_draw() var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D r.root.add_child(farm) await r.process_frame await r.physics_frame sim = farm.get_node("FarmSim") as FarmSim sim.set_physics_process(false) c = sim.campaign director = sim.dialogue r.check(director != null and director.box != null and director.box.get_parent() == farm.get_node("Hud"), "the farm has a dialogue director, its box in the HUD") if director == null: farm.queue_free() return box = director.box director.conversations.clear() # this suite's own conversations only alien = _speaker("Little Green Alien", DialogueSpeaker.HeadSide.LEFT, "thief") scarecrow = _speaker("Scarecrow", DialogueSpeaker.HeadSide.RIGHT, "portrait") _writing() _input() _style() await _triggers() _order_and_menus() _not_paused() director.stop() r.paused = false farm.queue_free() await r.process_frame ## Every conversation saved with the game loads, has lines with words, and plays within the vacation. func _saved() -> void: var all := DialogueDirector.load_all(DialogueDirector.DIR) var nights: int = (load("res://game/resources/game_config.tres") as GameConfig).campaign_nights var bad: Array[String] = [] for conv in all: if conv.lines.is_empty() or conv.night < 1 or conv.night > nights: bad.append(conv.resource_path) for line in conv.lines: if line == null or line.text.strip_edges() == "": bad.append(conv.resource_path) r.check(bad.is_empty(), "every saved conversation has lines with words and plays within the vacation (bad: %s)" % [bad]) r.check(DialogueDirector.load_all(SPEAKERS).is_empty(), "a speaker's file isn't taken for a conversation") ## The intro's conversations load with their lines and speakers, wait for the intro to play them ## (off, in a subfolder), and their speakers stand where they should. func _intro() -> void: for n: String in INTRO_LINES: var conv := load(INTRO.path_join(n + ".tres")) as Conversation if conv == null: r.check(false, "intro/%s.tres loads as a Conversation" % n) continue var who: Array[String] = [] for line in conv.lines: who.append(line.speaker.display_name if line != null and line.speaker != null and line.text.strip_edges() != "" else "?") r.check(who == INTRO_LINES[n], "intro/%s: its lines by %s (got %s)" % [n, INTRO_LINES[n], who]) r.check(not conv.enabled and conv.pause_game and is_equal_approx(conv.auto_advance, 1.6), "intro/%s is off (only the intro plays it), pauses (takes the taps), and goes on after 1.6 s" % n) var paths: Array[String] = [] for conv in DialogueDirector.load_all(DialogueDirector.DIR): paths.append(conv.resource_path.get_file()) r.check(paths == ["morning_day_2.tres"], "the director still plays only morning_day_2 by itself, not the intro's (got %s)" % [paths]) r.check(DialogueDirector.load_all(INTRO).is_empty(), "and even loaded from intro/ they're off") var big := load(SPEAKERS + "big_green_alien.tres") as DialogueSpeaker var little := load(SPEAKERS + "little_green_alien.tres") as DialogueSpeaker var farmer := load(SPEAKERS + "farmer.tres") as DialogueSpeaker var wife := load(SPEAKERS + "farmers_wife.tres") as DialogueSpeaker r.check(big != null and little != null and farmer != null and wife != null, "the intro's speakers load") if big == null or little == null or farmer == null or wife == null: return r.check(big.icon == little.icon and big.portrait_height > little.portrait_height and big.voice_pitch < little.voice_pitch, "the Big Green Alien is the little one's head, bigger (%d px), with a lower voice" % big.portrait_height) r.check(farmer.icon == "farmer" and wife.icon == "farmers_wife" and farmer.voice_pitch < wife.voice_pitch, "the farmer and his wife have their own heads; his voice is the lower") r.check(farmer.side == DialogueSpeaker.HeadSide.LEFT and wife.side == DialogueSpeaker.HeadSide.RIGHT and big.side == DialogueSpeaker.HeadSide.RIGHT and little.side == DialogueSpeaker.HeadSide.LEFT, "the farmer and the little alien on the left, his wife and the big alien on the right") var no_head: Array[String] = [] for f in ResourceLoader.list_directory(SPEAKERS): var sp := load(SPEAKERS + f) as DialogueSpeaker if sp != null and sp.portrait == null and not Sketch.ICONS.has(sp.icon): no_head.append(f) r.check(no_head.is_empty(), "every speaker has a head: a portrait or a Sketch icon (none: %s)" % [no_head]) ## The farmer's and his wife's heads are Sketch icons and draw (big, and HUD-small) without error. func _heads_draw() -> void: var ids: Array[String] = ["farmer", "farmers_wife"] r.check(Sketch.ICONS.has("farmer") and Sketch.ICONS.has("farmers_wife"), "the farmer and his wife are Sketch icons") var drawn: Array[String] = [] var canvas := Control.new() canvas.size = Vector2(400.0, 400.0) canvas.draw.connect(func() -> void: for id in ids: Sketch.icon(canvas, id, Rect2(Vector2.ZERO, Vector2(380.0, 380.0))) Sketch.icon(canvas, id, Rect2(Vector2(10.0, 10.0), Vector2(40.0, 40.0))) drawn.append(id)) r.root.add_child(canvas) await r.process_frame await r.process_frame r.check(drawn == ids, "their heads draw without an error (drew %s)" % [drawn]) canvas.queue_free() func _writing() -> void: var conv := _conversation(1, P.MORNING, 0.0, [_line(alien, "Hi. There"), _line(scarecrow, "Hello!")]) alien.letters_per_second = 10.0 r.check(box.play(conv) and box.is_playing() and box.visible and box.written_text() == "" and box.speaker_name() == "Little Green Alien", "play() puts the first line up, nothing written yet, under the speaker's name") r.check(not box.play(_conversation(1, P.MORNING, 0.0, [])) and box.conversation == conv, "a conversation without lines isn't shown") _frames(0.25) r.check(box.written_text() == "Hi", "letters come at the speaker's speed (10 a second: '%s' after 0.25 s)" % box.written_text()) _frames(0.2) var at_stop := box.written_text() _frames(0.2) r.check(at_stop == "Hi." and box.written_text() == "Hi.", "a beat after a full stop ('%s', still '%s' 0.2 s on)" % [at_stop, box.written_text()]) _frames(0.5) r.check(box.written_text().begins_with("Hi. T"), "then the words go on ('%s')" % box.written_text()) box.advance() r.check(box.is_written() and box.written_text() == "Hi. There" and box.index == 0, "going on mid-line writes the rest of it") box.advance() r.check(box.index == 1 and box.speaker_name() == "Scarecrow", "going on again: the next line, the next speaker") r.check(box._heads.size() == 2 and box._heads[0].leaving and box._heads[1].speaker == scarecrow, "the new speaker's head comes up; the last one's goes down") var done: Array[Conversation] = [] box.finished.connect(func(f: Conversation) -> void: done.append(f), CONNECT_ONE_SHOT) box.advance() box.advance() r.check(done.size() == 1 and done[0] == conv and not box.is_playing(), "after the last line the conversation is finished") _frames(0.6) r.check(not box.visible and box._heads.is_empty(), "and the box fades away, the head sinking") alien.letters_per_second = 45.0 func _input() -> void: var conv := _conversation(1, P.MORNING, 0.0, [_line(alien, "One"), _line(alien, "Two"), _line(alien, "Three")]) box.play(conv) r.check(box.mouse_filter == Control.MOUSE_FILTER_STOP and box.has_focus(), "a conversation that pauses the game takes the clicks and the keys") var click := InputEventMouseButton.new() click.button_index = MOUSE_BUTTON_LEFT click.pressed = true box._gui_input(click) r.check(box.is_written() and box.index == 0, "a click writes the rest of the line") box._gui_input(_action("ui_accept")) r.check(box.index == 1, "Enter (ui_accept) goes on to the next") box.blocked = func() -> bool: return true box._gui_input(_action("ui_accept")) box._gui_input(_action("ui_accept")) _frames(1.0) r.check(box.index == 1 and box.written_text() == "", "under a menu (blocked) it takes no input and writes nothing") box.blocked = Callable() box._gui_input(_action("hide_tips")) r.check(not box.is_playing(), "Backspace (hide_tips) skips the rest of the conversation") r.check(box.mouse_filter == Control.MOUSE_FILTER_IGNORE and not box.has_focus(), "and lets the clicks and keys go again") box.stop() func _style() -> void: var line := _line(scarecrow, "I [b]said[/b] go.") line.italic = true line.font_size = 50 line.text_color = Color.RED box.play(_conversation(1, P.MORNING, 0.0, [line, _line(null, "The wind blows.")])) var t := box._text r.check(t.text == "[i]I [b]said[/b] go.[/i]" and box._plain == "I said go.", "a line in italics, its BBCode kept ('%s')" % t.text) r.check(t.get_theme_font_size("normal_font_size") == 50 and t.get_theme_color("default_color") == Color.RED, "a line's size and color over the speaker's") r.check(t.get_theme_font("italics_font") is FontVariation and (t.get_theme_font("italics_font") as FontVariation).base_font == Sketch.font(), "italics are the handwritten font, slanted") r.check(box._name.horizontal_alignment == HORIZONTAL_ALIGNMENT_RIGHT and t.horizontal_alignment == HORIZONTAL_ALIGNMENT_RIGHT, "a speaker on the right has their name and words on the right") _frames(0.5) var head := box._current_head() var r_sheet := box._sheet_rect() r.check(head != null and head.speaker == scarecrow and head.risen == 1.0 and r_sheet.size.x == DialogueBox.WIDTH and absf(r_sheet.end.y - (box.size.y - DialogueBox.BOTTOM)) < 0.01, "their head is up, the paper over the hotbar") box.advance() box.advance() r.check(box.speaker_name() == "" and not box._name.visible and box._current_head() == null, "a line without a speaker: words only") box.stop() func _triggers() -> void: var morning := _conversation(2, P.MORNING, 0.0, [_line(alien, "Morning.")]) var night := _conversation(2, P.NIGHT, 5.0, [_line(scarecrow, "Night.")]) director.conversations.assign([morning, night]) c.new_campaign() _tick() r.check(not box.is_playing(), "night 1: nothing to say") _to_morning(2) await r.process_frame # the morning sign takes the focus (deferred) await r.process_frame _tick() r.check(box.conversation == morning and r.paused, "the morning of day 2: its conversation, the farm paused") r.check(box.has_focus() and not sim.review_panel.visible, "the morning sign waits; the box has the keys (Enter doesn't start the day)") box.skip() r.check(not r.paused and not director.is_playing() and sim.review_panel.visible, "done: the farm goes on, the morning sign is back") await r.process_frame r.check(sim.review_panel._start.has_focus(), "with Start the day focused") c.retry_night() _tick() r.check(not box.is_playing(), "a retry doesn't play it again") c.begin_day() c.advance_phase() c.advance_phase() _tick() r.check(c.state.phase == P.NIGHT and not box.is_playing(), "the night's conversation waits for its delay") c.state.phase_time_left = c.phase_length(P.NIGHT) - 5.5 _tick() r.check(box.conversation == night and r.paused, "5 s into night 2: it plays") c.retry_night() r.check(not box.is_playing() and not r.paused, "a new morning (here a retry) takes it down") c.new_campaign() _to_morning(2) _tick() r.check(box.conversation == morning, "a new vacation plays them again") director.stop() func _order_and_menus() -> void: var late := _conversation(3, P.MORNING, 2.0, [_line(alien, "Second.")]) var early := _conversation(3, P.MORNING, 1.0, [_line(alien, "First.")]) var asked := _conversation(1, P.MORNING, 0.0, [_line(scarecrow, "Asked.")]) director.conversations.assign([late, early]) _to_morning(3) _tick() r.check(box.conversation == early, "several at once: by delay") director.play(asked) box.skip() r.check(box.conversation == asked and r.paused, "play() while one is up: right after it, the farm still paused") box.skip() r.check(box.conversation == late, "then the rest") box.skip() r.check(not director.is_playing() and not r.paused, "then the farm goes on") director.conversations.assign([_conversation(4, P.MORNING, 0.0, [_line(alien, "Wait.")])]) box.blocked = func() -> bool: return true _to_morning(4) _tick() r.check(not box.is_playing(), "under Main's menus nothing starts") box.blocked = Callable() sim.upgrade_panel.is_open = true _tick() r.check(not box.is_playing(), "nor while a shop is open") sim.upgrade_panel.is_open = false _tick() r.check(box.is_playing(), "after them, it does") box.skip() director.replay() r.check(box.is_playing(), "replay() plays this phase's again") director.stop() func _not_paused() -> void: var conv := _conversation(5, P.MORNING, 0.0, [_line(alien, "Busy?"), _line(alien, "Never mind.")]) conv.pause_game = false director.conversations.assign([conv]) _to_morning(5) _tick() r.check(box.conversation == conv and not r.paused and box.mouse_filter == Control.MOUSE_FILTER_IGNORE and not box.has_focus(), "one that doesn't pause the game takes no clicks or keys") box._gui_input(_action("ui_accept")) r.check(box.index == 0, "Enter is the game's") _frames(0.5 + Conversation.AUTO_ADVANCE) r.check(box.index == 1, "its lines go on by themselves") _frames(0.5 + Conversation.AUTO_ADVANCE) r.check(not box.is_playing(), "and it ends by itself") func _to_morning(n: int) -> void: var s := c.state s.night = n s.nights_survived = n - 1 c.load_state(s) ## The director's frame (it starts what's due). func _tick() -> void: director._process(DT) ## The box's frames for seconds. func _frames(seconds: float) -> void: for i in roundi(seconds / DT): box._process(DT) func _action(name: String) -> InputEventAction: var e := InputEventAction.new() e.action = StringName(name) e.pressed = true return e static func _speaker(n: String, side: DialogueSpeaker.HeadSide, icon: String) -> DialogueSpeaker: var sp := DialogueSpeaker.new() sp.display_name = n sp.side = side sp.icon = icon return sp static func _line(sp: DialogueSpeaker, text: String) -> DialogueLine: var l := DialogueLine.new() l.speaker = sp l.text = text return l static func _conversation(n: int, phase: CampaignController.Phase, delay: float, lines: Array[DialogueLine]) -> Conversation: var conv := Conversation.new() conv.night = n conv.phase = int(phase) as Conversation.Phase conv.delay = delay conv.lines = lines return conv