(client) chore
This commit is contained in:
@ -1,41 +1,71 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Managers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Item
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示游戏中的一个物品资源,包含了物品的各项基本属性和数据。
|
||||
/// 这是一个只读的数据结构,用于存储物品的定义信息。
|
||||
/// </summary>
|
||||
public class ItemResource
|
||||
{
|
||||
public string DefName { get; protected set; } // 新增:物品的定义名称,唯一标识符
|
||||
public string Name { get; protected set; } // 物品的显示名称 (来自 label)
|
||||
/// <summary>
|
||||
/// 物品的定义名称,通常作为其唯一标识符。
|
||||
/// </summary>
|
||||
public string DefName { get; protected set; }
|
||||
/// <summary>
|
||||
/// 物品的显示名称(例如,在UI中显示的名称)。
|
||||
/// </summary>
|
||||
public string Name { get; protected set; }
|
||||
/// <summary>
|
||||
/// 物品的描述文本。
|
||||
/// </summary>
|
||||
public string Description { get; protected set; }
|
||||
/// <summary>
|
||||
/// 物品的图标精灵。
|
||||
/// </summary>
|
||||
public Sprite Icon { get; protected set; }
|
||||
/// <summary>
|
||||
/// 物品的稀有度。
|
||||
/// </summary>
|
||||
public ItemRarity Rarity { get; protected set; }
|
||||
/// <summary>
|
||||
/// 物品的最大堆叠数量。
|
||||
/// </summary>
|
||||
public int MaxStack { get; protected set; }
|
||||
/// <summary>
|
||||
/// 指示物品是否可以被装备。
|
||||
/// </summary>
|
||||
public bool IsEquippable { get; protected set; }
|
||||
|
||||
// 构造函数,现在接受 defName
|
||||
public ItemResource(string defName, string name, string description, Sprite icon, ItemRarity rarity, int maxStack, bool isEquippable)
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:通过 ItemDef 对象初始化 ItemResource。
|
||||
/// </summary>
|
||||
/// <param name="def">物品的定义对象。</param>
|
||||
/// <exception cref="ArgumentNullException">如果传入的 ItemDef 为 null,则抛出此异常。</exception>
|
||||
public ItemResource(ItemDef def)
|
||||
{
|
||||
DefName = defName; // 赋值 defName
|
||||
Name = name;
|
||||
Description = description;
|
||||
Icon = icon;
|
||||
Rarity = rarity;
|
||||
MaxStack = maxStack;
|
||||
IsEquippable = isEquippable;
|
||||
// 参数校验:在构造函数中进行参数非空检查至关重要,避免空引用异常。
|
||||
if (def == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(def), "创建 ItemResource 时,ItemDef 不能为 null。");
|
||||
}
|
||||
|
||||
// 从 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class WeaponResource : ItemResource
|
||||
{
|
||||
public AttributesDef Attributes { get; private set; }
|
||||
|
||||
// 构造函数,调用基类构造函数并传递 defName
|
||||
public WeaponResource(string defName, string name, string description, Sprite icon, ItemRarity rarity, int maxStack, bool isEquippable, AttributesDef attributes)
|
||||
: base(defName, name, description, icon, rarity, maxStack, isEquippable)
|
||||
{
|
||||
Attributes = attributes;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
33
Client/Assets/Scripts/Item/WeaponResource.cs
Normal file
33
Client/Assets/Scripts/Item/WeaponResource.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Entity;
|
||||
|
||||
namespace Item
|
||||
{
|
||||
public class WeaponResource : ItemResource
|
||||
{
|
||||
public Attributes Attributes { get; private set; }
|
||||
|
||||
public WeaponType Type { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:通过 WeaponDef 对象初始化 WeaponResource。
|
||||
/// </summary>
|
||||
/// <param name="def">武器的定义对象。</param>
|
||||
/// <exception cref="ArgumentNullException">如果传入的 WeaponDef 为 null,则抛出此异常。</exception>
|
||||
public WeaponResource(WeaponDef def)
|
||||
: base(def) // 调用基类 ItemResource 的构造函数,直接传入 WeaponDef。
|
||||
{
|
||||
// 参数校验:确保 WeaponDef 对象不为空,避免空引用异常。
|
||||
if (def == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(def), "创建 WeaponResource 时,WeaponDef 不能为 null。");
|
||||
}
|
||||
|
||||
// 初始化武器的属性。Attributes 对象通过 WeaponDef 中的属性数据进行构建。
|
||||
Attributes = new Attributes(def.attributes);
|
||||
// 初始化武器类型,直接从 WeaponDef 定义中获取。
|
||||
Type = def.type;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Item/WeaponResource.cs.meta
Normal file
3
Client/Assets/Scripts/Item/WeaponResource.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa734e2254034fa4a59fb1e0cdea250f
|
||||
timeCreated: 1756193577
|
Reference in New Issue
Block a user