From b5160373bb7c6679a6ce4df2d58db29390eec3d1 Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Wed, 12 Feb 2025 20:27:07 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=BA=90=EB=A6=AD=ED=84=B0=20?= =?UTF-8?q?=EB=A0=88=EB=B2=A8=EC=97=85=20=EC=8B=9C=20=EA=B2=BD=ED=97=98?= =?UTF-8?q?=EC=B9=98,=20=EA=B3=B5=EA=B2=A9=EB=A0=A5,=20=EB=B0=A9=EC=96=B4?= =?UTF-8?q?=EB=A0=A5,=20=EC=9E=94=EC=97=AC=20=EA=B2=BD=ED=97=98=EC=B9=98?= =?UTF-8?q?=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=97=85=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Character/CharacterBase.cs | 34 +++---- .../Scripts/Character/PlayerDataManager.cs | 99 +++++++++++++++++++ .../PlayerDataManager.cs.meta | 0 .../Scripts/Common/PlayerDataManager.cs | 43 -------- 4 files changed, 110 insertions(+), 66 deletions(-) create mode 100644 Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs rename Gameton-06/Assets/Gameton/Scripts/{Common => Character}/PlayerDataManager.cs.meta (100%) delete mode 100644 Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/CharacterBase.cs b/Gameton-06/Assets/Gameton/Scripts/Character/CharacterBase.cs index e707963b..ad371741 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Character/CharacterBase.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Character/CharacterBase.cs @@ -7,6 +7,8 @@ namespace TON public class CharacterBase : MonoBehaviour, IDamage { + [SerializeField] // + private PlayerData playerData; public float currentHP; public float maxHP; public float currentSP; @@ -41,42 +43,28 @@ namespace TON public void Initialize() { - int playerIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0); - PlayerData playerData = PlayerDataManager.Singleton.playersData[playerIndex]; + // int playerIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0); + PlayerDataManager.Singleton.SetCurrentUserData(); + playerData = PlayerDataManager.Singleton.player; currentHP = maxHP = playerData.hp; currentSP = maxSP = playerData.mp; } - public int level = 1; // 현재 레벨 - public int exp = 0; // 현재 경험치 - public int expVariable = 10; // 경험치 변수 (조정 가능) - - // 현재 레벨에서 다음 레벨까지 필요한 경험치 계산 - private int GetRequiredExp(int currentLevel) - { - return (6 * currentLevel * currentLevel) + (currentLevel * expVariable); - } - // 경험치 추가 및 레벨업 처리 public void AddExp(int amount) { - exp += amount; // 경험치 추가 - bool leveledUp = false; // 레벨업 여부 체크 - - while (exp >= GetRequiredExp(level)) // 경험치가 충분하면 반복해서 레벨업 - { - exp -= GetRequiredExp(level); // 초과 경험치 유지 - level++; // 레벨 증가 - leveledUp = true; - } + bool leveledUp = PlayerDataManager.Singleton.UpdateExpericence(amount); if (leveledUp) { - // 경험치와 레벨 데이터를 파일에 업데이트 한다. - Debug.Log($"레벨업! 현재 레벨: {level}, 남은 경험치: {exp}"); + // TODO: 레벨업 시 처리할 내용 추가 + Debug.Log($"레벨업! "); } + + // 경험치와 변경된 데이터를 파일에 업데이트 한다. + PlayerDataManager.Singleton.UpdatePlayerData(); } public void FixedUpdate() diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs new file mode 100644 index 00000000..f02c51b9 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs @@ -0,0 +1,99 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TON +{ + public class PlayerDataManager : SingletonBase + { + // 사용자가 생성해둔 플레이어 데이터를 싱글톤으로 전역 사용하기 위함 + public List playersData { get; private set; } + + public PlayerData player { get; private set; } + + [SerializeField] + private int expVariable = 50; // 경험치 변수 (조정 가능) + [SerializeField] + private int attackGrowthFactor = 50; // 공격력 성장 변수 (조정 가능) + [SerializeField] + private int defensiveGrowthFactor = 200; // 방어력 성장 변수 (조정 가능) + + protected override void Awake() + { + base.Awake(); + LoadPlayerData(); + } + + private void LoadPlayerData() + { + if (playersData != null) + { + playersData.Clear(); + } + + JSONLoader.SaveJsonToPersistentData("player"); + playersData = JSONLoader.LoadJsonFromPersistentData>("player"); + if (playersData == null) + { + playersData = new List(); + } + } + + // 공격력과 방어력 업데이트 + private void UpdateStats(int currentLevel) + { + player.attackPower *= 1 + (currentLevel - 1) / attackGrowthFactor; + player.defensivePower *= 1 + (currentLevel - 1) / defensiveGrowthFactor; + } + + // 현재 레벨에서 다음 레벨까지 필요한 경험치 계산 + private int GetRequiredExp(int currentLevel) + { + return (6 * currentLevel * currentLevel) + (currentLevel * expVariable); + } + + public bool UpdateExpericence(int amount) + { + // 경험치 추가 + player.experience += amount; + + // 추가된 경험치로 인한 현재 경험치가 레벨업에 필요한 경험치보다 크거나 같다면 레벨업 + int requireLevelUpExp = GetRequiredExp(player.level); + if (player.experience >= requireLevelUpExp) + { + // 레벨업 후 초과된 경험치를 반영하기 위해 다시 계산 + player.experience -= requireLevelUpExp; + // 레벨업으로 인한 공격력/방어력 업데이트 + UpdateStats(player.level); + // 레벨 증가 + player.level++; + return true; + } + return false; + } + + public void UpdatePlayerData() + { + int index = playersData.FindIndex(x => x.id == player.id); + if (index > -1) + { + playersData[index] = player; + JSONLoader.SaveUpdatedJsonToPersistentData(playersData, "player"); + } + } + + public void SetCurrentUserData() + { + int characterId = PlayerPrefs.GetInt("SelectedPlayerIndex", -1); + if (characterId > -1) + { + player = playersData[characterId]; + } + else + { + Debug.LogError("유효하지 않은 캐릭터 정보 입니다."); + } + } + + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs.meta similarity index 100% rename from Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs.meta rename to Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs.meta diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs deleted file mode 100644 index ac556369..00000000 --- a/Gameton-06/Assets/Gameton/Scripts/Common/PlayerDataManager.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -namespace TON -{ - public class PlayerDataManager : SingletonBase - { - // 사용자가 생성해둔 플레이어 데이터를 싱글톤으로 전역 사용하기 위함 - public List playersData { get; private set; } - - public PlayerData player { get; private set; } - - protected override void Awake() - { - base.Awake(); - LoadPlayerData(); - } - - private void LoadPlayerData() - { - playersData = JSONLoader.LoadFromResources>("Player"); - if (playersData == null) - { - playersData = new List(); - } - } - - public void SetCurrentUserData() - { - int characterId = PlayerPrefs.GetInt("SelectedPlayerIndex", -1); - if (characterId > -1) - { - player = playersData[characterId]; - } - else - { - Debug.LogError("유효하지 않은 캐릭터 정보 입니다."); - } - } - - } -} From ca1db76505eef91029c40a8ca5ab69cc0575a540 Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Wed, 12 Feb 2025 20:41:45 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EC=BA=90=EB=A6=AD=ED=84=B0=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=97=85=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Character/Heart/HeartDataManager.cs | 15 ++++++++++++--- .../Scripts/Character/PlayerDataManager.cs | 8 +++++--- .../Assets/Gameton/Scripts/Common/BootStrapper.cs | 5 +++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/Heart/HeartDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Character/Heart/HeartDataManager.cs index f2999520..048330db 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Character/Heart/HeartDataManager.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Character/Heart/HeartDataManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Assertions; namespace TON { @@ -25,7 +26,13 @@ namespace TON private void LoadHeartData() { - heartDatas = JSONLoader.LoadFromResources>("Heart"); + if (heartDatas != null) + { + heartDatas.Clear(); + } + + JSONLoader.SaveJsonToPersistentData("heart"); + heartDatas = JSONLoader.LoadJsonFromPersistentData>("Heart"); if (heartDatas == null) { heartDatas = new List(); @@ -37,8 +44,9 @@ namespace TON { HeartData heartData = new HeartData(characterId); heartDatas.Add(heartData); - JSONLoader.SaveToFile(heartDatas, "heart"); + Assert.IsTrue(JSONLoader.SaveUpdatedJsonToPersistentData(heartDatas, "heart")); Debug.Log($"heartData test:: {heartData.currentHearts}"); + LoadHeartData(); } public void SetCurrentUserHeart() @@ -68,7 +76,8 @@ namespace TON public void SaveHeartData() { heartDatas[characterId] = currentHeartData; - JSONLoader.SaveToFile(heartDatas, "heart"); + Assert.IsTrue(JSONLoader.SaveUpdatedJsonToPersistentData(heartDatas, "heart")); + LoadHeartData(); } // 게임이 다시 실행될때 마지막 하트 소모 시간과 현재 시간을 계산해서 하트 충전량을 반영 diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs index f02c51b9..bf809c09 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerDataManager.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Assertions; namespace TON { @@ -18,9 +19,9 @@ namespace TON [SerializeField] private int defensiveGrowthFactor = 200; // 방어력 성장 변수 (조정 가능) - protected override void Awake() + + public void Initalize() { - base.Awake(); LoadPlayerData(); } @@ -78,7 +79,8 @@ namespace TON if (index > -1) { playersData[index] = player; - JSONLoader.SaveUpdatedJsonToPersistentData(playersData, "player"); + Assert.IsTrue(JSONLoader.SaveUpdatedJsonToPersistentData(playersData, "player")); + Initalize(); } } diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/BootStrapper.cs b/Gameton-06/Assets/Gameton/Scripts/Common/BootStrapper.cs index 8ffe3858..fb6222c7 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Common/BootStrapper.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Common/BootStrapper.cs @@ -43,11 +43,12 @@ namespace TON { Main.Singleton.Initialize(); - List playersData = PlayerDataManager.Singleton.playersData; + // List playersData = PlayerDataManager.Singleton.playersData; + PlayerDataManager.Singleton.Initalize(); PlayerPrefs.SetInt("SelectedPlayerIndex", 0); PlayerDataManager.Singleton.SetCurrentUserData(); // HeartDataManager.Singleton.(); - List skillDatas = SkillDataManager.Singleton.skillDatas; + // List skillDatas = SkillDataManager.Singleton.skillDatas; SkillDataManager.Singleton.Initalize(); // TODO : Custom Order After System Load From c38335f0bf6cdbde91bcd1bff4654586aabc724a Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Wed, 12 Feb 2025 20:55:28 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=8A=A4=ED=82=AC=20=EC=9E=A5?= =?UTF-8?q?=EC=B0=A9=20=EC=8B=9C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs index 271feb13..d7367d19 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs @@ -103,14 +103,13 @@ namespace TON string persistentPath = GetPersistentPath(fileName); -#if UNITY_ANDROID // 📌 Step 1: persistentDataPath에 파일이 있는지 체크 // Android에서는 파일이 이미 존재하면 덮어쓰지 않도록 함 if (File.Exists(persistentPath)) { Debug.Log($"⚠ {fileName}.json 파일이 이미 존재합니다. 덮어쓰지 않습니다. ({persistentPath})"); + return; } -#endif // 📌 Step 2: Resources에서 JSON 불러오기 string path = DATA_PATH + fileName; // Resources 폴더 내 경로 TextAsset jsonFile = Resources.Load(path); @@ -118,7 +117,7 @@ namespace TON if (jsonFile != null) { File.WriteAllText(persistentPath, jsonFile.text); - Debug.Log($"✅ JSON 저장 완료 (처음 저장됨): {persistentPath}"); + Debug.Log($"✅ JSON 저장 완료: {persistentPath}"); } else {