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