From a3328c7724294e25a33010bf13d5b032cc20c8b8 Mon Sep 17 00:00:00 2001 From: 99jamin <99jamin56@gmail.com> Date: Thu, 24 Apr 2025 14:30:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?DEG-24=20[Refactor]=20=EC=84=B8=EC=9D=B4?= =?UTF-8?q?=EB=B8=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?=EB=B0=8F=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SaveDataController가 각 필드의 데이터를 모으고, 적용한다. --- Assets/KJM/KJM_Test/ISaveable.cs | 31 ++++++++ Assets/KJM/KJM_Test/ISaveable.cs.meta | 11 +++ Assets/KJM/KJM_Test/Save.cs | 52 +++++++++--- Assets/KJM/KJM_Test/SaveDataController.cs | 79 +++++++++++++++++++ .../KJM/KJM_Test/SaveDataController.cs.meta | 11 +++ Assets/KJM/KJM_Test/StatManager.cs | 10 +-- 6 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 Assets/KJM/KJM_Test/ISaveable.cs create mode 100644 Assets/KJM/KJM_Test/ISaveable.cs.meta create mode 100644 Assets/KJM/KJM_Test/SaveDataController.cs create mode 100644 Assets/KJM/KJM_Test/SaveDataController.cs.meta 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; } From 67410c08607777a5942303dc82b31f7f8bca6a33 Mon Sep 17 00:00:00 2001 From: 99jamin <99jamin56@gmail.com> Date: Thu, 24 Apr 2025 15:39:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?DEG-24=20[Test]=20=EC=9D=B8=ED=84=B0?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=82=AC=EC=9A=A9=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/KJM/KJM.unity | 4 +- Assets/KJM/KJM_Test/ISaveable.cs | 23 ---- Assets/KJM/KJM_Test/SaveDataController.cs | 5 +- Assets/KJM/KJM_Test/SaveManager.cs | 11 +- Assets/KJM/KJM_Test/StatManager.cs | 118 ----------------- Assets/KJM/KJM_Test/TestScript.cs | 122 ++++++++++++++++++ ...StatManager.cs.meta => TestScript.cs.meta} | 0 7 files changed, 134 insertions(+), 149 deletions(-) delete mode 100644 Assets/KJM/KJM_Test/StatManager.cs create mode 100644 Assets/KJM/KJM_Test/TestScript.cs rename Assets/KJM/KJM_Test/{StatManager.cs.meta => TestScript.cs.meta} (100%) diff --git a/Assets/KJM/KJM.unity b/Assets/KJM/KJM.unity index 0d91b2f4..09606b0d 100644 --- a/Assets/KJM/KJM.unity +++ b/Assets/KJM/KJM.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7f13f719e71b8e944fc097d5a437f5e135e094a1cf536cc80881327f9a5dbe59 -size 48687 +oid sha256:72074771a04b1e569bf2994ccbbf0fa4598b680121664c502cf750c8f77fd2f7 +size 49861 diff --git a/Assets/KJM/KJM_Test/ISaveable.cs b/Assets/KJM/KJM_Test/ISaveable.cs index 5c98ca97..a7d44c8e 100644 --- a/Assets/KJM/KJM_Test/ISaveable.cs +++ b/Assets/KJM/KJM_Test/ISaveable.cs @@ -6,26 +6,3 @@ 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/SaveDataController.cs b/Assets/KJM/KJM_Test/SaveDataController.cs index f380d64b..58cb82e8 100644 --- a/Assets/KJM/KJM_Test/SaveDataController.cs +++ b/Assets/KJM/KJM_Test/SaveDataController.cs @@ -6,13 +6,16 @@ using UnityEngine; public class SaveDataController : MonoBehaviour { - [SerializeField] private ISaveable[] saveables; + [SerializeField] private MonoBehaviour[] saveableBehaviours; + private ISaveable[] saveables; private Save tmpSave; void Awake() { tmpSave = new Save(); + saveables = saveableBehaviours.OfType().ToArray(); + if (saveables == null || saveables.Length == 0) saveables = FindObjectsOfType().OfType().ToArray(); } diff --git a/Assets/KJM/KJM_Test/SaveManager.cs b/Assets/KJM/KJM_Test/SaveManager.cs index 0b9c8df1..773cff46 100644 --- a/Assets/KJM/KJM_Test/SaveManager.cs +++ b/Assets/KJM/KJM_Test/SaveManager.cs @@ -12,7 +12,8 @@ public class SaveManager : Singleton private const string SaveFolder = "QuickSave"; private string MainSaveFilePath => GetSavePath("Save_Main"); private string BackupSaveFilePath => GetSavePath("Save_Backup"); - + + [SerializeField] private SaveDataController saveDataController; private Save mainSave; private Save backupSave; @@ -25,7 +26,7 @@ public class SaveManager : Singleton public void Save() { - if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(StatManager.instance.ToSaveData())) //같은 상태는 저장되지 않음. 백업 덮어쓰기 방지. + if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(saveDataController.GetSaveData())) //같은 상태는 저장되지 않음. 백업 덮어쓰기 방지. return; EnsureSaveExists(); @@ -45,7 +46,7 @@ public class SaveManager : Singleton mainSave = LoadMain(); backupSave = LoadBackup(); - StatManager.instance.loadSaveData2StataManager(mainSave); + saveDataController.ApplySaveData(mainSave); Debug.Log("메인 로드" + mainSave.homeSave.reputation); //임시 코드 Debug.Log("백업 로드" + backupSave.homeSave.reputation); //임시 코드 @@ -53,7 +54,7 @@ public class SaveManager : Singleton private void UpdateSaveInfo() { - mainSave = StatManager.instance.ToSaveData(); //스탯을 관리하는 클래스에 선언된 스탯 업데이트 함수를 호출 + mainSave = saveDataController.GetSaveData(); //스탯을 관리하는 클래스에 선언된 스탯 업데이트 함수를 호출 } private void SaveMain() @@ -119,7 +120,7 @@ public class SaveManager : Singleton //더미 세이브 파일 생성 private Save CreateNewSave() { - var fresh = StatManager.instance.ToSaveData(); + var fresh = saveDataController.GetSaveData(); SaveMain(); SaveBackup(); return fresh; diff --git a/Assets/KJM/KJM_Test/StatManager.cs b/Assets/KJM/KJM_Test/StatManager.cs deleted file mode 100644 index d00d5e68..00000000 --- a/Assets/KJM/KJM_Test/StatManager.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.SceneManagement; -using Random = UnityEngine.Random; - -/// -/// 테스트용 임시 클래스 -/// -public class StatManager : MonoBehaviour -{ - public static StatManager instance; - - public int attackLevel; - public int attackSpeedLevel; - public int heartLevel; - public int moveSpeedLevel; - public int evasionTimeLevel; - public int stageLevel; - - public float time; - public int currentDay; - public float health; - public float reputation; - - public bool isEvent; //Todo 이벤트 여부 및 관련 조건들 추가 - public int mealCount; - public int houseworkCount; - - - private void Awake() - { - instance = this; - } - - /// - /// 스탯값 변화 - /// - public void ChangeValue() - { - float floatValue = Random.Range(0f, 2f); - Debug.Log(floatValue); - int intValue = Random.Range(0, 10); - Debug.Log(intValue); - - attackLevel = intValue; - attackSpeedLevel = intValue; - heartLevel = intValue; - moveSpeedLevel = intValue; - evasionTimeLevel = intValue; - stageLevel = intValue; - - - time = floatValue; - currentDay = intValue; - health = floatValue; - reputation = floatValue; - - isEvent = false; - mealCount = intValue; - houseworkCount = intValue; - - Debug.Log("ChangeValue"); - - } - - /// - /// 스탯값 반환 - /// - /// - public Save ToSaveData() - { - return new Save - { - dungeonSave = new DungeonSave - { - attackLevel = this.attackLevel, - attackSpeedLevel = this.attackSpeedLevel, - heartLevel = this.heartLevel, - moveSpeedLevel = this.moveSpeedLevel, - evasionTimeLevel = this.evasionTimeLevel, - stageLevel = this.stageLevel - }, - homeSave = new HomeSave - { - time = this.time, - currentDay = this.currentDay, - health = this.health, - reputation = this.reputation, - mealCount = this.mealCount, - houseworkCount = this.houseworkCount, - } - }; - } - - public void loadSaveData2StataManager(Save saveData) - { - attackLevel = saveData.dungeonSave.attackLevel; - attackSpeedLevel = saveData.dungeonSave.attackSpeedLevel; - heartLevel = saveData.dungeonSave.heartLevel; - moveSpeedLevel = saveData.dungeonSave.moveSpeedLevel; - evasionTimeLevel = saveData.dungeonSave.evasionTimeLevel; - stageLevel = saveData.dungeonSave.stageLevel; - - time = saveData.homeSave.time; - currentDay = saveData.homeSave.currentDay; - health = saveData.homeSave.health; - reputation = saveData.homeSave.reputation; - mealCount = saveData.homeSave.mealCount; - houseworkCount = saveData.homeSave.houseworkCount; - } - - public void SceneChange() - { - SceneManager.LoadScene("Main"); - } -} diff --git a/Assets/KJM/KJM_Test/TestScript.cs b/Assets/KJM/KJM_Test/TestScript.cs new file mode 100644 index 00000000..107b18ab --- /dev/null +++ b/Assets/KJM/KJM_Test/TestScript.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; +using Random = UnityEngine.Random; + +/// +/// 테스트용 임시 클래스 +/// +public class TestScript : MonoBehaviour,ISaveable +{ + //던전 + private int attackLevel; + private int attackSpeedLevel; + private int heartLevel; + private int moveSpeedLevel; + private int evasionTimeLevel; + private int stageLevel; + + //일상 + private float time; + private int currentDay; + private float health; + private float reputation; + private int mealCount; + private int houseworkCount; + + /// + /// 스탯값 랜덤 변화 + /// + public void ChangeValue() + { + float floatValue = Random.Range(0f, 2f); + int intValue = Random.Range(0, 10); + + attackLevel = intValue; + attackSpeedLevel = intValue; + heartLevel = intValue; + moveSpeedLevel = intValue; + stageLevel = intValue; + + + time = floatValue; + currentDay = intValue; + health = floatValue; + reputation = floatValue; + mealCount = intValue; + houseworkCount = intValue; + + Debug.Log("ChangeValue : " + floatValue); + } + + /// + /// 인터페이스 함수1, 데이터 불러오기 + /// + /// + public void ApplySaveData(Save save) + { + if (save?.dungeonSave != null) + { + attackLevel = save.dungeonSave.attackLevel; + attackSpeedLevel = save.dungeonSave.attackSpeedLevel; + heartLevel = save.dungeonSave.heartLevel; + moveSpeedLevel = save.dungeonSave.moveSpeedLevel; + evasionTimeLevel = save.dungeonSave.evasionTimeLevel; + stageLevel = save.dungeonSave.stageLevel; + } + + if (save?.homeSave != null) + { + time = save.homeSave.time; + currentDay = save.homeSave.currentDay; + health = save.homeSave.health; + reputation = save.homeSave.reputation; + mealCount = save.homeSave.mealCount; + houseworkCount = save.homeSave.houseworkCount; + + Debug.Log("ApplySaveData : " + reputation); + } + } + + /// + /// 인터페이스 함수2, 데이터 전달 + /// + /// + public Save ExtractSaveData() + { + return new Save + { + dungeonSave = new DungeonSave() + { + attackLevel = this.attackLevel, + attackSpeedLevel = this.attackSpeedLevel, + heartLevel = this.heartLevel, + moveSpeedLevel = this.moveSpeedLevel, + evasionTimeLevel = this.evasionTimeLevel, + stageLevel = this.stageLevel, + + }, + + homeSave = new HomeSave + { + time = this.time, + currentDay = this.currentDay, + health = this.health, + reputation = this.reputation, + mealCount = this.mealCount, + houseworkCount = this.houseworkCount + } + }; + } + + /// + /// 씬 전환 테스트용 함수 + /// + public void SceneChange() + { + SceneManager.LoadScene("Main"); + } + +} diff --git a/Assets/KJM/KJM_Test/StatManager.cs.meta b/Assets/KJM/KJM_Test/TestScript.cs.meta similarity index 100% rename from Assets/KJM/KJM_Test/StatManager.cs.meta rename to Assets/KJM/KJM_Test/TestScript.cs.meta