diff --git a/Assets/KJM/KJM_Test/ISaveable.cs b/Assets/KJM/KJM_Test/ISaveable.cs new file mode 100644 index 00000000..5c98ca97 --- /dev/null +++ b/Assets/KJM/KJM_Test/ISaveable.cs @@ -0,0 +1,31 @@ +/// +/// 세이브 데이터를 주고 받는 함수 인터페이스 +/// +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 +// } +// }; +// } diff --git a/Assets/KJM/KJM_Test/ISaveable.cs.meta b/Assets/KJM/KJM_Test/ISaveable.cs.meta new file mode 100644 index 00000000..d61a3136 --- /dev/null +++ b/Assets/KJM/KJM_Test/ISaveable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b8b6bcb2e37dea949b70a8a96f96e44b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KJM/KJM_Test/Save.cs b/Assets/KJM/KJM_Test/Save.cs index 5687fb38..0cab35af 100644 --- a/Assets/KJM/KJM_Test/Save.cs +++ b/Assets/KJM/KJM_Test/Save.cs @@ -6,14 +6,27 @@ using UnityEngine; public class DungeonSave { // 강화 수치 - public int attackLevel; - public int attackSpeedLevel; - public int heartLevel; - public int moveSpeedLevel; - public int evasionTimeLevel; + public int attackLevel = 0; + public int attackSpeedLevel = 0; + public int heartLevel = 0; + public int moveSpeedLevel = 0; + 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 float time; - public int day; + public int currentDay = 0; + public float time = 999; + // 체력 및 평판 수치 - public float health; - public float reputation; + public float health = 999; + public float reputation = 999; //이벤트 - public bool isEvent; - public int mealCount; - public int houseworkCount; + public int mealCount = 999; + public int houseworkCount = 999; + + //병합을 위한 메서드 + 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; + } } // 게임 전체 저장 구조 diff --git a/Assets/KJM/KJM_Test/SaveDataController.cs b/Assets/KJM/KJM_Test/SaveDataController.cs new file mode 100644 index 00000000..f380d64b --- /dev/null +++ b/Assets/KJM/KJM_Test/SaveDataController.cs @@ -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().OfType().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); + } + } +} diff --git a/Assets/KJM/KJM_Test/SaveDataController.cs.meta b/Assets/KJM/KJM_Test/SaveDataController.cs.meta new file mode 100644 index 00000000..895cec8c --- /dev/null +++ b/Assets/KJM/KJM_Test/SaveDataController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2e5de90e465e1ed4f91e2e6f11e17273 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KJM/KJM_Test/StatManager.cs b/Assets/KJM/KJM_Test/StatManager.cs index b0129abc..d00d5e68 100644 --- a/Assets/KJM/KJM_Test/StatManager.cs +++ b/Assets/KJM/KJM_Test/StatManager.cs @@ -20,7 +20,7 @@ public class StatManager : MonoBehaviour public int stageLevel; public float time; - public int day; + public int currentDay; public float health; public float reputation; @@ -53,7 +53,7 @@ public class StatManager : MonoBehaviour time = floatValue; - day = intValue; + currentDay = intValue; health = floatValue; reputation = floatValue; @@ -85,10 +85,9 @@ public class StatManager : MonoBehaviour homeSave = new HomeSave { time = this.time, - day = this.day, + currentDay = this.currentDay, health = this.health, reputation = this.reputation, - isEvent = false, mealCount = this.mealCount, houseworkCount = this.houseworkCount, } @@ -105,10 +104,9 @@ public class StatManager : MonoBehaviour stageLevel = saveData.dungeonSave.stageLevel; time = saveData.homeSave.time; - day = saveData.homeSave.day; + currentDay = saveData.homeSave.currentDay; health = saveData.homeSave.health; reputation = saveData.homeSave.reputation; - isEvent = saveData.homeSave.isEvent; mealCount = saveData.homeSave.mealCount; houseworkCount = saveData.homeSave.houseworkCount; }