バックパック システム、辞書 & オーガナイザー
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Item
{
public int autoID;
public int itemID;
public string name;
public int count;
public int endurance;
public Item(int itemID,string name,int count=1,int endurance = 0)
{
this.itemID = itemID;
this.name = name;
this.count = count;
this.endurance = endurance;
}
public override string ToString()
{
return $"{
{
{autoID}:{itemID}-{name} 数量:{count}耐久:{endurance}}}";
}
}
//背包,道具管理器
class ItemManager
{
Dictionary<int, Item> dict = new Dictionary<int, Item>();
Dictionary<string, List<Item>> dictName = new Dictionary<string, List<Item>>();
int counter = 1;
//添加道具
public Item AddItem(Item item)
{
if(item.autoID != 0)
{
Console.WriteLine("道具autoID不为0,不能加入背包");
return null;
}
item.autoID = counter;
counter++;
dict.Add(item.autoID, item);
//加入第二个名字字典
if (!dictName.ContainsKey(item.name))
{
dictName.Add(item.name, new List<Item>());
}
dictName[item.name].Add(item);
//if (dictName.ContainsKey(item.name))
//{
// dictName[item.name].Add(item);
//}
//else
//{
// List<Item> t = new List<Item>();
// t.Add(item);
// dictName.Add(item.name, t);
//}
return item;
}
//删除道具
public bool RemoveItem(int id)
{
if (!dict.ContainsKey(id))
{
Console.WriteLine("未找到道具:" + id);
return false;
}
Item item = dict[id];
dict.Remove(id);
//从第二个字典中删除
//列表remove写法
//dictName[item.name].Remove(item);
//列表remove at 写法
var t = dictName[item.name];
for(int i = 0; i < t.Count; i++)
{
if(t[i].autoID == id)
{
t.RemoveAt(i);
}
}
return true;
}
//查找道具
public Item FindItem(int autoid)
{
if (!dict.ContainsKey(autoid))
{
Console.WriteLine("未找到道具:" + autoid);
return null;
}
return dict[autoid];
}
//根据名字查找道具
public List<Item> FindItemByName(string name)
{
//List<Item> list = new List<Item>();
//foreach(var pair in dict)
//{
// if (pair.Value.name == name)
// {
// list.Add(pair.Value);
// }
//}
if (!dictName.ContainsKey(name))
{
Console.WriteLine("未找到道具:" + name);
return null;
}
return dictName[name];
return list;
}
public void PrintAll()
{
Console.WriteLine("------------");
foreach(var pair in dict)
{
Console.WriteLine("Key:"+pair.Key +"Value:" + pair.Value);
}
Console.WriteLine("------dictName------");
foreach (var pair in dictName)
{
Console.WriteLine(pair.Key + " :");
foreach(var item in pair.Value)
{
Console.WriteLine($"{pair.Key}:{item}");
}
}
Console.WriteLine("============");
}
}
class Program
{
static ItemManager itemManager; //背包
static void Main(string[] args)
{
itemManager = new ItemManager();
Item item1 = new Item(1001, "红药水", 10);
Item item2 = new Item(1001, "红药水", 5);
Item item3 = new Item(1003, "铜剑", 1, 100);
Item item4 = new Item(1003, "草鞋", 1, 90);
Item item5 = new Item(1005, "草鞋", 1, 80);
itemManager.AddItem(item1);
itemManager.AddItem(item2);
itemManager.AddItem(item3);
itemManager.AddItem(item4);
itemManager.AddItem(item5);
//打印背包内容
itemManager.PrintAll();
itemManager.RemoveItem(4);
itemManager.PrintAll();
List<Item> list = itemManager.FindItemByName("红药水");
Console.ReadKey();
}
}
}
バックパック システム 1: JSON とデータ管理
{
"1001": {
"ID": "1001",
"Type": "1",
"Name": "剑",
"Attack": "75",
"Crit": "0.5",
"Defense": "0",
"Dodge": "0",
"ModelPath": "Model/Capsule",
"ImagePath": "Image/剑"
},
"1002": {
"ID": "1002",
"Type": "1",
"Name": "刀",
"Attack": "60",
"Crit": "0.8",
"Defense": "0",
"Dodge": "0",
"ModelPath": "Model/Cube",
"ImagePath": "Image/刀"
},
"1003": {
"ID": "1003",
"Type": "1",
"Name": "衣服",
"Attack": "50",
"Crit": "0.2",
"Defense": "0",
"Dodge": "0",
"ModelPath": "Model/Cylinder",
"ImagePath": "Image/衣服"
},
"1004": {
"ID": "1004",
"Type": "2",
"Name": "盔甲",
"Attack": "0",
"Crit": "0",
"Defense": "100",
"Dodge": "0.2",
"ModelPath": "Model/Sphere",
"ImagePath": "Image/盔甲"
}
}