using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Data;

namespace LoadAndSaveGame
{
    public class App
    {
        Hero hero;
        public App()
        {
            GameInfo gameInfo = null;
            bool isNewbie = true;
            if (File.Exists("./game_info.json"))
            {
                //있다
                Console.WriteLine("기존 유저");
                isNewbie = false;
                //기존 유저
                //game_info.json파일을 읽어와 역직렬화 -> GameInfo
                string json = File.ReadAllText("./game_info.json");
                Console.WriteLine(json);
                gameInfo = JsonConvert.DeserializeObject<GameInfo>(json);

                Console.WriteLine("아이템 정보: {0}", gameInfo.itemInfos.Count);

            }
            else
            {
                //신규유저
                //새로운 GameInfo객체를 생성해서 기본값으로 설정한 뒤 저장

                //Game_info.json파일을 읽어와 역직렬화 -> GameInfo객체

                Console.WriteLine("신규 유저");
                isNewbie = true;
                gameInfo = new GameInfo();
                gameInfo.init();

                //직렬화
                string json = JsonConvert.SerializeObject(gameInfo);
                Console.WriteLine(json);
                File.WriteAllText("./game_info.json", json);
                Console.WriteLine("파일 저장 완료");
            }

          


            InfoManager.instance.Init(gameInfo);

            //------------------------------------
            //게임시작
            //-------------------------------------

            this.hero = new Hero();

            //아이템 획득
            if(isNewbie == false)
            {
                foreach (var info in InfoManager.instance.gameInfo.itemInfos)
                {
                    Console.WriteLine("{0}, {1}", info.id, info.damage);
                    this.hero.AddItem(new Item(info));
                }

                List<Item> items = this.hero.GetItems();
                foreach (var item in items)
                {
                    Console.WriteLine("{0}:{1}", item.Info.id, item.Info.damage);
                }
            }


        }
        public Item GetItem(int id)
        {
            //ItemData data = DataManager.instance.GetItemData(id);
            return  new Item(new ItemInfo(id, 12));
        }
        public void SaveItems(List<Item> items)
        {

            List<ItemInfo> itemInfos = new List<ItemInfo>();
            foreach(Item item in items)
            {
                Console.WriteLine("id: {0}, damage: {1}",item.Info.id,item.Info.damage);
            }
            InfoManager.instance.gameInfo.itemInfos = itemInfos;
            //직렬화
            string json = JsonConvert.SerializeObject(InfoManager.instance.gameInfo);
            Console.WriteLine(json);
            File.WriteAllText("./game_info.json", json);
            Console.WriteLine("아이템 정보 저장 완료");
        }
        
    }
    
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadAndSaveGame
{
    public class GameInfo
    {
        //인벤토리의 아이템 정보들  ItemInfo[]
        public List<ItemInfo> itemInfos;



        //몬스터 정보들
        //영웅의 정보
        //유저의 ID



        public GameInfo() 
        {

        }

        //초기화

        public void init()
        {
            this.itemInfos = new List<ItemInfo>();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadAndSaveGame
{
    public class Hero
    {
        List<Item> items = new List<Item>();

        public Hero() { }

        public void AddItem(Item item)
        {
            this.items.Add(item);
        }

        public List<Item> GetItems()
        {
            return items;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadAndSaveGame
{
    public class InfoManager
    {
        public static readonly InfoManager instance = new InfoManager();
        public GameInfo gameInfo;
        private InfoManager() {
            //생성자
        }

        public void Init(GameInfo gameInfo)
        {
            this.gameInfo = gameInfo;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadAndSaveGame
{
    public class Item
    {
        
        public ItemInfo Info
        {
            get;
            private set;
        }



        //생성자
        public Item(ItemInfo info)
        {
            this.Info = info;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadAndSaveGame
{
    public class ItemInfo
    {
        public int id;
        public int damage;
        public ItemInfo(int id, int damage)
        {
            this.id = id;
            this.damage = damage;

        }
    }
}

+ Recent posts