using Godot; using System; using System.Collections.Generic; namespace Cosmobox { public partial class BagItemList { public List 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; } } }