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 new file mode 100644 index 00000000..a7d44c8e --- /dev/null +++ b/Assets/KJM/KJM_Test/ISaveable.cs @@ -0,0 +1,8 @@ +/// +/// 세이브 데이터를 주고 받는 함수 인터페이스 +/// +public interface ISaveable +{ + void ApplySaveData(Save save); + Save ExtractSaveData(); +} 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..58cb82e8 --- /dev/null +++ b/Assets/KJM/KJM_Test/SaveDataController.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using UnityEngine; + +public class SaveDataController : MonoBehaviour +{ + [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(); + } + + //세이브를 현재 상태에 적용 + 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/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 b0129abc..00000000 --- a/Assets/KJM/KJM_Test/StatManager.cs +++ /dev/null @@ -1,120 +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 day; - 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; - day = 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, - day = this.day, - health = this.health, - reputation = this.reputation, - isEvent = false, - 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; - day = saveData.homeSave.day; - health = saveData.homeSave.health; - reputation = saveData.homeSave.reputation; - isEvent = saveData.homeSave.isEvent; - 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