first
This commit is contained in:
65
Assets/02_Scripts/Story/StoryState.cs
Normal file
65
Assets/02_Scripts/Story/StoryState.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
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 void Clear()
|
||||
{
|
||||
MainProgress = 0;
|
||||
Affection.Clear();
|
||||
CompletedDialogs.Clear();
|
||||
ChosenCodes.Clear();
|
||||
Triggers.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 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);
|
||||
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>());
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user