using System;
using System.Collections.Generic;
using UnityEngine;
namespace Jovian.EncounterSystem {
/// One dialog line — an id/text pair in a .
[Serializable]
public class DialogLine {
/// Stable key referenced by (e.g. "common.farewell").
public string id;
/// The actual text shown to the player.
[TextArea(2, 6)]
public string text;
}
///
/// Single-asset registry of reusable dialog lines. One library file holds many lines; dialog
/// options reference them by id via . Split into multiple libraries
/// (e.g. CommonLines, TownDialogue) only when a single file becomes unwieldy.
///
[CreateAssetMenu(fileName = "DialogLineLibrary", menuName = "Jovian/Encounter System/Dialog Line Library", order = 4)]
public class DialogLineLibrary : ScriptableObject {
/// All lines in the library.
public List lines = new();
private Dictionary cache;
/// Return the text for , or null if not found.
public string Resolve(string id) {
if(string.IsNullOrEmpty(id)) {
return null;
}
EnsureCache();
return cache.TryGetValue(id, out var text) ? text : null;
}
/// Force the next call to rebuild the id → text cache.
/// Called automatically from OnValidate after inspector edits.
public void InvalidateCache() {
cache = null;
}
private void EnsureCache() {
if(cache != null) {
return;
}
cache = new Dictionary();
if(lines == null) {
return;
}
foreach(var line in lines) {
if(line != null && !string.IsNullOrEmpty(line.id)) {
cache[line.id] = line.text;
}
}
}
#if UNITY_EDITOR
private void OnValidate() {
InvalidateCache();
}
#endif
}
}