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. ## The opening intro (OpeningIntro) holds all of it: nothing starts while it's on screen (play() ## waits) and the sign stays down while it has the farm; at its end (intro_over()) night 1's ## morning conversations play, then the sign comes up. 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 (or the opening intro). func play(c: Conversation) -> void: if is_playing() or _intro_busy(): _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) ## Whether a conversation would start now (the intro keeps the sign down for it). func has_due() -> bool: return not _asked.is_empty() or (not _queue.is_empty() and _elapsed() >= _queue[0].delay) ## The opening intro is over: this morning's conversations now, then the sign. func intro_over() -> void: if not is_playing(): _next() ## 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() or _intro_busy(): 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() _next() ## The next conversation (asked for, or due), or the sign. func _next() -> void: 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 (not while the opening intro has the farm). func _sign(show: bool) -> void: var review := sim.review_panel if sim != null else null if review == null or (show and sim.intro != null and sim.intro.running): return if show: review.refresh() elif review.visible: review.visible = false review.mouse_filter = Control.MOUSE_FILTER_IGNORE func _intro_busy() -> bool: return sim != null and sim.intro != null and sim.intro.busy() ## 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)