背包物品的获取,添加,消耗
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;//添加到背包中items.Add(item);}return true;}//消耗物品public bool RemoveItem(int id){//判断游戏是否存在这一物品if (ItemManager.Instance.GetItemData(id) == null){return false;}//判断要消耗的物品Item item = GetItem(id);if (item != null){//消耗item.count--;if (item.count <= 0){items.Remove(item);Game.player.maxHp += 20;Game.player.currentHp = Game.player.maxHp;}return true;}else{Debug.Log(\"你没有这种东西\");return false;}}//输出物品集合public void Out(){Debug.Log(\"你的背包里有:\",ConsoleColor.Yellow);foreach (Item a in items){Console.ForegroundColor = ConsoleColor.Yellow;Console.Write(a.name + \"x\" + a.count+\" \");Console.ForegroundColor = ConsoleColor.White;}Console.WriteLine();}}}
背包物品的属性
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _7._24.Item{//物品类class Item{//物品idpublic int id;public string name;public int count;}}
游戏内物品的各种数据
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _7._24.Item{abstract class ItemData{public int id;public string name;public string des;public int price;public abstract void Start();}abstract class EquipData : ItemData{public int atk;public int def;}class A : ItemData{public A(){id = 1011;name = \"小血瓶\";des = \"回一点点血量\";price = 50;}public override void Start(){}}class B : ItemData{public B(){id = 1012;name = \"血瓶\";des = \"回血量\";price = 100;}public override void Start(){}}class C : ItemData{public C(){id = 1013;name = \"大血瓶\";des = \"回大量血量\";price = 150;}public override void Start(){}}class Dao : EquipData{public Dao(){atk = 100;def = 100;id = 1001;name = \"刀\";price = 100;des = \"很普通\";}public override void Start(){}}}
游戏内物品的字典库
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _7._24.Item{class ItemManager{private static ItemManager instance;public static ItemManager Instance{get{if (instance == null){instance = new ItemManager();}return instance;}}//物品id 物品数据public Dictionary<int, ItemData> itemdic = new Dictionary<int, ItemData>();private ItemManager(){ItemData itemData1 = new Dao();ItemData itemData2 = new A();ItemData itemData3 = new B();ItemData itemData4 = new C();itemdic.Add(itemData1.id, itemData1);itemdic.Add(itemData2.id, itemData2);itemdic.Add(itemData3.id, itemData3);itemdic.Add(itemData4.id, itemData4);}public ItemData GetItemData(int id){if (itemdic.ContainsKey(id)){return itemdic[id];}else{return null;}}}}