using System.Collections.Generic; using UnityEngine; using System.IO; [System.Serializable] public class PlayerOwnedCharacterJson { public string characterId; public int level = 1; public int starLevel = 0; } [System.Serializable] public class SavedFormationData { public string[] characterIds = new string[0]; public int[] cellIndices = new int[0]; } [System.Serializable] public class PlayerSaveJson { public string playerId = "local_player"; public PlayerOwnedCharacterJson[] unlockedCharacters = new PlayerOwnedCharacterJson[0]; public string[] currentDeck = new string[0]; public SavedFormationData savedFormation = new SavedFormationData(); } public class PlayerDataManager : MonoBehaviour { public static PlayerDataManager Instance { get; private set; } [Header("本地玩家 JSON(正式版替换为服务器响应)")] public TextAsset localPlayerJson; public List ownedCharacters = new List(); public PlayerCharacterData[] currentDeck = new PlayerCharacterData[5]; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); return; } Instance = this; InitializeFromJsonOrFallback(); } private void InitializeFromJsonOrFallback() { TextAsset json = localPlayerJson != null ? localPlayerJson : Resources.Load("PlayerData"); if (json != null) { try { var save = JsonUtility.FromJson(json.text); if (save != null && save.unlockedCharacters != null) { ownedCharacters.Clear(); foreach (var entry in save.unlockedCharacters) { if (entry == null || string.IsNullOrEmpty(entry.characterId)) continue; ownedCharacters.Add(new PlayerCharacterData(entry.characterId, Mathf.Max(1, entry.level), Mathf.Max(0, entry.starLevel))); } currentDeck = new PlayerCharacterData[5]; for (int i = 0; i < save.currentDeck.Length && i < currentDeck.Length; i++) currentDeck[i] = GetCharacterData(save.currentDeck[i]); return; } } catch (System.Exception e) { Debug.LogError("玩家 JSON 解析失败:" + e.Message); } } InitializeTempData(); } private void InitializeTempData() { ownedCharacters.Clear(); ownedCharacters.Add(new PlayerCharacterData("human_soldier", 1, 0)); ownedCharacters.Add(new PlayerCharacterData("archer", 1, 0)); ownedCharacters.Add(new PlayerCharacterData("shield_guard", 1, 0)); ownedCharacters.Add(new PlayerCharacterData("mage", 1, 0)); ownedCharacters.Add(new PlayerCharacterData("elite_soldier", 1, 0)); currentDeck = new PlayerCharacterData[5]; for (int i = 0; i < currentDeck.Length && i < ownedCharacters.Count; i++) currentDeck[i] = ownedCharacters[i]; } public PlayerCharacterData GetCharacterData(string characterId) { return ownedCharacters.Find(c => c.characterId == characterId); } public PlayerCharacterData GetDeckCharacter(int slot) { if (slot < 0 || slot >= currentDeck.Length) return null; return currentDeck[slot]; } public void UpgradeCharacter(string characterId) { var character = GetCharacterData(characterId); if (character != null) character.level++; } /// /// 保存阵型到 PlayerData.json /// public void SaveFormation(string[] characterIds, int[] cellIndices) { string path = Application.dataPath + "/Resources/PlayerData.json"; try { // 读取现有数据 string json = File.ReadAllText(path); var saveData = JsonUtility.FromJson(json); // 更新阵型数据 saveData.savedFormation = new SavedFormationData { characterIds = characterIds, cellIndices = cellIndices }; // 写回文件 string updatedJson = JsonUtility.ToJson(saveData, true); File.WriteAllText(path, updatedJson); Debug.Log($"✅ 阵型已保存到 PlayerData.json"); Debug.Log($"保存了 {characterIds.Length} 个英雄的阵型数据"); } catch (System.Exception e) { Debug.LogError($"保存阵型失败: {e.Message}"); } } /// /// 从 PlayerData.json 加载阵型 /// public SavedFormationData LoadFormation() { TextAsset json = localPlayerJson != null ? localPlayerJson : Resources.Load("PlayerData"); if (json != null) { try { var save = JsonUtility.FromJson(json.text); if (save != null && save.savedFormation != null) { return save.savedFormation; } } catch (System.Exception e) { Debug.LogError("加载阵型失败:" + e.Message); } } return new SavedFormationData(); } }