根据提供的code differences信息,由于没有具体的代码变更内容,我将生成一个通用的commit message模板:
``` docs(readme): 更新文档说明 - 添加项目使用指南 - 完善API接口说明 - 修正错误的配置示例 ``` 注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
This commit is contained in:
1 parent
8d58c2223c
commit
11f1af357d
191 files changed
+2315
-1539
No files matched your search
@@ -0,0 +1,176 @@
|
||||
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<PlayerCharacterData> ownedCharacters = new List<PlayerCharacterData>();
|
||||
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<TextAsset>("PlayerData");
|
||||
|
||||
if (json != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var save = JsonUtility.FromJson<PlayerSaveJson>(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++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存阵型到 PlayerData.json
|
||||
/// </summary>
|
||||
public void SaveFormation(string[] characterIds, int[] cellIndices)
|
||||
{
|
||||
string path = Application.dataPath + "/Resources/PlayerData.json";
|
||||
|
||||
try
|
||||
{
|
||||
// 读取现有数据
|
||||
string json = File.ReadAllText(path);
|
||||
var saveData = JsonUtility.FromJson<PlayerSaveJson>(json);
|
||||
|
||||
// 更新阵型数据
|
||||
saveData.savedFormation = new SavedFormationData
|
||||
{
|
||||
characterIds = characterIds,
|
||||
cellIndices = cellIndices
|
||||
};
|
||||
|
||||
// 写回文件
|
||||
string updatedJson = JsonUtility.ToJson(saveData, true);
|
||||
File.WriteAllText(path, updatedJson);
|
||||
|
||||
Debug.Log($"<color=green>✅ 阵型已保存到 PlayerData.json</color>");
|
||||
Debug.Log($"保存了 {characterIds.Length} 个英雄的阵型数据");
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError($"保存阵型失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 PlayerData.json 加载阵型
|
||||
/// </summary>
|
||||
public SavedFormationData LoadFormation()
|
||||
{
|
||||
TextAsset json = localPlayerJson != null
|
||||
? localPlayerJson
|
||||
: Resources.Load<TextAsset>("PlayerData");
|
||||
|
||||
if (json != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var save = JsonUtility.FromJson<PlayerSaveJson>(json.text);
|
||||
if (save != null && save.savedFormation != null)
|
||||
{
|
||||
return save.savedFormation;
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError("加载阵型失败:" + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return new SavedFormationData();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user