From 2166ea51ef1313ce03a3e3bc79299131f999c1e1 Mon Sep 17 00:00:00 2001 From: Wickedness Date: Thu, 18 Jun 2026 03:38:45 +0900 Subject: [PATCH] Add dialogue portrait slots --- README.md | 1 + docs/ARCHITECTURE.md | 2 +- docs/DATA_MODEL.md | 4 +- docs/ROADMAP.md | 2 +- scripts/core/battle_state.gd | 5 ++- scripts/scenes/battle_scene.gd | 81 +++++++++++++++++++++++++++++++--- tools/validate_data.ps1 | 13 ++++++ 7 files changed, 97 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5a681df..a254aa8 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Godot 4 tactical RPG prototype inspired by classic turn-based Romance of the Thr - Campaign completion screen. - New campaign save reset. - Victory and defeat result overlay. +- Dialogue portrait slot with optional image loading and speaker-initial fallback. - Placeholder menu/battle BGM plus UI, movement, attack, tactic, item, victory, and defeat SFX. - Floating combat text for damage, recovery, misses, support effects, level-ups, and promotions. - Short unit slide animation for board movement. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index a1caa7d..01701e7 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -58,6 +58,6 @@ Joined officers gate whether `requires_joined` player deployments are loaded at Inventory and campaign flags are copied from `CampaignState` into `BattleState` when a scenario starts. Pre-battle shop stock comes from the loaded scenario's `shop.items` list plus matching `shop.conditional_items` blocks. Shop purchases and 50% sell-back are campaign transactions: they update saved gold and unequipped inventory immediately, write the save file, and refresh the already-loaded battle inventory before the player begins the battle. Pre-battle Armory equipment changes use the same equipment rules as battle HUD equipment changes, then immediately save merged roster equipment/stat snapshots and inventory stock. Pre-battle Roster uses scenario `roster.max_units`, `roster.required_officers`, and `roster.required_units` to mark optional player deployments as sortie or reserve; reserve units remain in the candidate list but are excluded from board occupancy, drawing, selection, AI targeting, and living-unit victory checks. Non-controllable player units can act as protected escort targets: enemies can attack them and conditions can reference them, but they are skipped by player selection, Armory, Formation, counterattacks, and campaign roster progression snapshots when `persist_progression` is false. Scenario `ai_target_priority` nudges enemy movement and damage-skill scoring toward important targets such as envoys without overriding defeat bonuses or range checks. Pre-battle Formation uses the loaded scenario's `formation.cells` and only mutates current battle unit positions before battle-begin events fire. Destination victory cells from `unit_reaches_tile` conditions are exposed to the scene for objective overlays and tile info. Movement-triggered `unit_reaches_tile` events receive the unit that just moved, so ambushes fire on entry rather than from units already standing on a marker. Briefing data can append matching `briefing.conditional_lines`, and scenario events can use `when.campaign_flags` to branch dialogue, objective changes, and reinforcements from saved campaign choices. Consumable use can restore HP or MP, and in-battle item use plus equipment swaps mutate only the battle copy until victory; defeats and restarts do not spend saved items or preserve in-battle gear changes. Post-battle choices write campaign `flags` only after the result-panel button is pressed; if the player reloads after rewards are saved but before selecting, the pending scenario id restores the victory choice panel without replaying rewards. The next battle button stays locked until the campaign choice save succeeds. Equipment rewards share the same `item_id -> count` inventory stock, cover weapon, armor, and accessory slots, can be equipped from the side HUD before the selected unit moves or acts, and do not appear in the consumable action menu. Already completed scenarios can be replayed without granting duplicate rewards, choices, or replay inventory consumption; pre-battle Shop, Armory, Roster, and Formation are disabled on completed-scenario replays to avoid side effects. -The battle scene owns presentation feedback: briefing and result states use menu BGM, active battles use battle BGM, board units render low-HP warning rings and HP bar color states from battle state fields, hover previews are rendered as target badges from existing `BattleState` forecast APIs, log/result hooks trigger placeholder SFX, `BattleState.unit_motion_requested` asks the scene to interpolate a unit's draw position during movement, and `BattleState.combat_feedback_requested` asks the scene to draw transient floating combat text without changing battle rules. +The battle scene owns presentation feedback: briefing and result states use menu BGM, active battles use battle BGM, dialogue lines can show optional portrait textures or speaker-initial fallback panels, board units render low-HP warning rings and HP bar color states from battle state fields, hover previews are rendered as target badges from existing `BattleState` forecast APIs, log/result hooks trigger placeholder SFX, `BattleState.unit_motion_requested` asks the scene to interpolate a unit's draw position during movement, and `BattleState.combat_feedback_requested` asks the scene to draw transient floating combat text without changing battle rules. The `New Campaign` button clears `user://campaign_save.json`, resets campaign state to the first scenario, and reloads the opening briefing. diff --git a/docs/DATA_MODEL.md b/docs/DATA_MODEL.md index 8d539a5..00d8db0 100644 --- a/docs/DATA_MODEL.md +++ b/docs/DATA_MODEL.md @@ -392,7 +392,7 @@ Conditions may include `after_event` to stay inactive until a one-shot event has `unit_reaches_tile` checks living deployed units against a zero-based `pos` coordinate. Use `officer_ids` for persistent named officers or `unit_ids` for scenario-specific units; the validator requires one of those filters so destination objectives do not accidentally trigger from any unit. Victory destination cells are drawn as objective markers on the battle map. -`post_battle_dialogue` plays once after victory and before the victory result panel opens. Lines may be plain strings or objects with `speaker` and non-empty `text`. +`post_battle_dialogue` plays once after victory and before the victory result panel opens. Lines may be plain strings or objects with `speaker`, non-empty `text`, and optional `portrait` resource paths such as `res://art/portraits/cao_cao.png`. If no usable portrait is loaded, the battle scene falls back to a speaker-initial panel. `post_battle_choices` are shown on the victory result panel after first-time scenario rewards are applied. Each choice needs a unique stable lowercase `id`, a non-empty `label`, and a `set_flags` object whose keys are stable lowercase flag ids. Choices may also grant positive `gold`, known `items`, `join_officers`, and `leave_officers`. When rewards are saved but the player has not selected a choice yet, `CampaignState.pending_post_battle_choice_scenario_id` records that scenario id so reloading the game returns to the victory choice panel instead of skipping the branch. The selected choice is saved to `CampaignState.flags`, `CampaignState.applied_post_battle_choices`, and campaign membership only when the player presses its result-panel button; completed-scenario replays do not show choices again. Save-load migration for older saves uses flags as a conservative choice signal, so avoid designing multiple choices in one scenario that can all match the same saved flag state. @@ -413,7 +413,7 @@ Scenario `events` support `battle_start`, `battle_begin`, `turn_start`, and move { "type": "dialogue", "lines": [ - { "speaker": "Cao Cao", "text": "Hold formation." } + { "speaker": "Cao Cao", "portrait": "res://art/portraits/cao_cao.png", "text": "Hold formation." } ] }, { "type": "set_objective", "victory": "Defeat the vanguard and reinforcements." }, diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 31548bc..d620a7c 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -130,7 +130,7 @@ ## Milestone 5: Presentation -- Portrait pipeline. +- Portrait pipeline. Dialogue lines can carry optional portrait image paths, with speaker-initial fallback when art is missing. - Unit sprites and animations. Board movement now has a short slide animation, and board units show low-HP warning rings. - Battle effects. First floating combat text now covers damage, recovery, misses, support effects, level-ups, and promotions. Hover target preview badges summarize attacks, tactics, and items. - Music and sound. Basic placeholder BGM and SFX now play for menus, battle flow, unit actions, tactics, items, and result states. diff --git a/scripts/core/battle_state.gd b/scripts/core/battle_state.gd index a994995..38c307c 100644 --- a/scripts/core/battle_state.gd +++ b/scripts/core/battle_state.gd @@ -2487,14 +2487,15 @@ func _normalize_dialogue_line(line) -> Dictionary: var text := str(line) if text.is_empty(): return {} - return {"speaker": "", "text": text} + return {"speaker": "", "text": text, "portrait": ""} if typeof(line) == TYPE_DICTIONARY: var text := str(line.get("text", "")) if text.is_empty(): return {} return { "speaker": str(line.get("speaker", "")), - "text": text + "text": text, + "portrait": str(line.get("portrait", "")) } return {} diff --git a/scripts/scenes/battle_scene.gd b/scripts/scenes/battle_scene.gd index 43d1d3d..391cb2c 100644 --- a/scripts/scenes/battle_scene.gd +++ b/scripts/scenes/battle_scene.gd @@ -84,6 +84,9 @@ var formation_list: VBoxContainer var formation_status_label: Label var formation_unit_id := "" var dialogue_panel: PanelContainer +var dialogue_portrait_panel: PanelContainer +var dialogue_portrait_texture: TextureRect +var dialogue_portrait_label: Label var dialogue_speaker_label: Label var dialogue_text_label: Label var dialogue_continue_button: Button @@ -447,17 +450,42 @@ func _create_hud() -> void: dialogue_panel.size = Vector2(760, 140) root.add_child(dialogue_panel) + var dialogue_row := HBoxContainer.new() + dialogue_row.add_theme_constant_override("separation", 14) + dialogue_panel.add_child(dialogue_row) + + dialogue_portrait_panel = PanelContainer.new() + dialogue_portrait_panel.custom_minimum_size = Vector2(96, 112) + dialogue_row.add_child(dialogue_portrait_panel) + + var dialogue_portrait_stack := Control.new() + dialogue_portrait_stack.custom_minimum_size = Vector2(96, 112) + dialogue_portrait_panel.add_child(dialogue_portrait_stack) + + dialogue_portrait_texture = TextureRect.new() + dialogue_portrait_texture.set_anchors_preset(Control.PRESET_FULL_RECT) + dialogue_portrait_texture.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED + dialogue_portrait_stack.add_child(dialogue_portrait_texture) + + dialogue_portrait_label = Label.new() + dialogue_portrait_label.set_anchors_preset(Control.PRESET_FULL_RECT) + dialogue_portrait_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + dialogue_portrait_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER + dialogue_portrait_label.add_theme_font_size_override("font_size", 22) + dialogue_portrait_stack.add_child(dialogue_portrait_label) + var dialogue_column := VBoxContainer.new() + dialogue_column.custom_minimum_size = Vector2(620, 112) dialogue_column.add_theme_constant_override("separation", 8) - dialogue_panel.add_child(dialogue_column) + dialogue_row.add_child(dialogue_column) dialogue_speaker_label = Label.new() - dialogue_speaker_label.custom_minimum_size = Vector2(720, 22) + dialogue_speaker_label.custom_minimum_size = Vector2(600, 22) dialogue_column.add_child(dialogue_speaker_label) dialogue_text_label = Label.new() dialogue_text_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART - dialogue_text_label.custom_minimum_size = Vector2(720, 56) + dialogue_text_label.custom_minimum_size = Vector2(600, 56) dialogue_column.add_child(dialogue_text_label) dialogue_continue_button = Button.new() @@ -1486,13 +1514,14 @@ func _normalized_dialogue_lines(lines: Array) -> Array: continue result.append({ "speaker": str(line.get("speaker", "")), - "text": text + "text": text, + "portrait": str(line.get("portrait", "")) }) elif typeof(line) == TYPE_STRING: var text := str(line) if text.is_empty(): continue - result.append({"speaker": "", "text": text}) + result.append({"speaker": "", "text": text, "portrait": ""}) return result @@ -1533,12 +1562,54 @@ func _render_dialogue_line() -> void: dialogue_speaker_label.text = speaker dialogue_speaker_label.visible = not speaker.is_empty() dialogue_text_label.text = str(line.get("text", "")) + _update_dialogue_portrait(speaker, str(line.get("portrait", ""))) dialogue_continue_button.text = "Close" if active_dialogue_index >= active_dialogue_lines.size() - 1 else "Next" dialogue_panel.visible = true _update_hud() queue_redraw() +func _update_dialogue_portrait(speaker: String, portrait_path: String) -> void: + if dialogue_portrait_panel == null: + return + var texture := _load_dialogue_portrait(portrait_path) + var has_portrait := texture != null + var has_placeholder := not speaker.is_empty() + dialogue_portrait_panel.visible = has_portrait or has_placeholder + dialogue_portrait_texture.texture = texture + dialogue_portrait_texture.visible = has_portrait + dialogue_portrait_label.text = _dialogue_portrait_initials(speaker) + dialogue_portrait_label.visible = not has_portrait and has_placeholder + + +func _load_dialogue_portrait(portrait_path: String) -> Texture2D: + var normalized_path := portrait_path.strip_edges() + if normalized_path.is_empty(): + return null + if not normalized_path.begins_with("res://") and not normalized_path.begins_with("user://"): + normalized_path = "res://%s" % normalized_path + if not ResourceLoader.exists(normalized_path, "Texture2D"): + return null + return load(normalized_path) as Texture2D + + +func _dialogue_portrait_initials(speaker: String) -> String: + var normalized_speaker := speaker.strip_edges() + if normalized_speaker.is_empty(): + return "" + var parts := normalized_speaker.split(" ", false) + var initials := "" + for part in parts: + if str(part).is_empty(): + continue + initials += str(part).substr(0, 1).to_upper() + if initials.length() >= 2: + break + if initials.is_empty(): + return normalized_speaker.substr(0, 1).to_upper() + return initials + + func _advance_dialogue() -> void: if not _is_dialogue_visible(): return diff --git a/tools/validate_data.ps1 b/tools/validate_data.ps1 index 9db2128..d4bf4cc 100644 --- a/tools/validate_data.ps1 +++ b/tools/validate_data.ps1 @@ -1203,6 +1203,19 @@ function Check-Dialogue-Line($Line, [string]$Context) { if ([string]::IsNullOrWhiteSpace([string](Get-Prop $Line "text" ""))) { Fail "$Context has an empty dialogue text." } + if (Has-Prop $Line "portrait") { + $portrait = Get-Prop $Line "portrait" "" + if ($portrait -is [System.Array] -or $portrait -is [System.Management.Automation.PSCustomObject]) { + Fail "$Context portrait must be a string resource path." + } + if ([string]::IsNullOrWhiteSpace([string]$portrait)) { + Fail "$Context has an empty portrait path." + } + $portraitText = [string]$portrait + if ($portraitText -notmatch '^res://.+\.(png|jpg|jpeg|webp)$') { + Fail "$Context portrait must be a res:// image path." + } + } } function Check-Post-Battle-Dialogue($Dialogue, [string]$ScenarioId) {