DEG-24 [Refactor] 세이브 데이터 적용 및 불러오기 개선

SaveDataController가 각 필드의 데이터를 모으고, 적용한다.
This commit is contained in:
99jamin 2025-04-24 14:30:00 +09:00
parent 39abb506da
commit a3328c7724
6 changed files with 175 additions and 19 deletions

View File

@ -0,0 +1,31 @@
/// <summary>
/// 세이브 데이터를 주고 받는 함수 인터페이스
/// </summary>
public interface ISaveable
{
void ApplySaveData(Save save);
Save ExtractSaveData();
}
// 인터페이스 사용예시
//
// public void ApplySaveData(Save save)
// {
// if (save?.homeSave != null)
// {
// mealCount = save.homeSave.mealCount;
// }
// }
//
// public Save ExtractSaveData()
// {
// // 자신이 책임지는 부분만 채움, 나머지는 null로 둠
// return new Save
// {
// homeSave = new HomeSave
// {
// mealCount = mealCount
// }
// };
// }

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b8b6bcb2e37dea949b70a8a96f96e44b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -6,14 +6,27 @@ using UnityEngine;
public class DungeonSave public class DungeonSave
{ {
// 강화 수치 // 강화 수치
public int attackLevel; public int attackLevel = 0;
public int attackSpeedLevel; public int attackSpeedLevel = 0;
public int heartLevel; public int heartLevel = 0;
public int moveSpeedLevel; public int moveSpeedLevel = 0;
public int evasionTimeLevel; public int evasionTimeLevel = 0;
// 현재 진행 중인 스테이지 // 현재 진행 중인 스테이지
public int stageLevel; public int stageLevel = 0;
//병합을 위한 메서드
public void MergeWith(DungeonSave other)
{
if (other == null) return;
if (other.attackLevel != 0) attackLevel = other.attackLevel;
if (other.attackSpeedLevel != 0) attackSpeedLevel = other.attackSpeedLevel;
if (other.heartLevel != 0) heartLevel = other.heartLevel;
if (other.moveSpeedLevel != 0) moveSpeedLevel = other.moveSpeedLevel;
if (other.evasionTimeLevel != 0) evasionTimeLevel = other.evasionTimeLevel;
if (other.stageLevel != 0) stageLevel = other.stageLevel;
}
} }
// 일상(자취방) 관련 저장 데이터 // 일상(자취방) 관련 저장 데이터
@ -21,17 +34,30 @@ public class DungeonSave
public class HomeSave public class HomeSave
{ {
// 일상 시간 // 일상 시간
public float time; public int currentDay = 0;
public int day; public float time = 999;
// 체력 및 평판 수치 // 체력 및 평판 수치
public float health; public float health = 999;
public float reputation; public float reputation = 999;
//이벤트 //이벤트
public bool isEvent; public int mealCount = 999;
public int mealCount; public int houseworkCount = 999;
public int houseworkCount;
//병합을 위한 메서드
public void MergeWith(HomeSave other)
{
if (other == null) return;
if (other.currentDay != 0) currentDay = other.currentDay;
if (other.time < 999) time = other.time;
if (other.health < 999) health = other.health;
if (other.reputation < 999) reputation = other.reputation;
if (other.mealCount < 999) mealCount = other.mealCount;
if (other.houseworkCount < 999) houseworkCount = other.houseworkCount;
}
} }
// 게임 전체 저장 구조 // 게임 전체 저장 구조

View File

@ -0,0 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEngine;
public class SaveDataController : MonoBehaviour
{
[SerializeField] private ISaveable[] saveables;
private Save tmpSave;
void Awake()
{
tmpSave = new Save();
if (saveables == null || saveables.Length == 0)
saveables = FindObjectsOfType<MonoBehaviour>().OfType<ISaveable>().ToArray();
}
//세이브를 현재 상태에 적용
public void ApplySaveData(Save saveData)
{
GetSaveDataFromManager(saveData);
SendSaveDataToClass();
}
//현재 상태를 세이브에 적용
public Save GetSaveData()
{
UpdateSaveData();
return tmpSave;
}
//로컬에서 로드한 세이브 데이터를 받아오는 함수.
private void GetSaveDataFromManager(Save saveData)
{
tmpSave = saveData;
}
//세이브 데이터를 각클래스에게 적용하는 함수.
private void SendSaveDataToClass()
{
foreach (var s in saveables)
{
s.ApplySaveData(tmpSave);
}
}
//각 클래스에게서 현재 정보를 받아오는 함수.
private void UpdateSaveData()
{
foreach (var s in saveables)
{
Save partial = s.ExtractSaveData();
MergeSave(ref tmpSave, partial);
}
}
private void MergeSave(ref Save baseSave, Save newSave)
{
if (newSave == null) return;
if (newSave.dungeonSave != null)
{
if (baseSave.dungeonSave == null)
baseSave.dungeonSave = new DungeonSave();
baseSave.dungeonSave.MergeWith(newSave.dungeonSave);
}
if (newSave.homeSave != null)
{
if (baseSave.homeSave == null)
baseSave.homeSave = new HomeSave();
baseSave.homeSave.MergeWith(newSave.homeSave);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2e5de90e465e1ed4f91e2e6f11e17273
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,7 +20,7 @@ public class StatManager : MonoBehaviour
public int stageLevel; public int stageLevel;
public float time; public float time;
public int day; public int currentDay;
public float health; public float health;
public float reputation; public float reputation;
@ -53,7 +53,7 @@ public class StatManager : MonoBehaviour
time = floatValue; time = floatValue;
day = intValue; currentDay = intValue;
health = floatValue; health = floatValue;
reputation = floatValue; reputation = floatValue;
@ -85,10 +85,9 @@ public class StatManager : MonoBehaviour
homeSave = new HomeSave homeSave = new HomeSave
{ {
time = this.time, time = this.time,
day = this.day, currentDay = this.currentDay,
health = this.health, health = this.health,
reputation = this.reputation, reputation = this.reputation,
isEvent = false,
mealCount = this.mealCount, mealCount = this.mealCount,
houseworkCount = this.houseworkCount, houseworkCount = this.houseworkCount,
} }
@ -105,10 +104,9 @@ public class StatManager : MonoBehaviour
stageLevel = saveData.dungeonSave.stageLevel; stageLevel = saveData.dungeonSave.stageLevel;
time = saveData.homeSave.time; time = saveData.homeSave.time;
day = saveData.homeSave.day; currentDay = saveData.homeSave.currentDay;
health = saveData.homeSave.health; health = saveData.homeSave.health;
reputation = saveData.homeSave.reputation; reputation = saveData.homeSave.reputation;
isEvent = saveData.homeSave.isEvent;
mealCount = saveData.homeSave.mealCount; mealCount = saveData.homeSave.mealCount;
houseworkCount = saveData.homeSave.houseworkCount; houseworkCount = saveData.homeSave.houseworkCount;
} }