Generic, Dictionary, Json을 이용한 데이터 처리
C#/수업내용 2019. 4. 4. 16:521 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 RawData { public int id; public RawData() { } } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class WeaponData : RawData { public string name; public int level; public int damage_min; public int damage_max; public WeaponData(int id, string name, int level, int min, int max) { this.id = id; this.name = name; this.level = level; this.damage_min = min; this.damage_max = max; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class WeaponInfo { public int id; public int damage; public WeaponInfo(int id, int damage_min, int damage_max) { Random rand = new Random(); this.id = id; this.damage = rand.Next(damage_min, damage_max); } } } | 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 WeaponItem { public WeaponInfo info; public WeaponItem(WeaponInfo 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class ArmorData :RawData { public string name; public int level; public int armor_min; public int armor_max; public ArmorData(int id, string name, int level, int min, int max) { this.id = id; 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class ArmorInfo :RawData { public int armor; public ArmorInfo(int id, int armor_min, int armor_max) { Random rand = new Random(); this.id = id; this.armor = rand.Next(armor_min, armor_max); } } } | 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 ArmorItem { public ArmorInfo info; public ArmorItem(ArmorInfo 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class CharStatData :RawData { public string name; public int main_stat_id; public float main_stat_val; public int sub_stat_id; public float sub_stat_val; public CharStatData(int id, string name, int main_stat_id, float main_stat_val, int sub_stat_id, float sub_stat_val) { this.id = id; this.name = name; this.main_stat_id = main_stat_id; this.main_stat_val = main_stat_val; this.sub_stat_id = sub_stat_id; this.sub_stat_val = sub_stat_val; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_0404 { public class StatData :RawData { public string name; public StatData(int id, string name) { this.id = id; this.name = name; } } } | 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 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 | 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 { public Dictionary<int, WeaponData> dicWeaponItem; public Dictionary<int, ArmorData> dicArmorItem; public Dictionary<int, StatData> dicStatData; public Dictionary<int, CharStatData> dicCharStatData; public App() { dicWeaponItem = new Dictionary<int, WeaponData>(); dicArmorItem = new Dictionary<int, ArmorData>(); dicStatData = new Dictionary<int, StatData>(); dicCharStatData = new Dictionary<int, CharStatData>(); } public void Start() { LoadData("./Data/weapon_data.json", dicWeaponItem); LoadData("./Data/armor_data.json", dicArmorItem); LoadData("./Data/stat_data.json", dicStatData); LoadData("./Data/char_stat_data.json", dicCharStatData); for(int i = 0; i < 5; i++) { Console.WriteLine("{0}",dicWeaponItem[i].name); } for (int i = 0; i < 5; i++) { Console.WriteLine("{0}",dicArmorItem[i].name); } for (int i = 100; i < 105; i++) { Console.WriteLine("{0}",dicStatData[i].name); } for (int i = 0; i < 5; i++) { Console.WriteLine("{0}{1}",dicCharStatData[i].id, dicCharStatData[i].name); } } //data load method public void LoadData<T>(string path, Dictionary<int, T> dictionary) where T : RawData { var str = File.ReadAllText(path); var arrJson = JsonConvert.DeserializeObject<T[]>(str); foreach(var json in arrJson) { dictionary.Add(json.id, json); } } //아이템 인스턴스화 /* public WeaponItem 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 WeaponItem(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 WeaponItem(info); } return default(WeaponItem); } */ } } | cs |
'C# > 수업내용' 카테고리의 다른 글
2D Animation, Mecanim, Background (0) | 2019.05.07 |
---|---|
Damage Text 출력하기 (0) | 2019.04.25 |
ItemData구현 (0) | 2019.04.04 |
Dictionary를 이용한 Inventory구현 및 Json사용 (0) | 2019.04.03 |
Inventory구현 (창고이동, 제작, 목록 등등) (0) | 2019.04.02 |