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