Talking-head dialogue: DialogueDirector, DialogueBox, conversations and speakers

Conversations saved in game/resources/dialogue/ play once at their night and
phase in a paper box with talking heads; example: the morning of day 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-07 21:33:54 -04:00
parent bbd8012fff
commit 1d92f49d96
22 changed files with 1158 additions and 2 deletions

View file

@ -22,6 +22,8 @@ signal campaign_ended(outcome: Outcome)
## The dawn grace is over: listeners settle the night (enemies leave, loot on the ground returns)
## before its losses are recorded and the outcome is checked.
signal night_ending
## A vacation starts: a new one (new_campaign) or a saved one continued (load_state).
signal started
@export var config: GameConfig
@ -57,6 +59,7 @@ func _physics_process(delta: float) -> void:
func new_campaign() -> void:
state = FarmState.create(config)
outcome = Outcome.CONTINUE
started.emit()
_enter(Phase.MORNING)
@ -106,6 +109,7 @@ func advance_phase() -> void:
func load_state(saved: FarmState) -> void:
state = saved
outcome = Outcome.CONTINUE
started.emit()
_enter(Phase.MORNING)

View file

@ -11,6 +11,7 @@ extends CanvasLayer
## F8: run from the editor, Godot stops the game on F8 (its Stop shortcut)
## N start the night early (only when GameConfig.allow_early_night is on)
## R after Game Over: retry the night; after the farmers return: new vacation
## F5 play this phase's talking-head conversations again (DialogueDirector)
## F6 end the current phase now
## F7 cycle game speed 1x / 5x / 20x
## F9 debug: scarecrow to 0 HP
@ -73,6 +74,9 @@ func _unhandled_input(event: InputEvent) -> void:
unlock_all()
KEY_N:
campaign.start_night_early()
KEY_F5:
if sim != null and sim.dialogue != null:
sim.dialogue.replay()
KEY_F6:
campaign.advance_phase()
KEY_F7:

View file

@ -0,0 +1,36 @@
class_name Conversation
extends Resource
## A talking-head conversation: its lines (DialogueLine), shown one at a time in the DialogueBox,
## and when it plays. Saved in game/resources/dialogue/, it plays by itself (DialogueDirector)
## once, delay seconds into the phase of its night: the morning of day 2 is night 2, MORNING;
## night 18 is night 18, NIGHT. The morning isn't timed, so its conversations start at once
## (the morning sign waits until they're done). From code, sim.dialogue.play(conversation) shows
## one at any time.
## The phases of a night (CampaignController.Phase, in the same order).
enum Phase { MORNING, DAY, DUSK, NIGHT, DAWN }
## Seconds a line stays when the game isn't paused and auto_advance is 0.
const AUTO_ADVANCE := 3.0
## Off: it never plays by itself (play() still shows it).
@export var enabled := true
@export_range(1, 99) var night := 1
@export var phase: Phase = Phase.MORNING
## Seconds into the phase (timed phases only).
@export_range(0.0, 600.0, 0.5, "suffix:s") var delay := 0.0
## Stops the farm while it's up, and the player goes through the lines. In co-op the farm can't
## stop: the player's scarecrow stands still instead. Off: the farm plays on, the box takes no
## input (the player is busy), and the lines go on by themselves.
@export var pause_game := true
## Seconds a line stays once it's written before the next one comes by itself; 0 waits for the
## player (AUTO_ADVANCE when the game isn't paused).
@export_range(0.0, 30.0, 0.1, "suffix:s") var auto_advance := 0.0
@export var lines: Array[DialogueLine] = []
## Seconds a written line stays before the next one comes by itself (INF: until the player goes
## on).
func line_hold() -> float:
if auto_advance > 0.0:
return auto_advance
return INF if pause_game else AUTO_ADVANCE

View file

@ -0,0 +1 @@
uid://b8csl51l2cwpg

View file

@ -0,0 +1,459 @@
class_name DialogueBox
extends Control
## The talking-head box: a conversation (Conversation) one line at a time on a sheet of paper like
## the morning sign's (Sketch.paper, nailed at its top corners) above the hotbar, the speaker's
## name over their words on their side of it, and their picture (the talking head) standing behind
## the paper's top edge on that side. A new speaker's head rises from behind the paper (the last
## one's sinks back); while the words are written, letter by letter (a beat longer after a comma
## or a full stop), the head bobs, flaps its mouth (DialogueSpeaker.talk_frames), and blips
## (voice); then it breathes, and a pen arrow bobs in the corner. A line without a speaker is
## narration: words only.
## Click, [Enter / Space / E] or (A) writes the rest of the line, then goes on to the next;
## [Backspace / Back] skips the rest of the conversation. One that doesn't pause the game takes no
## input and goes on by itself (Conversation.line_hold). Works while the game is paused
## (DialogueDirector pauses it for a conversation); waits while blocked (a menu is up over it).
signal finished(conversation: Conversation)
## The paper's size and its gap above the screen's bottom (the hotbar is under it).
const WIDTH := 1360.0
const HEIGHT := 350.0
const BOTTOM := 118.0
const SIDE_GAP := 40.0
## The name's and words' inset from the paper's edges.
const MARGIN := Vector2(64.0, 30.0)
## A head stands this far in from the paper's side, its bottom TUCK behind the paper.
const HEAD_INSET := 30.0
const TUCK := 44.0
const RISE_TIME := 0.25
const OPEN_TIME := 0.18
## A beat after these letters, when a space or the end follows.
const PAUSES := {".": 0.32, "!": 0.32, "?": 0.32, "…": 0.32, ",": 0.14, ";": 0.14, ":": 0.14}
const TALK_FPS := 9.0
const SEED := 701.0
## Optional: whether a menu is up over the box (it waits: no input, no writing).
var blocked: Callable
## The conversation up (null when none), and its line on the paper.
var conversation: Conversation
var index: int = -1
static var _narrator := DialogueSpeaker.new()
static var _variants := {} # "font id/slant/bold" -> FontVariation
var _stage: Control
var _sheet: Control
var _name: Label
var _text: RichTextLabel
var _more: Control
var _voice: AudioStreamPlayer
var _heads: Array[Head] = []
var _speaker: DialogueSpeaker
var _plain := ""
var _shown: int = 0
var _speed := 0.0
var _budget := 0.0
var _wait := 0.0
var _hold := 0.0
var _letters: int = 0
var _open := 0.0
var _closing := false
var _time := 0.0
var _focus_before: Control
## A speaker's head on the stage: rising in (risen 0 -> 1) or sinking away (leaving).
class Head:
var speaker: DialogueSpeaker
var face: Texture2D
var risen := 0.0
var leaving := false
var talking := false
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
process_mode = Node.PROCESS_MODE_ALWAYS
mouse_filter = Control.MOUSE_FILTER_IGNORE
_stage = Control.new()
_stage.clip_contents = true # a sunk head is out of sight below the paper's top
_stage.mouse_filter = Control.MOUSE_FILTER_IGNORE
_stage.draw.connect(_draw_heads)
add_child(_stage)
_sheet = Control.new()
_sheet.mouse_filter = Control.MOUSE_FILTER_IGNORE
_sheet.draw.connect(_draw_sheet)
add_child(_sheet)
_name = Label.new()
_name.mouse_filter = Control.MOUSE_FILTER_IGNORE
_sheet.add_child(_name)
_text = RichTextLabel.new()
_text.bbcode_enabled = true
_text.scroll_active = false
_text.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_text.visible_characters_behavior = TextServer.VC_CHARS_AFTER_SHAPING # words don't jump lines as they're written
_text.mouse_filter = Control.MOUSE_FILTER_IGNORE
_sheet.add_child(_text)
_more = Control.new()
_more.mouse_filter = Control.MOUSE_FILTER_IGNORE
_more.size = Vector2(40.0, 40.0)
_more.draw.connect(_draw_more)
_sheet.add_child(_more)
_voice = AudioStreamPlayer.new()
_voice.bus = &"SFX"
add_child(_voice)
visible = false
## Shows conversation c from its first line, over whatever was up. Returns false (and shows
## nothing) when it has no lines.
func play(c: Conversation) -> bool:
if c == null or c.lines.is_empty():
return false
conversation = c
_closing = false
visible = true
_take_input(c.pause_game)
_show_line(0)
_place()
return true
func is_playing() -> bool:
return conversation != null
func is_blocked() -> bool:
return blocked.is_valid() and bool(blocked.call())
## Whether the line on the paper is all written.
func is_written() -> bool:
return _shown >= _plain.length()
## The words on the paper as written so far (no BBCode), for tests.
func written_text() -> String:
return _plain.substr(0, _shown)
func speaker_name() -> String:
return _name.text
## Writes the rest of the line, or goes on to the next one (after the last: closes).
func advance() -> void:
if conversation == null:
return
if not is_written():
_shown = _plain.length()
_wait = 0.0
_text.visible_characters = -1
return
if index + 1 < conversation.lines.size():
_show_line(index + 1)
else:
_finish()
## Skips the rest of the conversation.
func skip() -> void:
if conversation != null:
_finish()
## Takes the box away at once, without finished.
func stop() -> void:
if conversation != null:
_take_input(false)
conversation = null
_closing = false
_heads.clear()
_open = 0.0
visible = false
func _finish() -> void:
var c := conversation
conversation = null
_closing = true # the paper fades and the head sinks, unless the next conversation starts now
_take_input(false)
finished.emit(c)
func _show_line(i: int) -> void:
index = i
var line := conversation.lines[i]
_speaker = line.speaker if line.speaker != null else _narrator
_style(line, _speaker)
_text.text = _bbcode(line, _speaker)
_text.visible_characters = 0
_plain = _text.get_parsed_text()
_shown = 0
_budget = 0.0
_wait = 0.0
_letters = 0
_hold = conversation.line_hold()
_speed = line.letters_per_second if line.letters_per_second > 0.0 else _speaker.letters_per_second
if _speed <= 0.0:
advance() # all at once
_set_head(_speaker, line.portrait if line.portrait != null else _speaker.portrait)
func _style(line: DialogueLine, sp: DialogueSpeaker) -> void:
var font: Font = sp.font if sp.font != null else Sketch.font()
_name.text = sp.display_name
_name.visible = sp.display_name != ""
_name.add_theme_font_override("font", font)
_name.add_theme_font_size_override("font_size", sp.name_size)
_name.add_theme_color_override("font_color", sp.name_color)
_name.add_theme_constant_override("outline_size", 0)
_name.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT if sp.side == DialogueSpeaker.HeadSide.RIGHT else HORIZONTAL_ALIGNMENT_LEFT
_text.add_theme_font_override("normal_font", font)
_text.add_theme_font_override("italics_font", _variant(font, true, false))
_text.add_theme_font_override("bold_font", _variant(font, false, true))
_text.add_theme_font_override("bold_italics_font", _variant(font, true, true))
var font_size := line.font_size if line.font_size > 0 else sp.font_size
for key: String in ["normal_font_size", "italics_font_size", "bold_font_size", "bold_italics_font_size"]:
_text.add_theme_font_size_override(key, font_size)
_text.add_theme_color_override("default_color", line.text_color if line.text_color.a > 0.0 else sp.text_color)
_text.add_theme_constant_override("line_separation", 2)
match sp.align:
DialogueSpeaker.Align.LEFT:
_text.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
DialogueSpeaker.Align.CENTER:
_text.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
DialogueSpeaker.Align.RIGHT:
_text.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_:
_text.horizontal_alignment = _name.horizontal_alignment
static func _bbcode(line: DialogueLine, sp: DialogueSpeaker) -> String:
var t := line.text
if line.italic or sp.italic:
t = "[i]%s[/i]" % t
if line.bold or sp.bold:
t = "[b]%s[/b]" % t
return t
## The font slanted (italics) and / or heavier (bold), for fonts without those faces (the
## handwritten one has neither).
static func _variant(base: Font, slant: bool, bold: bool) -> Font:
var key := "%d/%s/%s" % [base.get_instance_id(), slant, bold]
if not _variants.has(key):
var v := FontVariation.new()
v.base_font = base
if slant:
v.variation_transform = Transform2D(Vector2(1.0, 0.25), Vector2(0.0, 1.0), Vector2.ZERO)
if bold:
v.variation_embolden = 0.8
_variants[key] = v
return _variants[key]
## The speaker's head takes the stage (a new speaker's rises, the last one's sinks), unless
## they're already on it; a line can change their face.
func _set_head(sp: DialogueSpeaker, face: Texture2D) -> void:
var current := _current_head()
if current != null and current.speaker == sp:
current.face = face
return
if current != null:
current.leaving = true
if face == null and sp.icon == "":
return # narration, or nobody to show
var h := Head.new()
h.speaker = sp
h.face = face
_heads.append(h)
func _current_head() -> Head:
for i in range(_heads.size() - 1, -1, -1):
if not _heads[i].leaving:
return _heads[i]
return null
func _take_input(on: bool) -> void:
mouse_filter = Control.MOUSE_FILTER_STOP if on else Control.MOUSE_FILTER_IGNORE
focus_mode = Control.FOCUS_ALL if on else Control.FOCUS_NONE # (NONE lets the focus go)
if on:
var had := get_viewport().gui_get_focus_owner()
if had != self:
_focus_before = had
grab_focus() # keys come here first: Enter doesn't press the morning sign's button under it
elif is_instance_valid(_focus_before) and _focus_before.is_visible_in_tree() and _focus_before.focus_mode != Control.FOCUS_NONE:
_focus_before.grab_focus()
_focus_before = null
func _process(delta: float) -> void:
if not visible or is_blocked():
return
_time += delta
_open = move_toward(_open, 0.0 if _closing else 1.0, delta / OPEN_TIME)
var staying: Array[Head] = []
for h in _heads:
var going := h.leaving or _closing
h.risen = move_toward(h.risen, 0.0 if going else 1.0, delta / RISE_TIME)
if h.risen > 0.0 or not going:
staying.append(h)
_heads = staying
if _closing:
if _open <= 0.0 and _heads.is_empty():
visible = false
else:
if conversation.pause_game and not has_focus():
grab_focus() # back from a menu over it
_write(delta)
_place()
## Writes letters at the speaker's speed (with a beat after a comma or a full stop), and goes
## on by itself once the line's been up long enough (Conversation.line_hold).
func _write(delta: float) -> void:
var head := _current_head()
var total := _plain.length()
if _shown >= total:
if head != null:
head.talking = false
_hold -= delta
if _hold <= 0.0:
advance()
return
if head != null:
head.talking = _wait <= 0.0
if _wait > 0.0:
_wait -= delta
return
_budget += delta * _speed
while _budget >= 1.0 and _shown < total:
_budget -= 1.0
_shown += 1
var ch := _plain[_shown - 1]
if ch != " " and ch != "\n":
_letters += 1
if _speaker.voice != null and (_letters - 1) % _speaker.voice_every == 0:
_voice.stream = _speaker.voice
_voice.pitch_scale = _speaker.voice_pitch * randf_range(0.92, 1.08)
_voice.volume_db = _speaker.voice_volume_db
_voice.play()
var beat: float = PAUSES.get(ch, 0.0)
if beat > 0.0 and (_shown == total or _plain[_shown] == " " or _plain[_shown] == "\n"):
_wait = beat
_budget = 0.0
break
_text.visible_characters = _shown if _shown < total else -1
## Lays the paper out over the hotbar (sliding up and fading in as it opens) and the stage above
## it, down to TUCK under the paper's top edge.
func _place() -> void:
var r := _sheet_rect()
var k := _ease(_open)
_sheet.position = r.position + Vector2(0.0, (1.0 - k) * 30.0)
_sheet.size = r.size
_sheet.modulate.a = k
_stage.position = Vector2.ZERO
_stage.size = Vector2(size.x, r.position.y + TUCK)
var name_h := _name.get_minimum_size().y if _name.visible else 0.0
var inner := r.size.x - 2.0 * MARGIN.x
_name.position = MARGIN
_name.size = Vector2(inner, name_h)
var top := MARGIN.y + name_h + (8.0 if _name.visible else 0.0)
_text.position = Vector2(MARGIN.x, top)
_text.size = Vector2(inner, r.size.y - top - 36.0)
_more.position = Vector2(r.size.x - 50.0 - _more.size.x, r.size.y - 30.0 - _more.size.y)
_more.visible = conversation != null and conversation.pause_game and is_written()
_stage.queue_redraw()
_more.queue_redraw()
func _sheet_rect() -> Rect2:
var w := minf(WIDTH, size.x - 2.0 * SIDE_GAP)
return Rect2((size.x - w) * 0.5, size.y - BOTTOM - HEIGHT, w, HEIGHT)
static func _ease(t: float) -> float:
return 1.0 - pow(1.0 - clampf(t, 0.0, 1.0), 3.0)
func _gui_input(event: InputEvent) -> void:
if _press(event):
accept_event()
func _unhandled_input(event: InputEvent) -> void:
if _press(event):
get_viewport().set_input_as_handled()
## A press that goes on (or skips), while the box takes input. Returns whether it was used.
func _press(event: InputEvent) -> bool:
if conversation == null or not conversation.pause_game or event.is_echo() or is_blocked():
return false
if event.is_action_pressed("hide_tips"):
skip()
return true
var mb := event as InputEventMouseButton
if (mb != null and mb.pressed and mb.button_index == MOUSE_BUTTON_LEFT) or event.is_action_pressed("ui_accept") \
or event.is_action_pressed("interact"):
advance()
return true
return false
## The sheet of paper, nailed at its top corners as on the morning sign.
func _draw_sheet() -> void:
var r := Rect2(Vector2.ZERO, _sheet.size)
Sketch.paper(_sheet, r, SEED)
for x: float in [30.0, r.size.x - 30.0]:
Sketch.nail(_sheet, Vector2(x, 26.0))
## The heads, their feet TUCK under the paper's top edge: rising or sinking, bobbing while they
## talk (their mouth flapping when they have talk frames), breathing when they're done.
func _draw_heads() -> void:
var r := _sheet_rect()
for h in _heads:
var sp := h.speaker
var face := _frame(h)
var tall := sp.portrait_height
var wide := tall * face.get_size().aspect() if face != null else tall
var x := r.position.x + HEAD_INSET if sp.side == DialogueSpeaker.HeadSide.LEFT else r.end.x - HEAD_INSET - wide
var lift := 0.0
var tilt := 0.0
var stretch := 1.0
if h.talking:
lift = absf(sin(_time * 10.0)) * 8.0
tilt = sin(_time * 5.0) * 0.03
else:
stretch = 1.0 + sin(_time * 2.2) * 0.008
var foot := Vector2(x + wide * 0.5, _stage.size.y + (1.0 - _ease(h.risen)) * (tall + 20.0) - lift) + sp.portrait_offset
if face != null:
_stage.draw_set_transform(foot, tilt, Vector2(-1.0 if sp.flip else 1.0, stretch))
_stage.draw_texture_rect(face, Rect2(-wide * 0.5, -tall, wide, tall), false)
_stage.draw_set_transform_matrix(Transform2D.IDENTITY)
else:
Sketch.icon(_stage, sp.icon, Rect2(foot - Vector2(wide * 0.5, tall * stretch), Vector2(wide, tall * stretch)))
## The head's picture now: its face, or while it talks (with its usual face) its mouth flaps.
func _frame(h: Head) -> Texture2D:
var sp := h.speaker
if not h.talking or sp.talk_frames.is_empty() or h.face != sp.portrait:
return h.face
var k := int(_time * TALK_FPS) % (sp.talk_frames.size() + 1)
return h.face if k == 0 else sp.talk_frames[k - 1]
## The pen arrow in the corner when the line is written and the player goes on.
func _draw_more() -> void:
var ink: Color = _text.get_theme_color("default_color")
var c := _more.size * 0.5 + Vector2(0.0, sin(_time * 5.0) * 4.0)
var tip := PackedVector2Array([c + Vector2(-13.0, -8.0), c + Vector2(13.0, -8.0), c + Vector2(0.0, 11.0)])
_more.draw_colored_polygon(tip, Color(ink, 0.85))
Sketch.outline(_more, tip, ink, 2.0, SEED + 9.0, 0.8, 1)

View file

@ -0,0 +1 @@
uid://cmgdtiix0rsho

View file

@ -0,0 +1,194 @@
class_name DialogueDirector
extends Node
## Plays the talking-head conversations (Conversation) at their time. Every enabled Conversation
## saved in game/resources/dialogue/ plays once, delay seconds into its night's phase (the
## morning of day 2, night 18, ...), in the DialogueBox over the HUD; several due at once play one
## after another (by delay, then file name). One that pauses the game stops the farm while it's
## up (in co-op, where the farm doesn't stop, this machine's scarecrow stands still instead). A
## retried night doesn't play its conversations again; a new or continued vacation does. The
## morning sign (ReviewPanel) waits, hidden, until the morning's conversations are done. Nothing
## starts while a menu is up (the shops, or Main's: DialogueBox.blocked) or the vacation is over,
## and a new morning (or a retry) takes down whatever was up. play() shows a conversation from
## code at any time; replay() (debug: F5) plays this phase's again.
const DIR := "res://game/resources/dialogue/"
var sim: FarmSim
var box: DialogueBox
## The conversations that play by themselves, by file name.
var conversations: Array[Conversation] = []
var _queue: Array[Conversation] = [] # this phase's, not played yet, by delay
var _asked: Array[Conversation] = [] # play() while another was up
var _played := {} # Conversation -> true, this night
var _night: int = 0
var _paused := false
var _froze := false
## Puts the box in hud_layer (over the HUD) and plays the conversations saved in dir.
func setup(p_sim: FarmSim, hud_layer: Node, dir: String = DIR) -> void:
sim = p_sim
box = DialogueBox.new()
box.name = "DialogueBox"
hud_layer.add_child(box)
box.finished.connect(_on_finished)
conversations = load_all(dir)
var c := sim.campaign
c.started.connect(func() -> void: _played.clear())
c.phase_changed.connect(_on_phase)
c.campaign_ended.connect(func(_o: CampaignController.Outcome) -> void: stop())
_on_phase(c.state.phase, c.state.phase)
## The enabled conversations saved in dir (not its subfolders), by file name.
static func load_all(dir: String) -> Array[Conversation]:
var out: Array[Conversation] = []
if not DirAccess.dir_exists_absolute(dir):
return out
var files := Array(ResourceLoader.list_directory(dir))
files.sort()
for f: String in files:
if f.ends_with("/"):
continue
var c := load(dir.path_join(f)) as Conversation # (speakers and the like aren't)
if c != null and c.enabled:
out.append(c)
return out
func is_playing() -> bool:
return box != null and box.is_playing()
## The conversations of night n's phase that haven't played (this night), by delay, then file name.
func due(n: int, phase: CampaignController.Phase) -> Array[Conversation]:
var out: Array[Conversation] = []
for c in conversations:
if c.night == n and int(c.phase) == int(phase) and not _played.has(c):
out.append(c)
var order := func(a: Conversation, b: Conversation) -> bool:
return a.delay < b.delay or (a.delay == b.delay and conversations.find(a) < conversations.find(b))
out.sort_custom(order)
return out
## Shows conversation c now, or after the one that's up.
func play(c: Conversation) -> void:
if is_playing():
_asked.append(c)
return
_played[c] = true
_hold(c.pause_game)
if box.play(c):
_sign(false)
else:
_release()
## Debug: plays this phase's conversations again, now.
func replay() -> void:
var s := sim.campaign.state
for c in conversations:
if c.night == s.night and int(c.phase) == int(s.phase):
_played.erase(c)
var again := due(s.night, s.phase)
_queue.clear()
for c in again:
play(c)
## Takes down whatever is up and forgets what was due.
func stop() -> void:
_queue.clear()
_asked.clear()
if box != null:
box.stop()
_release()
_sign(true)
## Main, back from a menu that let the game go on: a conversation that's up holds it again.
func hold() -> void:
if is_playing():
_hold(box.conversation.pause_game)
func _exit_tree() -> void:
if _paused:
get_tree().paused = false # the farm's going away with its conversation up
func _process(_delta: float) -> void:
if _queue.is_empty() or is_playing() or box.is_blocked() or sim.campaign.is_over() or sim._menu_open():
return
if _elapsed() >= _queue[0].delay:
play(_queue.pop_front())
## Seconds into the phase (INF in the untimed morning: its conversations start at once).
func _elapsed() -> float:
var c := sim.campaign
if not c.is_timed(c.state.phase):
return INF
return c.phase_length(c.state.phase) - c.state.phase_time_left
func _on_phase(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void:
var n := sim.campaign.state.night
if phase == CampaignController.Phase.MORNING:
stop() # a new day, or a retry
if n != _night:
_played.clear()
_night = n
_queue = due(n, phase)
func _on_finished(_c: Conversation) -> void:
_release()
if not _asked.is_empty():
play(_asked.pop_front())
elif not _queue.is_empty() and _elapsed() >= _queue[0].delay:
play(_queue.pop_front()) # straight on, without a tick of the farm between them
else:
_sign(true)
## The morning sign steps aside while a conversation is up, and comes back (with the focus on
## Start the day) after it.
func _sign(show: bool) -> void:
var review := sim.review_panel if sim != null else null
if review == null:
return
if show:
review.refresh()
elif review.visible:
review.visible = false
review.mouse_filter = Control.MOUSE_FILTER_IGNORE
## Stops the farm for a conversation that pauses the game: the whole tree solo, this machine's
## scarecrow in co-op.
func _hold(pause: bool) -> void:
if not pause:
return
if sim.coop != null and sim.coop.is_active():
if not sim.local_frozen:
sim.freeze_local(true)
_froze = true
else:
get_tree().paused = true
_paused = true
## Lets the farm go on, unless a menu is up (Main's pause or freeze is its own).
func _release() -> void:
var menu := box != null and box.is_blocked()
if _paused:
_paused = false
if not menu:
get_tree().paused = false
if _froze:
_froze = false
if sim.local_frozen and not menu:
sim.freeze_local(false)

View file

@ -0,0 +1 @@
uid://bcu5kh8ecg8th

View file

@ -0,0 +1,22 @@
class_name DialogueLine
extends Resource
## One thing said in a conversation (Conversation): who says it (DialogueSpeaker) and what. The
## text takes BBCode for a word or two: [i]italics[/i], [b]bold[/b], [color=#b02010]red[/color],
## [font_size=48]big[/font_size], [wave]wavy[/wave], [shake]shaky[/shake]. The look settings
## change the whole line from the speaker's.
@export var speaker: DialogueSpeaker
@export_multiline var text := ""
@export_group("Look (over the speaker's)")
## Another face for this line (an angry one, say); empty: the speaker's portrait.
@export var portrait: Texture2D
## Transparent: the speaker's text color.
@export var text_color := Color(0.0, 0.0, 0.0, 0.0)
## 0: the speaker's.
@export_range(0, 120) var font_size := 0
## The whole line in italics or bold (also when the speaker's words aren't).
@export var italic := false
@export var bold := false
## 0: the speaker's speed.
@export_range(0.0, 200.0, 1.0, "suffix:letters/s") var letters_per_second := 0.0

View file

@ -0,0 +1 @@
uid://oluompq3r367

View file

@ -0,0 +1,51 @@
class_name DialogueSpeaker
extends Resource
## Someone who talks in a conversation (Conversation, shown by DialogueBox): their name, their
## picture (the talking head, standing behind the box's top edge), the side of the box they stand
## on, and how their name and words look. Save one per character (game/resources/dialogue/
## speakers/) and use it in every line they say; a line (DialogueLine) can change the look of its
## own words, and its text takes BBCode ([i]...[/i], [color=red], ...).
enum HeadSide { LEFT, RIGHT }
## Where the words sit on the paper: on the speaker's side (as their name is), or set.
enum Align { SIDE, LEFT, CENTER, RIGHT }
@export var display_name := ""
## The talking head (a picture with a transparent background), scaled to portrait_height.
@export var portrait: Texture2D
## Optional mouth flaps: shown in turn with portrait while the words are being written.
@export var talk_frames: Array[Texture2D] = []
## Placeholder art while there's no portrait: one of Sketch.ICONS ("portrait" is the scarecrow,
## "thief" an alien, "breaker" a robot, "crow", "ufo").
@export var icon := ""
@export var side: HeadSide = HeadSide.LEFT
@export_group("Portrait")
@export_range(60.0, 800.0, 1.0, "suffix:px") var portrait_height := 360.0
## Nudges the head from its place (+x right, +y down).
@export var portrait_offset := Vector2.ZERO
## Mirrors the picture (to face the other way).
@export var flip := false
@export_group("Name")
@export var name_color := Sketch.PAPER_INK
@export_range(8, 120) var name_size := 50
@export_group("Words")
## Empty: the handwritten font of the morning sign (Sketch.font()).
@export var font: Font
@export var text_color := Sketch.PAPER_INK
@export_range(8, 120) var font_size := 36
@export var italic := false
@export var bold := false
@export var align: Align = Align.SIDE
## How fast the words are written; 0 shows each line at once.
@export_range(0.0, 200.0, 1.0, "suffix:letters/s") var letters_per_second := 45.0
@export_group("Voice")
## A short blip played as the words are written (none: silent).
@export var voice: AudioStream
@export_range(0.25, 4.0, 0.01) var voice_pitch := 1.0
@export_range(-40.0, 12.0, 0.5, "suffix:dB") var voice_volume_db := -10.0
## A blip every this many letters.
@export_range(1, 10) var voice_every := 2

View file

@ -0,0 +1 @@
uid://c1dai0qffqkmi

View file

@ -24,7 +24,8 @@ extends Node
## simulated there, the host's state is applied instead (replica = true), and player, scarecrow,
## combat, and interactions are that machine's own scarecrow's (player 2's).
## FarmAudio plays every sound, Tutorial shows the first
## day's and night's tips, and FarmersReturn plays the ending. Debug: T / G / H send in a
## day's and night's tips, DialogueDirector the talking-head conversations (game/resources/
## dialogue/), and FarmersReturn plays the ending. Debug: T / G / H send in a
## thief / a crow / a fence breaker at any time.
const SCARECROW_RADIUS := 0.4
@ -83,6 +84,7 @@ var replica_step: Callable
var coop: CoopSession
var audio: FarmAudio
var tutorial: Tutorial
var dialogue: DialogueDirector
var ending: FarmersReturn
## Flow-field rebuild after a structure change: running, and stats of the last one.
var rebuilding := false
@ -320,6 +322,10 @@ func _ready() -> void:
tutorial.name = "Tutorial"
hud.get_parent().add_child(tutorial)
tutorial.setup(self)
dialogue = DialogueDirector.new()
dialogue.name = "Dialogue"
add_child(dialogue)
dialogue.setup(self, hud.get_parent()) # its box over the tips and the morning sign
if end_panel != null:
end_panel.get_parent().move_child(end_panel, -1) # over the banners and the tips
audio = FarmAudio.new()

View file

@ -63,6 +63,8 @@ func _ready() -> void:
sim.end_panel.menu_requested.connect(to_menu)
if sim.tutorial != null:
sim.tutorial.enabled = settings.show_tips
if sim.dialogue != null:
sim.dialogue.box.blocked = func() -> bool: return screen != Screen.PLAYING
main_menu.continue_requested.connect(continue_vacation)
main_menu.new_requested.connect(_ask_new_vacation)
main_menu.settings_requested.connect(_open_settings)
@ -188,6 +190,8 @@ func resume() -> void:
get_tree().paused = false
if sim.local_frozen:
sim.freeze_local(false)
if sim.dialogue != null:
sim.dialogue.hold() # a conversation that was up stops the farm again
func restart_night() -> void:

View file

@ -92,7 +92,8 @@ func _process(delta: float) -> void:
if sim == null:
return
var s := sim.campaign.state
if not enabled or dismissed or s.night != 1 or sim.campaign.is_over() or sim._menu_open():
if not enabled or dismissed or s.night != 1 or sim.campaign.is_over() or sim._menu_open() \
or (sim.dialogue != null and sim.dialogue.is_playing()):
_show("")
return
_check_done()