forked from Shardstone/trail-into-darkness
added encounter system
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Jovian.EncounterSystem {
|
||||
[Serializable]
|
||||
public class DialogLine {
|
||||
public string id;
|
||||
[TextArea(2, 6)] public string text;
|
||||
}
|
||||
|
||||
/// <summary>Flat registry of reusable dialog lines. Referenced via <see cref="DialogLineRef"/>.</summary>
|
||||
[CreateAssetMenu(fileName = "DialogLineLibrary", menuName = "Jovian/Encounter System/Dialog Line Library", order = 4)]
|
||||
public class DialogLineLibrary : ScriptableObject {
|
||||
public List<DialogLine> lines = new();
|
||||
|
||||
private Dictionary<string, string> cache;
|
||||
|
||||
public string Resolve(string id) {
|
||||
if(string.IsNullOrEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
EnsureCache();
|
||||
return cache.TryGetValue(id, out var text) ? text : null;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user