Yang.공부방

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    //싱글톤 (인스턴스를 하나만 생성하여 어떠한 곳에서도 접근이 가능하게끔 만든다)
    public class DataManager
    {
        public static DataManager instance;
        public Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
 
        private DataManager()
        {
        }
 
        public static DataManager GetInstance()
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
            }
            return DataManager.instance; 
        }
 
        //파일읽는 메소드(역직렬화) : CharacterData 즉, 변하면 안되는 기본 데이터를 불러온다
        public void LoadDatas()
        {
 
            var json = File.ReadAllText("./data/character_data.json");
 
            var arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(json);
         
            foreach(var data in arrCharacterData)
            {
                dicCharacterData.Add(data.id, data);
            }
            Console.WriteLine("모든 데이터가 로드 되었습니다");
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    interface IMoveable //인터페이스
    {
        void Move(Vector2 position);
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class Vector2
    {
        public float x;
        public float y;
 
        public Vector2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
 
        public static Vector2 operator+  (Vector2 a, Vector2 b)
        {
            return new Vector2(a.x + b.x, a.y + b.y);
        }
 
        public static Vector2 operator -(Vector2 a, Vector2 b)
        {
            return new Vector2(a.x - b.x, a.y - b.y);
        }
 
        public static float GetDistance(Vector2 a, Vector2 b)
        {
            return (float)Math.Sqrt(Math.Pow(b.x - a.x, 2+ Math.Pow(b.y - a.y, 2));
        }
    }
}
 
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 Lol
{
    public class UserInfo //사용자의 데이터를 저장하는 클래스
    {
        public GameInfo gameInfo;
        public bool newBieCheck = true//신규유저인지 확인하는 변수
 
        public UserInfo()
        {
            this.gameInfo = new GameInfo();
        }
    }
}
 
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 Lol
{
    public class GameInfo
    {
        public List<CharacterInfo> characterInfos;
 
        public GameInfo()
        {
            this.characterInfos = new List<CharacterInfo>();
        }
    }
}
 
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 Lol
{
    public class CharacterInfo
    {
        public int id; //원래는 변하면 안되지만 Key로 사용한다.
        public int hp; //변할수 있는 값
 
        public CharacterInfo(int id, int hp)
        {
            this.id = id;
            this.hp = hp;
        }
    }
}
 
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 Lol
{
    public class CharacterData //변하면 안되는 기본 데이터
    {
        public int id;
        public int type;
        public string name;
        public int hp;
        public int damage;
        public float range;
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    //싱글톤 (인스턴스를 하나만 생성하여 어떠한 곳에서도 접근이 가능하게끔 만든다)
    public class DataManager
    {
        public static DataManager instance;
        public Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
 
        private DataManager()
        {
        }
 
        public static DataManager GetInstance()
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
            }
            return DataManager.instance; 
        }
 
        //파일읽는 메소드(역직렬화) : CharacterData 즉, 변하면 안되는 기본 데이터를 불러온다
        public void LoadDatas()
        {
 
            var json = File.ReadAllText("./data/character_data.json");
 
            var arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(json);
         
            foreach(var data in arrCharacterData)
            {
                dicCharacterData.Add(data.id, data);
            }
            Console.WriteLine("모든 데이터가 로드 되었습니다");
        }
    }
}
 
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 Lol
{
    public class Anivia : Character ,IMoveable
    {
        public Anivia() : base()
        {
        }
 
        public virtual void Move(Vector2 position)
        {
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    interface IMoveable //인터페이스
    {
        void Move(Vector2 position);
    }
}
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    public class GameLauncher
    {
        private List<Character> characters = new List<Character>();
        private UserInfo userInfo;
        private Baron baron;
        private Anivia anivia;
 
        public GameLauncher()
        {
            DataManager.GetInstance().LoadDatas(); //파일불러오기
            this.GameStart();
            this.Save();
 
        }
 
        public void GameStart()
        {
            Console.WriteLine("게임이 시작되었습니다");
            
            if (File.Exists("./info/user_info.json"))
            {
                //this.userInfo.newBie = false;
                Console.WriteLine("기존유저입니다");
                var json = File.ReadAllText("./info/user_info.json"); //저장되어있는 info 파일 읽기
                var info = JsonConvert.DeserializeObject<UserInfo>(json); //역직렬화
                this.CreateUserInfo(info);//userinfo클래스의 인스턴스 생성
 
            }
            else
            {
                Console.WriteLine("신규유저입니다");
                Directory.CreateDirectory("./info");
 
                this.CreateUserInfo();
            }
 
 
 
            Console.WriteLine("게임준비완료");
 
            //캐릭터 생성
 
            if (this.userInfo.newBieCheck == true// 신규유저일 경우
            { 
                this.userInfo.newBieCheck = false;
                this.baron = this.CreateCharacterInfo<Baron>(0);
                //this.anivia = this.CreateCharacterInfo<Anivia>(1);
                baron.TakeDamage(1);
                characters.Add(baron);
            }
            else//기존유저일경우
            {
                //this.baron = this.CreateCharacterInfo<Baron>(this.userInfo.gameInfo.characterInfos[0]);
                //this.anivia = this.CreateCharacterInfo<Anivia>(this.userInfo.gameInfo.characterInfos[1]);
                //인덱스 오류
            }
        }
 
        //UserInfo 생성
        public void CreateUserInfo(UserInfo info = null//선택적 매개변수 (아무 값이 들어오지 않으면 null로 전달)
        {
            if (userInfo == null)
            {
                if (info == null//아무값이 안들어오면 UserInfo클래스의 인스턴스를 생성후 userInfo에 넣어준다
                {
                    this.userInfo = new UserInfo();
                }
                else
                {
                    this.userInfo = info; //값이 있을경우 userInfo에 넣어준다
                }
            }
            else
            {
                Console.WriteLine("이미 UserInfo객체가 있습니다");
            }
        }
 
        //저장메서드
        public void Save()
        {
            var characterInfo = new List<CharacterInfo>(); //새로운 List인스턴스생성
            foreach (var character in this.characters)
            {
                characterInfo.Add(character.Info); //값을 복사한다.
            }
            this.userInfo.gameInfo.characterInfos = characterInfo; //List 인스턴스 characterInfos 에 복사한 값을 넣어준다.
 
            //직렬화
            var json = JsonConvert.SerializeObject(this.userInfo);
            File.WriteAllText("./info/user_info.json", json, Encoding.UTF8);
            Console.WriteLine("저장완료");
        }
 
        private T CreateCharacterInfo<T>(CharacterInfo info) where T : Character, new() //기존유저일경우
        {
            var data = DataManager.GetInstance().dicCharacterData[info.id];
 
            var character = new T();
            character.Info = info;
 
 
            return character;
        }
 
        //오버로드
        private T CreateCharacterInfo<T>(int id) where T : Character, new() //신규유저일경우
        {
            var data = DataManager.GetInstance().dicCharacterData[id];
            var info = new CharacterInfo(data.id, data.hp);
 
            var character = new T();
            character.Info = info;
 
            return character;
        }
    }
}
 
cs


'C# > Problems' 카테고리의 다른 글

Array 인벤토리 잘모르겠는것  (0) 2019.04.03

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    class Inventory
    {
        public Item[] items = new Item[10];
 
        public Inventory()
        {
        }
 
        //추가
        public void AddItem(Item item)
        {
            for(int idx =0; idx <items.Length; idx++)
            {
                if(items[idx] == null)
                {
                    items[idx] = item;
                    Console.WriteLine("{0}이 생성되었습니다", items[idx].name);
                    break;
                }
            }
        }
 
        //삭제
        public void RemoveItem(string name)
        {
            Item removeitem = null;
            for (int i=0; i<items.Length; i++)
            { 
                if(items[i].name == name)
                {
                    removeitem = items[i];
                    items[i] = null;
                    break;
                }
            }
            Console.WriteLine("{0}을 버렸습니다",removeitem.name);
        }
        /*
        //검색
        public Item FindItem(string name)
        {
        }
        */
        //추가
        public void UpdateItem(ref Item item, string name)
        {
 
        }
 
        //출력
        public void DisplayItems()
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    Console.WriteLine("--> {0}",items[i].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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    class App
    {
        public App()
        {
 
        }
 
        public void Start()
        {
            Inventory inventory = new Inventory();
 
            inventory.AddItem(new Item("두건"));
            //inventory.AddItem(null);
            inventory.AddItem(new Item("조종복"));
            inventory.AddItem(new Item("장갑"));
            inventory.AddItem(new Item("null"));
 
            inventory.RemoveItem("두건");
 
            inventory.DisplayItems();
 
            
            
            
        }
    }
}
 
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_09
{
    public class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
        }
    }
}
 
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_09
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
        }
    }
}
 
cs


break;문의 습관화

ref

아이템 개수 추가 삭제

'C# > Problems' 카테고리의 다른 글

롤 애니비아 바론 싸움 (캐릭터생성오류)  (0) 2019.04.11