(미완성)인벤토리 만들기
C#/미완성2019. 4. 8. 03:08
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json; namespace ConsoleApp3 { public class WeaponInfo : ItemInfo { public int max_level; public string name; public int type; public float dps_min; public float dps_max; public float attack_speed; public WeaponInfo(int id) : base(id) { } } } | 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 ConsoleApp3 { public class Weapon : Item { new public WeaponInfo info; public Weapon(ItemInfo info) : base(info) { this.info = (WeaponInfo)info; } } } | 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 ConsoleApp3 { public class ItemInfo { public int id; public ItemInfo(int id) { this.id = id; } } } | 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 ConsoleApp3 { public class Item { public ItemInfo itemInfo; public Item(ItemInfo itemInfo) { this.itemInfo = itemInfo; } } } | 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 85 86 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.IO; namespace ConsoleApp3 { public class GameLauncher { private InventoryInfo inventoryInfo; private Inventory inventory; private Dictionary<int, Weapon> dicWeaponData = new Dictionary<int, Weapon>(); public GameLauncher() { } public void CreateInventory() { this.inventoryInfo = new InventoryInfo(); } public void StartGame() { while (true) { Console.Clear(); Console.WriteLine("[1:아이템생성 과 인벤추가 ,2:인벤목록출력 ,3: 아이템꺼내기 ,4:저장후 종료]"); var inputKey = Console.ReadLine(); Console.Write(" "); switch (inputKey) { case "1": Console.WriteLine("아이템 추가"); inventory.AddItem(dicWeaponData); //this.inventoryInfo = inventory.AddItem(dicWeaponData); break; case "2": Console.WriteLine("[인벤토리]"); break; case "3": Console.WriteLine("아이템꺼내기"); Console.WriteLine("꺼내실 아이템을 입력해주세요."); var inputStr = Console.ReadLine(); break; case "4": Console.WriteLine("아이템검색"); Console.WriteLine("검색하실 아이템명을 입력해주세요."); var foundItem = Console.ReadLine(); var item = inventory.FindItemByName(foundItem); if (item != null) { Console.WriteLine("해당아이템이 존재합니다."); } else { Console.WriteLine("해당아이템이 존재하지않습니다."); } Console.WriteLine("전 단계로 돌아가시려면 아무키나 입력하세요"); Console.ReadKey(); break; case "5": Console.WriteLine("\t\t저장 후 게임종료"); //this.SaveAndExit(); break; } } } public void SaveAndExit(InventoryInfo inventoryInfo) { //File.WriteAllText("./info/user_info.json", json, Encoding.UTF8); Console.WriteLine("데이터가 저장되었습니다."); //Thread.Sleep(1000); Console.WriteLine("End"); Console.ReadKey(); System.Environment.Exit(0); } } } | 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 ConsoleApp3 { public class InventoryInfo { public List<ItemInfo> itemInfos; public int itemCount; private List<Item> itemList; public InventoryInfo() { this.itemList = new List<Item>(); } } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp3 { public class Inventory { public InventoryInfo info; public Inventory(Dictionary<int, WeaponInfo> dicWeaponData) { } public void AddItem(Dictionary<int, Weapon> dic) { Console.WriteLine("추가하실 아이템을 선택해주세요"); Console.WriteLine("1.왕시해자 2.고딕검 3.큰날 4.신검 5.군주의검"); var inputkey = Console.ReadLine(); if (inputkey == "1" || inputkey == "왕시해자") { } else if(inputkey == "2" || inputkey == "고딕검") { } else if (inputkey == "3" || inputkey == "큰날") { } else if (inputkey == "4" || inputkey == "신검") { } else if (inputkey == "5" || inputkey == "군주의검") { } else if (inputkey == "0" || inputkey == "뒤로가기") { } else { Console.WriteLine("잘못입력하셨습니다."); } } public Item FindItemByName(string name) { return null; } public void RemoveItem(string 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json; namespace ConsoleApp3 { public class App { private Inventory inventory; //private InventoryInfo InventoryInfo; private GameLauncher gameLauncher; Dictionary<int, WeaponInfo> dicWeaponData; public App() { this.dicWeaponData = new Dictionary<int, WeaponInfo>(); this.gameLauncher = new GameLauncher(); } public void Start() { this.Init(); //gameLauncher.SaveAndExit(new InventoryInfo()); } public void Init() { if (Directory.Exists("./data")) { Console.WriteLine("기존유저입니다"); var weaponJson = File.ReadAllText("./data/weapon_data.json"); var arrWeaponData = JsonConvert.DeserializeObject<WeaponInfo[]>(weaponJson); //foreach(var data in arrWeaponData) //{ // dicWeaponData.Add(data.id, data); //} this.inventory = new Inventory(dicWeaponData); } else { Console.WriteLine("신규유저입니다"); Directory.CreateDirectory("./data"); var json = JsonConvert.SerializeObject(this.inventory); File.WriteAllText("./data/weapon_data.json", json, Encoding.UTF8); var weaponJson = File.ReadAllText("./data/weapon_data.json"); var arrWeaponData = JsonConvert.DeserializeObject<WeaponInfo>(weaponJson); } } public void GameStart() { this.gameLauncher.StartGame(); } } } | 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 ConsoleApp3 { class Program { static void Main(string[] args) { //var app = new App(); //app.Start(); new App().Start(); } } } |
인벤토리와 json파일을 연결하는 것
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
#미완성#인벤토리 (0) | 2019.03.30 |
Array[배열] #미완성 : 메소드예시 (0) | 2019.03.27 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |