初次提交
This commit is contained in:
71
Script/Pawn/BagItemList.cs
Normal file
71
Script/Pawn/BagItemList.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public partial class BagItemList
|
||||
{
|
||||
public List<BagItem> Items = new();
|
||||
|
||||
// 查找物品
|
||||
public BagItem Find(string name)
|
||||
{
|
||||
foreach (var item in Items)
|
||||
{
|
||||
if (item.ItemName == name)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 添加物品
|
||||
public bool AddItem(ItemResource resource, int amount = 1)
|
||||
{
|
||||
if (resource == null || amount <= 0)
|
||||
{
|
||||
GD.PrintErr("Invalid item or amount.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否已有相同物品
|
||||
var existingItem = Find(resource.ItemName);
|
||||
if (existingItem != null && resource.Stackable)
|
||||
{
|
||||
existingItem.AddAmount(amount); // 堆叠物品
|
||||
}
|
||||
else
|
||||
{
|
||||
// 创建新物品条目
|
||||
var newItem = new BagItem
|
||||
{
|
||||
itemResource = resource,
|
||||
amount = amount
|
||||
};
|
||||
Items.Add(newItem);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 移除物品
|
||||
public bool RemoveItem(string name, int amount = 1)
|
||||
{
|
||||
var item = Find(name);
|
||||
if (item == null)
|
||||
{
|
||||
GD.Print($"Item '{name}' not found in bag.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (amount >= item.amount)
|
||||
{
|
||||
Items.Remove(item); // 完全移除物品
|
||||
return true;
|
||||
}
|
||||
|
||||
item.RemoveAmount(amount); // 减少数量
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user