2025-08-26 16:00:58 +08:00
|
|
|
|
using System;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
using Data;
|
2025-08-26 16:00:58 +08:00
|
|
|
|
using Managers;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Item
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示游戏中的一个物品资源,包含了物品的各项基本属性和数据。
|
|
|
|
|
/// 这是一个只读的数据结构,用于存储物品的定义信息。
|
|
|
|
|
/// </summary>
|
2025-08-19 20:22:10 +08:00
|
|
|
|
public class ItemResource
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的定义名称,通常作为其唯一标识符。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string DefName { get; protected set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的显示名称(例如,在UI中显示的名称)。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name { get; protected set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的描述文本。
|
|
|
|
|
/// </summary>
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public string Description { get; protected set; }
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的图标精灵。
|
|
|
|
|
/// </summary>
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public Sprite Icon { get; protected set; }
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的稀有度。
|
|
|
|
|
/// </summary>
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public ItemRarity Rarity { get; protected set; }
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品的最大堆叠数量。
|
|
|
|
|
/// </summary>
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public int MaxStack { get; protected set; }
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 指示物品是否可以被装备。
|
|
|
|
|
/// </summary>
|
2025-08-25 18:24:12 +08:00
|
|
|
|
public bool IsEquippable { get; protected set; }
|
2025-08-26 16:00:58 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数:通过 ItemDef 对象初始化 ItemResource。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="def">物品的定义对象。</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException">如果传入的 ItemDef 为 null,则抛出此异常。</exception>
|
|
|
|
|
public ItemResource(ItemDef def)
|
2025-08-25 18:24:12 +08:00
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// 参数校验:在构造函数中进行参数非空检查至关重要,避免空引用异常。
|
|
|
|
|
if (def == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(def), "创建 ItemResource 时,ItemDef 不能为 null。");
|
|
|
|
|
}
|
2025-08-25 18:24:12 +08:00
|
|
|
|
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// 从 ItemDef 对象中直接赋值 ItemResource 的所有属性。
|
|
|
|
|
// 这将创建 ItemResource 的逻辑完全封装在类内部,外部调用方无需关心具体属性的提取过程。
|
|
|
|
|
DefName = def.defName;
|
|
|
|
|
Name = def.label;
|
|
|
|
|
Description = def.description;
|
|
|
|
|
Icon = PackagesImageManager.Instance.GetSprite(def.texture);
|
|
|
|
|
Rarity = def.rarity;
|
|
|
|
|
MaxStack = def.maxStack;
|
|
|
|
|
IsEquippable = def.ssEquippable;
|
|
|
|
|
if (!Icon && def.texture == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"ItemResource: Failed to load sprite for texture '{def.texture}' for item '{def.defName}'. Icon will be null.");
|
|
|
|
|
}
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-08-26 16:00:58 +08:00
|
|
|
|
}
|