c#/수업 내용

인벤토리 만들기

yeeendy 2023. 1. 16. 15:14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study14
{
    class Item
    {
        public ItemInfo info;
        public string name;
        public int damage;
        //생성자
        public Item()
        {

        }
        //overloading
        //public Item(ItemData itemData) { } //dictionary를 통해 id를 키로 data를 가져올 수 있기 때문에 필요 없음
        public Item(ItemInfo itemInfo)
        {

        }
    }
}​
using System;
using System.Collections.Generic;

namespace Study14
{
    class GameInfo
    {
        //생성자
        public GameInfo()
        {

        }
    }
}​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study14
{
    class Game
    {
        Hero hero;
        private Dictionary<int, ItemData> dicItemDatas;
        
        //생성자
        public Game()
        {
            
        }

        //초기화
        public void Init(Dictionary<int, ItemData> dicItemDatas)
        {
            this.dicItemDatas = dicItemDatas;
        }
        public void Run()
        {
            this.CreateHero();

            Item item = new Item(new ItemInfo(100,10));
            this.hero.GetItem(item); //아이템 획득
            this.hero.OpenInventory(); //인벤토리에 있는 모든 아이템 출력
        }
        private void CreateHero()
        {
            //영웅 생성
            this.hero = new Hero();
            //인벤토리 생성
            var inven = new Inventory();
            //신규유저
            inven.Init(this.dicItemDatas);
            this.hero.Init(inven);

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study14
{
    class Inventory
    {
        //아이템들 관리
        List<Item> items;
        Dictionary<int, ItemData> dicItemDatas;

        //생성자
        public Inventory()
        {
            this.items = new List<Item>(); //컬렉션 초기화
        }

        public void Init(Dictionary<int, ItemData> dicItemDatas)
        {
            this.dicItemDatas = dicItemDatas;
        }
        public void AddItem(Item item)
        {
            this.items.Add(item);
        }

        public void PrintAllItems()
        {
            //Console.WriteLine(this.dicItemDatas);  //null이 출력된다.
            foreach (var item in this.items)
            {
                ItemData data = this.dicItemDatas[item.info.id];
                Console.WriteLine("{0} {1} {2}", item.info.id, data.name, item.info.damage);
            }
        }
    }
}​
using System;
using System.Collections.Generic;


namespace Study14
{
    class ItemInfo
    {
        public int id;
        public int damage;
        //생성자
        public ItemInfo(int id, int damage)
        {
            this.id = id;
            this.damage = damage;
        }
    }
}
using System;


namespace Study14
{
    class ItemData
        //매핑클래스는 생성자 불필요
    {
        public int id;
        public string name;
        public int damage;
    }
}​
using System;
using System.Collections.Generic;


namespace Study14
{
    class Hero
    {
        public Inventory inven;    
        //생성자
        public Hero()
        {

        }
        public void Init(Inventory inven)
        {
            this.inven = inven;
        }
        public void GetItem(Item item)
        {
            this.inven.AddItem(item);
        }
        public void OpenInventory()
        {
            this.inven.PrintAllItems();
        }
    }
}
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;

namespace Study14
{
    class App
    {
        
        private GameInfo gameInfo;
        private Game game;
        private Dictionary<int, ItemData> dicItemDatas;

        //생성자
        public App()
        {
            this.LoadItemData();

            this.Initialize(); //초기화

            this.game = new Game();
            this.game.Init(this.dicItemDatas); //게임 초기화
            this.game.Run();  //게임 실행
        }

        private void Initialize()
        {
            if (this.IsNewbie())
            {
                Console.WriteLine("신규유저");
                //새로운 GameInfo객체를 만들어서 직렬화 하고 파일로 저장
                this.gameInfo = new GameInfo();
                var json = JsonConvert.SerializeObject(this.gameInfo);
                File.WriteAllText("./game_info.json", json);
            }
            else
            {
                Console.WriteLine("기존유저");
                //game_info.json 파일 불러오고 역직렬화 한다
                var json = File.ReadAllText("./game_info.json");
                this.gameInfo = JsonConvert.DeserializeObject<GameInfo>(json);
                Console.WriteLine(json);
            }
        }
        private bool IsNewbie()
        {
            return !File.Exists("./game_info.json");
        }

        private void LoadItemData()
        {
            var path = "./item_data.json";
            var json = File.ReadAllText(path);
            var itemDatas = JsonConvert.DeserializeObject<ItemData[]>(json);
            var dicItemDatas = itemDatas.ToDictionary(x => x.id);
            Console.WriteLine("item_data.json 로드 완료");
            Console.WriteLine(dicItemDatas.Count);
            foreach (var pair in dicItemDatas)
            {
                ItemData data = pair.Value;
                Console.WriteLine("{0} {1} {2}", data.id, data.name, data.damage);
            }

        }
    }
}

Inventory에서 null값 나오는 거 해결못함

'c# > 수업 내용' 카테고리의 다른 글

유니티  (0) 2023.01.26
LCRSTree  (0) 2023.01.17
변하는 데이터 만들기 연습  (0) 2023.01.15
변하지 않는 데이터 만들기 연습 2 (Mission, Item)  (0) 2023.01.13
230113  (0) 2023.01.13