ItemData구현

C#/수업내용 2019. 4. 4. 15:07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Study_0404
{
    public class App
    {
        private string[] arrDataName = { "weapon_data.json""armor_data.json""stat_data.json""char_stat_data.json" };
        public Dictionary<int, ItemData> dicWeaponItem;
        public Dictionary<int, ItemData> dicArmorItem;
        public List<Item> inventory;
 
        public App()
        {
            dicWeaponItem = new Dictionary<int, ItemData>();
            dicArmorItem = new Dictionary<int, ItemData>();
            inventory = new List<Item>();
        }
 
 
        public void Start()
        {
            this.LoadAllData();
            for(int index=0; index<6; index++)
            {
                inventory.Add(this.CreateItem(index, 0));
                inventory.Add(this.CreateItem(index, 1));
 
            }
            for(int index = 0; index < inventory.Count; index++)
            {
                Console.WriteLine("pointer id : {0}", index);
                if(inventory[index].info.type == 0)
                {
                    Console.WriteLine("{0}.{1} Lv.{2} Dmg.{3}", inventory[index].info.id, dicWeaponItem[inventory[index].info.id].name, dicWeaponItem[inventory[index].info.id].level, inventory[index].info.damage);
                }
                if(inventory[index].info.type == 1)
                {
 
                    Console.WriteLine("{0}.{1} Lv.{2} Arm.{3}", inventory[index].info.id, dicArmorItem[inventory[index].info.id].name, dicArmorItem[inventory[index].info.id].level, inventory[index].info.armor);
                }
            }
        }
 
        //data load method
        public void LoadAllData() 
        {
            for(int i = 0; i<this.arrDataName.Length; i++)
            {
                var fileName = this.arrDataName[i];
                string path = string.Format("./Data/{0}", arrDataName[i]);
            }
            string weaponDataPath = "./Data/weapon_data.json";
            bool isExist = true;
            if (isExist)
            {
                var weapon = File.ReadAllText(weaponDataPath);
                var weaponJson = JsonConvert.DeserializeObject<ItemData[]>(weapon);
                foreach (var data in weaponJson)
                {
                    var itemData = new ItemData(data.id, data.type, data.name, data.level, data.damage_min, data.damage_max);
                    dicWeaponItem.Add(itemData.id, itemData);
                }
            }
            string armorDataPath = "./Data/armor_data.json";
            isExist = File.Exists(armorDataPath);
            if (isExist)
            {
                var armor = File.ReadAllText(armorDataPath);
                var armorJson = JsonConvert.DeserializeObject<ItemData[]>(armor);
                foreach(var data in armorJson)
                {
                    var itemData = new ItemData(data.id, data.type, data.name, data.level, data.armor_min, data.armor_max);
                    dicArmorItem.Add(itemData.id, itemData);
                }
            }            
 
        }
 
        public Item CreateItem(int id, int type)
        {
            Random rand = new Random();
            if(type == 0)
            {
                int damageOrArmor = rand.Next(dicWeaponItem[id].damage_min, dicWeaponItem[id].damage_max);
                ItemInfo info = new ItemInfo(id, type, damageOrArmor);
                return new Item(info);
            }
            else if(type == 1)
            {
                int damageOrArmor = rand.Next(dicArmorItem[id].armor_min, dicArmorItem[id].armor_max);
                ItemInfo info = new ItemInfo(id, type, damageOrArmor);
                return new Item(info);
            }
            return default(Item);
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_0404
{
    public class Item
    {
        public ItemInfo info;
        public Item(ItemInfo info)
        {
            this.info = info;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_0404
{
    public class ItemData
    {
        public int id;
        public int type;
        public string name;
        public int level;
        public int damage_min;
        public int damage_max;
        public int armor_min;
        public int armor_max;
 
        public ItemData(int id, int type, string name, int level, int min, int max)
        {
            if(type==0)
            {
                this.id = id;
                this.type = type;
                this.name = name;
                this.level = level;
                this.damage_min = min;
                this.damage_max = max;
            }
            else if(type==1)
            {
                this.id = id;
                this.type = type;
                this.name = name;
                this.level = level;
                this.armor_min = min;
                this.armor_max = max;
 
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_0404
{
    public class ItemInfo
    {
        public int id;
        public int type;
        public int damage;
        public int armor;
        public ItemInfo(int id, int type, int damageOrArmor)
        {
            if (type == 0)
            {
                this.id = id;
                this.type = type;
                this.damage = damageOrArmor;
            }
            else if (type == 1)
            {
                this.id = id;
                this.type = type;
                this.armor = damageOrArmor;
            }
 
        }
    }
}
 
cs


: