feat(gameplay): 提升单位检测范围并添加法师属性 - 将所有单位的检测范围从默认值提升至100 - 为法师预制体添加护盾、等级等新属性 - 为法师添加暴击、吸血、伤害减免等战斗属性 - 更换法师弹丸预设并调整相关参数 - 添加锚点交战Z线控制功能 - 更新玩家数据中配备的召唤师技能 - 移除多余的纹理配置文件 ```
183 lines
6.0 KiB
C#
183 lines
6.0 KiB
C#
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 string[] equippedSummonerSkills = new string[] { "神圣赞歌", "陨石轰炸" }; // 装备的 2 个召唤师技能名称
|
|
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];
|
|
public string[] equippedSummonerSkills = new string[] { "神圣赞歌", "陨石轰炸" };
|
|
|
|
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]);
|
|
|
|
if (save.equippedSummonerSkills != null && save.equippedSummonerSkills.Length >= 2)
|
|
{
|
|
equippedSummonerSkills = new string[] { save.equippedSummonerSkills[0], save.equippedSummonerSkills[1] };
|
|
}
|
|
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();
|
|
}
|
|
} |