-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryUiManager.cs
286 lines (249 loc) · 9.1 KB
/
InventoryUiManager.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryUiManager : MonoBehaviour
{
[SerializeField] GameObject inventoryContainer;
[SerializeField] GameObject inventoryButtonPrefab;
[Header ("Item Info Panel Properties")]
[SerializeField] GameObject uiItemInfo;
[SerializeField] Button equipButton;
[SerializeField] Button unequipButton;
[SerializeField] Button upgradeButton;
[SerializeField] float showHideTime;
[SerializeField] LeanTweenType easeType;
[Header("Player Info Panel Properties")]
[SerializeField] GameObject uiPlayerInfo;
[SerializeField] Text damageModText;
[SerializeField] Text hpModText;
[SerializeField] Text speedModText;
[SerializeField] Text armorModText;
ItemProgression itemProg;
Item currentItem;
public GameObject currentButton { set; get; }
InvetoryEquipedItemButton[] equipedItemButtons = new InvetoryEquipedItemButton[8];
GameObject player;
Inventory inventory;
SavingWrapper saveWrapper;
Wallet wallet;
private void Start()
{
itemProg = Resources.Load<ItemProgression>("ItemTable");
player = GameObject.FindGameObjectWithTag("Player");
saveWrapper = FindObjectOfType<SavingWrapper>();
inventory = player.GetComponentInChildren<Inventory>();
equipedItemButtons = FindObjectsOfType<InvetoryEquipedItemButton>();
wallet = FindObjectOfType<Wallet>();
saveWrapper.Load(true); //Mallon prepei na fygei apo edw
HideItemInfo();
HidePlayerInfo();
FillContainer();
FillEquipedButtons();
}
Sprite LoadIcon(string name)
{
return Resources.Load<Sprite>(name);
}
/// <summary>
/// Gemizei to unequiped container sto inventory me buttons me ta unequiped items
/// </summary>
void FillContainer()
{
inventory.unequipedItems.Sort(); //Taxinomish
for (int i = 0; i < inventory.unequipedItems.Count; i++)
{
CreateButton(inventory.unequipedItems[i]);
}
}
/// <summary>
/// Dhmioyrgei button me unequiped item tou inventory
/// </summary>
/// <param name="item"></param>
void CreateButton(Item item)
{
GameObject button = Instantiate(inventoryButtonPrefab, inventoryContainer.transform) as GameObject;
InvetoryItemButton buttonInfo = button.GetComponent<InvetoryItemButton>();
buttonInfo.Item = item;
string name = buttonInfo.Item.itemName.ToString() + " Lvl:" + buttonInfo.Item.level.ToString();
button.GetComponent<Image>().sprite = LoadIcon(name);
button.GetComponentInChildren<Text>().text = name;
}
/// <summary>
/// Fortwnei ta slots twn Equiped Items
/// </summary>
void FillEquipedButtons()
{
foreach (var button in equipedItemButtons)
{
for (int i = 0; i < inventory.equipedItems.Count; i++)
{
if (inventory.equipedItems[i].itemType == button.itemType)
{
button.Item = inventory.equipedItems[i];//test
button.GetComponent<Image>().sprite = LoadIcon(button.Item.itemName.ToString());
button.GetComponent<Image>().color = Color.red;
button.isFull = true;
}
}
}
}
/// <summary>
/// Emfanizei to ItemInfo panel me to info gia to antikeimeno poy epilexthike
/// </summary>
/// <param name="item"></param>
public void ShowItemInfo(Item item, bool itemIsEquiped)
{
currentItem = item;
if (!uiItemInfo.activeInHierarchy)
{
LeanTween.scale(uiItemInfo.GetComponent<RectTransform>(), Vector2.one, showHideTime).setEase(easeType);
uiItemInfo.SetActive(true);
}
UpdateItemTexts();
if (itemIsEquiped)
{
equipButton.gameObject.SetActive(false);
unequipButton.gameObject.SetActive(true);
}
else
{
equipButton.gameObject.SetActive(true);
unequipButton.gameObject.SetActive(false);
}
CheckUpgradeButton();
}
/// <summary>
/// Enables or Not the upgrade Button depending on item cost and available money in wallet
/// </summary>
void CheckUpgradeButton()
{
int maxLvl = itemProg.GetMaxLevel(currentItem.itemName,currentItem.rarity);
int priceToUpgrade = itemProg.GetItemPriceCost(currentItem.itemName, currentItem.rarity, currentItem.level);
bool active = priceToUpgrade <= wallet.GetMoney();
//
//TODO Implement material cost
//
if(currentItem.level < maxLvl - 1)
{
upgradeButton.GetComponentInChildren<Text>().text = "Upgrade Cost: " + priceToUpgrade.ToString();
upgradeButton.interactable = active;
}
else
{
upgradeButton.GetComponentInChildren<Text>().text = "!!Maxed!!";
upgradeButton.interactable = false;
}
}
public void UpgradeItem()
{
currentItem.level++;
UpdateItemTexts();
CheckUpgradeButton();
}
/// <summary>
/// Kanei update ta texts tou ItemInfo panel
/// </summary>
void UpdateItemTexts()
{
Text[] texts = uiItemInfo.GetComponentsInChildren<Text>();
texts[0].text = currentItem.itemName.ToString() + " Lvl:" + currentItem.level.ToString();
texts[1].text = currentItem.description;
for (int i = 2; i < 6; i++)
{
texts[i].text = "";
}
Modifier[] mods = itemProg.GetModifiers(currentItem.itemName, currentItem.rarity, currentItem.level);
for (int i = 2; i < mods.Length + 2; i++)
{
texts[i].text = mods[i - 2].modifierType.ToString() + ": " + mods[i - 2].Value.ToString();
}
}
/// <summary>
/// Apokripsh tou ItemInfo panel
/// </summary>
public void HideItemInfo()
{
LeanTween.scale(uiItemInfo.GetComponent<RectTransform>(), Vector2.zero, showHideTime).setEase(easeType);
uiItemInfo.SetActive(false);
}
/// <summary>
/// Emfanizei to PlayerInfo panel me ta stat twn modifiers tou
/// </summary>
/// <param name="item"></param>
public void ShowPlayerInfo()
{
if (!uiPlayerInfo.activeInHierarchy)
{
LeanTween.scale(uiPlayerInfo.GetComponent<RectTransform>(), Vector2.one, showHideTime).setEase(easeType);
uiPlayerInfo.SetActive(true);
}
//Updates Texts
BaseStats plStats = player.GetComponentInChildren<BaseStats>();
damageModText.text = "Damage: " + plStats.GetStat(Stat.Damage).ToString();
hpModText.text = "Health : " + plStats.GetStat(Stat.Health).ToString();
}
/// <summary>
/// Apokripsh tou PlayerInfo panel
/// </summary>
public void HidePlayerInfo()
{
LeanTween.scale(uiPlayerInfo.GetComponent<RectTransform>(), Vector2.zero, showHideTime).setEase(easeType);
uiPlayerInfo.SetActive(false);
}
/// <summary>
/// Kanei equip to epilegmeno item
/// </summary>
public void EquipItem()
{
InvetoryEquipedItemButton eqPlaceholder = EquipItemPlaceholder(currentItem.itemType);
if (eqPlaceholder.isFull)
{
Item equiped = eqPlaceholder.Item;
inventory.UnequipItem(equiped);
CreateButton(equiped);
eqPlaceholder.ClearItem();
}
eqPlaceholder.Item = currentItem;
eqPlaceholder.GetComponent<Image>().sprite = LoadIcon(currentItem.itemName.ToString());
eqPlaceholder.GetComponent<Image>().color = Color.red;
eqPlaceholder.isFull = true;
inventory.EquipItem(currentItem);
HideItemInfo();
Destroy(currentButton);
}
/// <summary>
/// Returns the proper equiped button for the given ItemType
/// </summary>
InvetoryEquipedItemButton EquipItemPlaceholder(ItemType type)
{
for (int i = 0; i < equipedItemButtons.Length; i++)
{
if (equipedItemButtons[i].itemType == type)
{
return equipedItemButtons[i];
}
}
Debug.Log("No equiped button placeholder found for ItemType: " + type.ToString());
return null;
}
/// <summary>
/// Kanei unequip to item. Ui UnequipButton calls it
/// </summary>
public void UnequipItem()
{
currentButton.GetComponent<InvetoryEquipedItemButton>().ClearItem();
inventory.UnequipItem(currentItem);
CreateButton(currentItem);
HideItemInfo();
}
/// <summary>
/// Deletes an item in inventory... TODO(na metatrapei se anakyklvsh)
/// </summary>
public void DeleteItem()
{
inventory.DeleteItem(currentItem);
Destroy(currentButton);
HideItemInfo();
}
}