94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
namespace BeiBao
|
|
{
|
|
/// <summary>
|
|
/// 定义物品类
|
|
/// 每个物品有名称、ID、最大堆叠数量和当前数量。
|
|
/// </summary>
|
|
public class Item
|
|
{
|
|
private string _itemName;
|
|
private int _itemID;
|
|
private int _maxStack;
|
|
private int _quantity;
|
|
|
|
/// <summary>
|
|
/// 名字
|
|
/// </summary>
|
|
public string ItemName => _itemName;
|
|
|
|
/// <summary>
|
|
/// ID
|
|
/// </summary>
|
|
public int ItemID => _itemID;
|
|
|
|
/// <summary>
|
|
/// 最大堆叠数量
|
|
/// </summary>
|
|
public int MaxStack => _maxStack;
|
|
|
|
/// <summary>
|
|
/// 当前数量
|
|
/// </summary>
|
|
public int Quantity => _quantity;
|
|
|
|
public Item(string itemName, int itemID, int maxStack)
|
|
{
|
|
_itemName = itemName;
|
|
_itemID = itemID;
|
|
_maxStack = maxStack;
|
|
_quantity = 0;
|
|
}
|
|
|
|
// 尝试增加数量,返回剩余未能堆叠的数量
|
|
public int AddQuantity(int quantity)
|
|
{
|
|
int remaining = quantity;
|
|
if (Quantity + remaining > MaxStack)
|
|
{
|
|
remaining = (Quantity + quantity) - MaxStack;
|
|
_quantity = MaxStack;
|
|
}
|
|
else
|
|
{
|
|
_quantity += remaining;
|
|
remaining = 0;
|
|
}
|
|
|
|
return remaining;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 仅作为检查,并不发生实际的数据改变
|
|
/// </summary>
|
|
/// <param name="quantity"></param>
|
|
/// <returns></returns>
|
|
public int CheckAddQuantity(int quantity)
|
|
{
|
|
int remaining = quantity;
|
|
return Quantity + remaining > MaxStack ? remaining = (Quantity + quantity) - MaxStack : 0;
|
|
}
|
|
|
|
// 尝试移除数量,返回是否成功
|
|
public bool RemoveQuantity(int quantity)
|
|
{
|
|
if (Quantity >= quantity)
|
|
{
|
|
_quantity -= quantity;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 仅作为检查,并不发生实际的数据改变
|
|
/// </summary>
|
|
/// <param name="quantity"></param>
|
|
/// <returns></returns>
|
|
public int CheckRemoveQuantity(int quantity)
|
|
{
|
|
int tmp = quantity - _quantity;
|
|
return tmp <= 0 ? 0 : tmp;
|
|
}
|
|
}
|
|
} |