182 lines
6.7 KiB
C#
182 lines
6.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using Cielonos.MainGame;
|
||
using Cielonos.MainGame.Inventory;
|
||
using UnityEngine;
|
||
|
||
namespace Cielonos.MainGame.Characters
|
||
{
|
||
public partial class PlayerInventorySubcontroller
|
||
{
|
||
/// <summary>
|
||
/// 将当前玩家的整个背包与装备槽位状态打包生成 SaveData。
|
||
/// </summary>
|
||
public PlayerInventorySaveData GetSaveData()
|
||
{
|
||
var data = new PlayerInventorySaveData();
|
||
|
||
// 1. 保存背包中的所有主武器
|
||
foreach (var weapon in backpackSm.mainWeapons)
|
||
{
|
||
if (weapon != null)
|
||
{
|
||
data.mainWeapons.Add(CreateSaveDataFromItem(weapon));
|
||
}
|
||
}
|
||
|
||
// 2. 保存快速切换(Prepared)列表中的主武器
|
||
foreach (var weapon in equipmentSm.preparedMainWeapons)
|
||
{
|
||
if (weapon != null)
|
||
{
|
||
data.preparedMainWeapons.Add(CreateSaveDataFromItem(weapon));
|
||
}
|
||
}
|
||
|
||
// 3. 保存当前手持的主武器
|
||
if (equipmentSm.currentMainWeapon != null)
|
||
{
|
||
data.currentMainWeapon = CreateSaveDataFromItem(equipmentSm.currentMainWeapon);
|
||
}
|
||
|
||
// 4. 保存背包中所有的辅助装备
|
||
foreach (var support in backpackSm.supportEquipments)
|
||
{
|
||
if (support != null)
|
||
{
|
||
data.supportEquipments.Add(CreateSaveDataFromItem(support));
|
||
}
|
||
}
|
||
|
||
// 5. 保存装备槽(0~3)中穿戴的辅助装备
|
||
for (int i = 0; i < equipmentSm.currentSupportEquipments.Count; i++)
|
||
{
|
||
var slotSupport = equipmentSm.currentSupportEquipments[i];
|
||
if (slotSupport != null)
|
||
{
|
||
data.currentSupportEquipments.Add(CreateSaveDataFromItem(slotSupport));
|
||
}
|
||
else
|
||
{
|
||
data.currentSupportEquipments.Add(null);
|
||
}
|
||
}
|
||
|
||
// 6. 保存背包中的所有被动装备
|
||
foreach (var passive in backpackSm.passiveEquipments)
|
||
{
|
||
if (passive != null)
|
||
{
|
||
data.passiveEquipments.Add(CreateSaveDataFromItem(passive));
|
||
}
|
||
}
|
||
|
||
// 7. 保存背包中的所有消耗品
|
||
foreach (var consumable in backpackSm.consumables)
|
||
{
|
||
if (consumable != null)
|
||
{
|
||
data.consumables.Add(CreateSaveDataFromItem(consumable));
|
||
}
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据保存的 SaveData,一键还原玩家的背包和当前装备槽位。
|
||
/// </summary>
|
||
public void ApplySaveData(PlayerInventorySaveData data)
|
||
{
|
||
if (data == null) return;
|
||
|
||
// 1. 彻底清理默认装配的武器与背包实体,防止状态和逻辑堆叠
|
||
ClearInventory();
|
||
|
||
// 2. 第一阶段:生成所有背包内的主武器
|
||
var restoredWeaponsDict = new Dictionary<string, MainWeaponBase>();
|
||
foreach (var weaponData in data.mainWeapons)
|
||
{
|
||
if (weaponData == null) continue;
|
||
MainWeaponBase weaponInstance = weaponData.RestoreItem() as MainWeaponBase;
|
||
if (weaponInstance != null)
|
||
{
|
||
restoredWeaponsDict[weaponData.itemClass] = weaponInstance;
|
||
}
|
||
}
|
||
|
||
if (data.preparedMainWeapons is { Count: > 0 })
|
||
{
|
||
// 3. 第二阶段:将指定的主武器加入备用快速切换(Prepare)列表
|
||
foreach (var prepData in data.preparedMainWeapons)
|
||
{
|
||
if (prepData != null && restoredWeaponsDict.TryGetValue(prepData.itemClass, out var prepWeapon))
|
||
{
|
||
equipmentSm.PrepareMainWeapon(prepWeapon);
|
||
}
|
||
}
|
||
}
|
||
else if(data.mainWeapons.Count > 0)
|
||
{
|
||
// 兜底:如果存档中没有记录 Prepare 列表,默认将背包中的第一把武器加入 Prepare 列表
|
||
var firstWeapon = restoredWeaponsDict[data.mainWeapons[0].itemClass];
|
||
equipmentSm.PrepareMainWeapon(firstWeapon);
|
||
}
|
||
|
||
// 4. 第三阶段:装备当前手持激活的主武器
|
||
if (data.currentMainWeapon != null &&
|
||
restoredWeaponsDict.TryGetValue(data.currentMainWeapon.itemClass, out var activeWeapon))
|
||
{
|
||
equipmentSm.EquipMainWeapon(activeWeapon);
|
||
}
|
||
else if (equipmentSm.preparedMainWeapons.Count > 0)
|
||
{
|
||
// 兜底:如果存档中没有记录当前手持,默认装备切换列表中的第一把武器
|
||
equipmentSm.EquipMainWeapon(equipmentSm.preparedMainWeapons[0]);
|
||
}
|
||
|
||
// 5. 第一阶段:生成所有背包内的辅助装备
|
||
var restoredSupportDict = new Dictionary<string, SupportEquipmentBase>();
|
||
foreach (var supportData in data.supportEquipments)
|
||
{
|
||
if (supportData == null) continue;
|
||
SupportEquipmentBase supportInstance = supportData.RestoreItem() as SupportEquipmentBase;
|
||
if (supportInstance != null)
|
||
{
|
||
restoredSupportDict[supportData.itemClass] = supportInstance;
|
||
}
|
||
}
|
||
|
||
// 6. 第二阶段:将对应的辅助装备装配到指定的槽位 (0 ~ 3)
|
||
for (int i = 0; i < data.currentSupportEquipments.Count; i++)
|
||
{
|
||
var slotData = data.currentSupportEquipments[i];
|
||
if (slotData != null && restoredSupportDict.TryGetValue(slotData.itemClass, out var supportInstance))
|
||
{
|
||
equipmentSm.EquipSupportEquipment(supportInstance, i);
|
||
}
|
||
}
|
||
|
||
// 7. 还原背包内的被动装备
|
||
foreach (var passiveData in data.passiveEquipments)
|
||
{
|
||
if (passiveData != null)
|
||
{
|
||
passiveData.RestoreItem();
|
||
}
|
||
}
|
||
|
||
// 8. 还原背包内的消耗品
|
||
foreach (var consumableData in data.consumables)
|
||
{
|
||
if (consumableData != null)
|
||
{
|
||
consumableData.RestoreItem();
|
||
}
|
||
}
|
||
|
||
Debug.Log("[SaveSystem] 背包与全部槽位配置还原成功!");
|
||
}
|
||
}
|
||
}
|