背包物品的获取,添加,消耗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _7._24.Item
{
//背包类
class Bag
{
//物品数组
public List<Item> items = new List<Item>();
//得到物品
public Item GetItem(int id)
{
foreach(Item item in items)
{
if (item.id == id)
{
return item;
}
}
return null;
}
//添加物品
public bool AddItem(int id)
{
if (ItemManager.Instance.GetItemData(id) == null)
{
return false;
}
Item item = GetItem(id);
//如果已经存在
if (item != null)
{
item.count++;
}
else
{
//如果不存在
item = new Item();
item.id = id;
item.name = ItemManager.Instance.GetItemData(id).name;
item.count = 1;