인벤토리 만들기 (제너릭)

Unity3D/C# 2019. 4. 2. 19:07
반응형
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_07
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            app.Start();
            Console.ReadKey();
        }
    }
}
 
 
 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_07
{
    public class App
    {
        //생성자 
        public App()
        {
 
        }
 
        //실행 메서드 
        public void Start()
        {
            var inventory = new Inventory<Equipment>();
 
            //var item = new Equipment("장검", 1);
            //var item2 = new Equipment("장검", 100);
 
            bool isAdded = false;
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.AddItem(new Equipment("장검"100), out isAdded);
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.AddItem(new Equipment("장검"1), out isAdded);
            inventory.DisplayItems();
 
            Console.WriteLine();
            inventory.DisplayItems();
 
            var item3 = inventory.GetItem("장검"11);
 
            Console.WriteLine("{0} x{1}개를 꺼냈습니다."item3.name, item3.stack);
 
            Console.WriteLine();
            inventory.DisplayItems();
        }
    }
}
 
 
 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_07
{
    //모든 아이템들의 기본 클래스 
    public class Item
    {
        //맴버변수 이름 선언 
        public string name;
        public int stack;
 
        public Item()
        {
 
        }
 
        public Item(string name, int stack) {
            this.name = name;
            this.stack = stack;
        }
    }
}
 
 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_07
{
    //장비 클래스 
    public class Equipment : Item
    {
        public Equipment() : base()
        {
 
        }
 
        public Equipment(string name, int stack) : base(name, stack)
        {
            Console.WriteLine("{0}이 {1}개 생성되었습니다.", name, stack);
        }
    }
}
 
 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_07
{
    //약초 클래스 
    public class Herb : Item
    {
        public Herb() : base()
        {
 
        }
        public Herb(string name, int stack) : base(name, stack)
        {
            Console.WriteLine("{0}이 {1}개 생성되었습니다.", name, stack);
        }
    }
}
 
 
 

 

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_07
{
    public class Inventory<T> where T : Item, new()
    {
        private List<T> items;
 
        public Inventory()
        {
            this.items = new List<T>();
        }
 
        public void AddItem(T item, out bool result)
        {
            if (item == null)
            {
                result = false;
                return;
            }
            else
            {
                Item foundItem = null;
                foreach (var element in this.items)
                {
                    if (element.name.Equals(item.name))
                    {
                        foundItem = element;
                    }
                }
 
                if (foundItem != null)
                {
                    foundItem.stack += item.stack;
                    result = true;
                }
                else
                {
                    result = true;
                    this.items.Add(item);
                }
            }
        }
 
        public T GetItem(string name, int stack = 1
        {
            Item foundItem = null;
            Item rtnItem = null;
 
            foreach (var item in this.items)
            {
                if (item.name == name)
                {
                    foundItem = item;
                }
            }
 
            if (foundItem != null)
            {
                var getStack = 0;
                var remainStack = foundItem.stack;
 
                if (stack >= foundItem.stack)
                {
                    getStack = foundItem.stack;
                    remainStack = 0;
                }
                else {
                    getStack = stack;
                    remainStack = foundItem.stack - getStack;
                    foundItem.stack -= getStack;
                }
 
                rtnItem = new T();
                rtnItem.name = foundItem.name;
                rtnItem.stack = getStack;
 
                if (remainStack <= 0)
                {
                    this.RemoveItem(foundItem.name);
                }
            }
 
            return rtnItem as T;
        }
 
        public bool RemoveItem(string name)
        {
            foreach (var element in items)
            {
                if (element.name.Equals(name))
                {
                    this.items.Remove(element);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
 
        public T FindItem(string name)
        {
            foreach (var element in this.items)
            {
                if (element.name == name)
                {
                    return element as T;
                }
            }
            return default (T);
        }
 
        public void UpdateItem(ref T item, string name, int stack)
        {
            if (item == null) {
                Console.WriteLine("업데이트 실패");
                return;
            }
            
            item.name = name;
            item.stack = stack;
        }
 
        public void DisplayItems()
        {
            if (items.Count <= 0)
            {
                Console.WriteLine("인벤토리에 아이템이 없습니다.");
            }
            else
            {
                foreach (var item in this.items)
                {
                    Console.WriteLine("-> {0} x{1}"item.name, item.stack);
                }
            }
        }
    }
}
 
 
 
반응형

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

(C#) 타입 비교  (0) 2019.04.05
(C#) Array to Dictionary (Linq)  (0) 2019.04.05
referencesource microsoft  (0) 2019.04.02
IEnumerator, IEnumerable 상속받은 Inventory 구현 (인덱서)  (0) 2019.04.01
C#. 쓰레드(Thread)  (0) 2019.04.01
: