티스토리 뷰

Unity 3D/팀 프로젝트

inventory.cs

猫猫 2015. 3. 24. 10:27
반응형
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
 
public class Inventory : MonoBehaviour {
    public GameObject inven;
    public GameObject quest;
    public GameObject townPortal;
    public int slotsX, slotsY;
    public GUISkin skin;
    public List<Item> inventory = new List<Item>();
    public List<Item> slots = new List<Item>();
    public bool showInventory;
    private Button q_button;
    private Button i_button;
    private Button t_button;
    private ItemDatabase database;
    private bool showTooltip;
    public bool showQuest;
    private string tooltip;
    private bool draggingItem; //drag인지 아닌지?
    private Item draggedItem; //drag 일어난 아이템의 모든것을 가지고 있다.
    private int prevIndex;
    public bool showTown;
 
 
    void Start()
    {
        for(int i = 0; i < (slotsX * slotsY); i++)
        {
            slots.Add (new Item());
            inventory.Add (new Item());
        }
        database = GameObject.FindGameObjectWithTag("itemdatabase").GetComponent<ItemDatabase>();
        q_button = GameObject.FindGameObjectWithTag("button").GetComponent<Button>();
        i_button = GameObject.FindGameObjectWithTag("inven").GetComponent<Button>();
        t_button = GameObject.FindGameObjectWithTag("town").GetComponent<Button>();
 
        AddItem(1); // ID로 add한다.
        AddItem(0); 
        AddItem(1);
 
        //버튼 클릭시
        q_button.onClick.AddListener(() => { showQuest = !showQuest; });
        i_button.onClick.AddListener(() => { showInventory = !showInventory; });
        t_button.onClick.AddListener(() => {showTown = !showTown; });
 
    //    RemoveItem(1);
    }
 
    void Update()
    {
        Check();
    }
 
 
    
    void Check() //키 다운값들 모음
    {
        if(Input.GetKeyDown(KeyCode.I) ||Input.GetKeyDown(KeyCode.E) )    showInventory  = !showInventory;
        if(Input.GetKeyDown(KeyCode.J)) showQuest = !showQuest;
        if(Input.GetKeyDown(KeyCode.T)) showTown = !showTown;
 
 
        if(!showTown)     townPortal.SetActive(false);
        else townPortal.SetActive(true);
        if(showTown)
        {
            GameObject.FindGameObjectWithTag("tg").GetComponent<Image>().fillAmount += Time.deltaTime;//(*speed);
            if(GameObject.FindGameObjectWithTag("tg").GetComponent<Image>().fillAmount ==1)
            {
                showTown = !showTown;
            }
        }
 
 
        if(!showInventory)     inven.SetActive(false);
        else inven.SetActive(true);
        
        if(!showQuest) quest.SetActive(false);
        else quest.SetActive(true);
    }
 
    void OnGUI()
    {
        tooltip = ""//tooltip은 일단 null값
        GUI.skin = skin; //public으로 넣어주었던skin을 넣어줍니다.
        if(showInventory)
        {
            DrawInventory();
            if(showTooltip)
                //showTooltip이 트루라면 skin의 style중 tooltipskin이라는 이름의 스타일을 마우스 이벤트 위치의 사각형에 그려줍니다.
                GUI.Box( new Rect(Event.current.mousePosition.x+15f    , Event.current.mousePosition.y, 200200), tooltip, skin.GetStyle("tooltipskin"));
        }
        //만약 드래깅 아이템이 일어난다면 drawtexture로 새로운 사각형을 만들어서 draggedItem의 아이콘을 마우스 포지션에 뿌려줍니다.
        if(draggingItem)
        {
            GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 5050), draggedItem.itemIcon);
        }
    }
 
    void DrawInventory()
    {
 
        Event e = Event.current; //단축용
        int i = 0//index 
        for(int y = 0; y < slotsY; y++//y,x를 바꾸면 가로로 정렬되기 때문에.
        {
            for(int x = 0; x < slotsX; x++)
            {
                //inventory size
 
                //Rect slotRect = new Rect ( 782+x * 30, 335+y * 30, 30, 30); //public slotsX,Y값만큼 rect를 그리는데 56씩 그린다.
                Rect slotRect = new Rect ( 605+*24245+* 222424);
 
                GUI.Box(slotRect, "", skin.GetStyle("Inven_grid")); //위치는 slotRect위치, skin은 Inven_Grid라는 스킨
 
                slots[i] = inventory[i];
 
                if(slots[i].itemName != null)
                {
                    //slots[i]에 아이템 이름이 들어온다면 널이 아니기 때문에 slots이 갖고 있는 아이템 아이콘을 해당 rect에 뿌려줍니다.
                    GUI.DrawTexture(slotRect, slots[i].itemIcon);
 
                    if(slotRect.Contains(e.mousePosition)) //contains는 xywh 4개 좌표를 불러오고 event.current.mousepositon으로 마우스 좌표를 가져와 비교한다. 
                    {
                        CreateTooltip(slots[i]);
                        showTooltip = true;
                        if(e.button == && e.type == EventType.mouseDrag && !draggingItem) //0번버튼이 눌렸거나, 마우스 이벤트 중 드래그 이벤트가 일어났을때
                        {
                            draggingItem = true;
                            prevIndex = i; //swap기능을 위해서
                            draggedItem = slots[i];
                            inventory[i] = new Item(); //해당 아이템을 들어올렸으므로 덮어서 없애준다.
                        }
                        //mouseup과 dragginItem이 동시에 일어났을때 즉, 해당 슬롯에서 드래그하여 놓았을 때,
                        // 버블정렬같이 인벤토리의 prev에는 인벤의 i번째, 인벤의 i번째에는 draggeditem을 넣어준다.
                        //dragging은 종료, draggeditem은 null로 만들어 해당 swap을 종료한다.
                        if(e.type == EventType.mouseUp && draggingItem) 
                        {
                            inventory[prevIndex] = inventory[i];
                            inventory[i] = draggedItem;
                            draggingItem = false;
                            draggedItem = null;
                        }
 
                        if(!draggingItem)
                        {
                            tooltip = CreateTooltip(slots[i]);
                            showTooltip = true;
                        }
 
                        //마우스 오른쪽 클릭시 이벤트
                        if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
                        {
                            if(slots[i].itemType == Item.ItemType.Consumable)
                            {
                                UseConsumable(slots[i], i, true);
                            }
                        }
                    }
                    if(tooltip =="") showTooltip = false;
 
                } 
 
                else 
                {
                    if(slotRect.Contains(e.mousePosition))
                    {
                        if(e.type == EventType.mouseUp && draggingItem) 
                        {
                            inventory[i] = draggedItem;
                            draggingItem = false;
                            draggedItem = null;
                        }
                    }
                }
                if(tooltip == "" | showInventory == false//tooltip에 내용이 없다면 보이지 않게 한다.
                {
                    showTooltip = false;
                }
 
                i++
            }                            
        }
    }
 
    string CreateTooltip(Item item) 
    {
        tooltip = "<color=#7d59ff>"+item.itemName+"</color>\n\n" + item.itemDesc; //강제 색상변환, \n을 따옴표 안에 넣으면 줄 변환이 가능하다.
        return tooltip;
    }
 
 
    void RemoveItem(int id)
    {
        for(int i = 0; i < inventory.Count; i++)
        {
            if(inventory[i].itemId ==id)
            {
                inventory[i] = new Item(); //덮어씌워서 없앤다.
                break;
            }
        }
    }
    void AddItem(int id)
    {
        for(int i = 0; i < inventory.Count; i++)
        {
            if(inventory[i].itemName == null)
            {
                for(int j = 0; j < database.items.Count; j++)
                {
                    if(database.items[j].itemId == id)
                    {
                        inventory[i] = database.items[j];
                    }
                }
                break;
            }
        }
    }
 
    bool InventoryContains(int id) //인벤에 아이템이 존재하지 않으면 트루를 돌려준다.
    {    
        bool result = false;
        for(int i = 0; i<inventory.Count; i++)
        {
            result = inventory[i].itemId == id; //다르면 false, 같으면 true
            if(result)
            {
                break;
            }
        }
        return result;
    }
 
    private void UseConsumable(Item item, int slot, bool delItem)
    {
        switch(item.itemId)
        {
        case 1:
            {
            //Stat Id, Buff amount, Buff duration
            //PlayerStats.IncreatStat(3, 15, 30f);
            print("use consumable " + item.itemName);
            break;
            }
        }
 
        if(delItem)
        {
            inventory[slot] = new Item();
        }
    }
 
}
 
cs


반응형

'Unity 3D > 팀 프로젝트' 카테고리의 다른 글

이펙트 관리  (0) 2015.03.24
item.cs  (0) 2015.03.24
itemdatabase.cs  (0) 2015.03.24