2025-08-19 20:22:10 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Data;
|
|
|
|
|
using Item;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
using UnityEngine;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
namespace Managers
|
|
|
|
|
{
|
2025-08-26 00:11:36 +08:00
|
|
|
|
public class ItemResourceManager : Utils.Singleton<ItemResourceManager>,ILaunchManager
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-25 18:24:12 +08:00
|
|
|
|
private readonly Dictionary<string, Item.ItemResource> _items = new();
|
|
|
|
|
private readonly Dictionary<string, List<Item.ItemResource>> _itemsByName = new(); // 保持按显示名称查找的字典
|
|
|
|
|
|
2025-08-26 00:11:36 +08:00
|
|
|
|
public string StepDescription => "加载物品定义中";
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
2025-08-25 18:24:12 +08:00
|
|
|
|
var baseItemDefs = Managers.DefineManager.Instance.QueryDefinesByType<ItemDef>();
|
|
|
|
|
var weaponDefs = Managers.DefineManager.Instance.QueryDefinesByType<WeaponDef>();
|
|
|
|
|
|
|
|
|
|
var allDefs = new List<ItemDef>();
|
|
|
|
|
if (baseItemDefs != null) allDefs.AddRange(baseItemDefs);
|
|
|
|
|
if (weaponDefs != null) allDefs.AddRange(weaponDefs);
|
|
|
|
|
|
|
|
|
|
if (allDefs.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("ItemResourceManager: No ItemDef or WeaponDef found to initialize.");
|
2025-08-19 20:22:10 +08:00
|
|
|
|
return;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var def in allDefs)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (_items.ContainsKey(def.defName))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError(
|
|
|
|
|
$"ItemResourceManager: Duplicate itemDef.defName found: {def.defName}. Skipping this item.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var itemIcon = Managers.PackagesImageManager.Instance.GetSprite(def.texture);
|
|
|
|
|
if (!itemIcon)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
$"ItemResourceManager: Failed to load sprite for texture '{def.texture}' for item '{def.defName}'. Icon will be null.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var itemName = string.IsNullOrEmpty(def.label) ? def.defName : def.label;
|
|
|
|
|
if (string.IsNullOrEmpty(def.label))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
$"ItemResourceManager: ItemDef '{def.defName}' has an empty label. Using defName as item name.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var itemDescription = def.description ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
Item.ItemResource itemResource;
|
|
|
|
|
|
|
|
|
|
if (def is WeaponDef currentWeaponDef)
|
|
|
|
|
{
|
|
|
|
|
itemResource = new Item.WeaponResource(
|
|
|
|
|
def.defName, // 传递 defName
|
|
|
|
|
itemName,
|
|
|
|
|
itemDescription,
|
|
|
|
|
itemIcon,
|
|
|
|
|
currentWeaponDef.rarity,
|
|
|
|
|
currentWeaponDef.maxStack,
|
|
|
|
|
currentWeaponDef.ssEquippable,
|
|
|
|
|
currentWeaponDef.attributes
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
itemResource = new Item.ItemResource(
|
|
|
|
|
def.defName, // 传递 defName
|
|
|
|
|
itemName,
|
|
|
|
|
itemDescription,
|
|
|
|
|
itemIcon,
|
|
|
|
|
def.rarity,
|
|
|
|
|
def.maxStack,
|
|
|
|
|
def.ssEquippable
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_items.Add(def.defName, itemResource);
|
|
|
|
|
|
|
|
|
|
// 将物品添加到按显示名称查找的字典 (这里仍然使用 itemResource.Name,因为字典的目的是按显示名称查找)
|
|
|
|
|
if (!_itemsByName.ContainsKey(itemResource.Name))
|
|
|
|
|
{
|
|
|
|
|
_itemsByName.Add(itemResource.Name, new List<Item.ItemResource>());
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-25 18:24:12 +08:00
|
|
|
|
_itemsByName[itemResource.Name].Add(itemResource);
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public Item.ItemResource GetItem(string defName)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-25 18:24:12 +08:00
|
|
|
|
return _items.GetValueOrDefault(defName, null);
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-08-25 18:24:12 +08:00
|
|
|
|
|
|
|
|
|
// FindItemByName 和 FindAllItemsByName 保持不变,因为它们是按显示名称查找的
|
|
|
|
|
public Item.ItemResource FindItemByName(string itemName)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (string.IsNullOrEmpty(itemName)) return null;
|
|
|
|
|
return _itemsByName.GetValueOrDefault(itemName)?.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Item.ItemResource> FindAllItemsByName(string itemName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(itemName)) return new List<Item.ItemResource>();
|
|
|
|
|
return _itemsByName.GetValueOrDefault(itemName, new List<Item.ItemResource>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_items.Clear();
|
|
|
|
|
_itemsByName.Clear();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-25 18:24:12 +08:00
|
|
|
|
|
|
|
|
|
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|