71 lines
2.9 KiB
C#
71 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// 이야기 진행 상태 데이터 (메인 진행도 / 호감도 / 대화 이력 / 선택 이력).
|
|
// 상태 보관과 JSON 변환만 담당한다 — 게임 로직에서는 StoryManager를 통해 접근할 것.
|
|
public class StoryState
|
|
{
|
|
public int MainProgress;
|
|
public readonly Dictionary<string, int> Affection = new(); // 캐릭터 Id → 호감도
|
|
public readonly HashSet<string> CompletedDialogs = new(); // 완료한 DialogGroup 이름
|
|
public readonly HashSet<string> ChosenCodes = new(); // 골랐던 선택지 Code
|
|
public readonly HashSet<string> Triggers = new(); // 켜진 트리거 Id (대화 조건 검사용)
|
|
public readonly List<string> Evidence = new(); // 가진 증거품 항목 Id (획득 순서 유지 = 표시 순서)
|
|
|
|
public void Clear()
|
|
{
|
|
MainProgress = 0;
|
|
Affection.Clear();
|
|
CompletedDialogs.Clear();
|
|
ChosenCodes.Clear();
|
|
Triggers.Clear();
|
|
Evidence.Clear();
|
|
}
|
|
|
|
// ── JSON 변환 ────────────────────────────────────────────────
|
|
// JsonUtility가 Dictionary/HashSet을 직렬화하지 못해 리스트로 바꿔 저장한다.
|
|
[Serializable]
|
|
private class JsonData
|
|
{
|
|
public int MainProgress;
|
|
public List<string> AffectionIds = new();
|
|
public List<int> AffectionValues = new();
|
|
public List<string> CompletedDialogs = new();
|
|
public List<string> ChosenCodes = new();
|
|
public List<string> Triggers = new();
|
|
public List<string> Evidence = new();
|
|
}
|
|
|
|
public string ToJson()
|
|
{
|
|
var data = new JsonData { MainProgress = MainProgress };
|
|
foreach (var kvp in Affection)
|
|
{
|
|
data.AffectionIds.Add(kvp.Key);
|
|
data.AffectionValues.Add(kvp.Value);
|
|
}
|
|
data.CompletedDialogs.AddRange(CompletedDialogs);
|
|
data.ChosenCodes.AddRange(ChosenCodes);
|
|
data.Triggers.AddRange(Triggers);
|
|
data.Evidence.AddRange(Evidence);
|
|
return JsonUtility.ToJson(data, prettyPrint: true);
|
|
}
|
|
|
|
// json이 유효하지 않으면 null 반환
|
|
public static StoryState FromJson(string json)
|
|
{
|
|
var data = JsonUtility.FromJson<JsonData>(json);
|
|
if (data == null) return null;
|
|
|
|
var state = new StoryState { MainProgress = data.MainProgress };
|
|
for (int i = 0; i < data.AffectionIds.Count && i < data.AffectionValues.Count; i++)
|
|
state.Affection[data.AffectionIds[i]] = data.AffectionValues[i];
|
|
state.CompletedDialogs.UnionWith(data.CompletedDialogs);
|
|
state.ChosenCodes.UnionWith(data.ChosenCodes);
|
|
state.Triggers.UnionWith(data.Triggers ?? new List<string>());
|
|
state.Evidence.AddRange(data.Evidence ?? new List<string>());
|
|
return state;
|
|
}
|
|
}
|