/
Inventory.cs
171 lines (145 loc) · 4.14 KB
/
Inventory.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance = null;
public int hotbarSize = 7;
public int slotCount = 6;
public int slotSize = 20;
public HotbarUI hotbarUi;
public InventoryUI inventoryUi;
[SerializeField]
private List<ItemSlot> itemSlots;
private void Awake()
{
if(instance == null)
{
instance = this;
}
if(instance != this)
{
Destroy(this.gameObject);
}
InitItemSlots();
}
private void Start()
{
hotbarSize = hotbarUi.GetHotbarSize();
inventoryUi.InitInventoryUI(hotbarUi.GetSlotUi());
slotCount = inventoryUi.GetInventorySize();
InitItemSlots();
}
private void Update()
{
if (Input.GetButtonDown("Inventory"))
{
inventoryUi.Toggle();
}
}
public bool AddItem(Item itemToAdd)
{
List<ItemSlot> slotsWithItem = FindSlotsWithItem(itemToAdd);
for(int i = 0; i < slotsWithItem.Count; i++)
{
if(slotsWithItem[i].quantity < itemToAdd.stackSize)
{
slotsWithItem[i].quantity++;
Destroy(itemToAdd.gameObject);
UpdateUi();
return true;
}
}
int index = FindFirstEmptySlot();
if(index == -1)
{
return false;
}
itemSlots[index].item = itemToAdd;
itemSlots[index].quantity++;
UpdateUi();
return true;
}
public List<ItemSlot> FindSlotsWithItem(Item item)
{
//List<ItemSlot> foundSlots = itemSlots.FindAll(slots => slots.item != null && slots.item.name == item.name);
List<ItemSlot> foundSlots = itemSlots.FindAll(slots => slots.item != null && slots.item.name == item.name &&slots.item.Compare(item.GetComponent<IComparable>()));
return foundSlots;
}
public int FindFirstEmptySlot()
{
return itemSlots.FindIndex(slot => slot.item == null);
}
public int FindSlotsWithCapacity(List<ItemSlot> slots)
{
return itemSlots.FindIndex(slot => slot.quantity < slot.item.stackSize);
}
public void RemoveItem(int fromSlot)
{
RemoveItemFromSlot(itemSlots[fromSlot]);
}
public void RemoveItemFromSlot(ItemSlot slot)
{
slot.quantity--;
if (slot.quantity == 0)
{
DestroyObject(slot.item.gameObject);
slot.item = null;
UIManager.inventorySubmenuUi.Leave();
}
UpdateUi();
}
public void RemoveSelectedHotbarItem()
{
RemoveItem(hotbarUi.GetSelectedHotbar());
}
public Item GetHotbarItem()
{
Item item = itemSlots[hotbarUi.GetSelectedHotbar()].item;
return item;
}
public void RemoveItemForRangedAttack()
{
RemoveItem(hotbarUi.GetSelectedHotbar());
}
private void InitItemSlots()
{
itemSlots = new List<ItemSlot>(hotbarSize + slotCount);
for (int i = 0; i < itemSlots.Capacity; i++)
{
itemSlots.Add(new ItemSlot());
}
UpdateUi();
}
public void ConsumeHotbarItem()
{
Item item = itemSlots[hotbarUi.GetSelectedHotbar()].item;
if (item != null)
{
IConsumable consumable = item.GetComponent<IConsumable>();
if (consumable != null)
{
consumable = InstantiateItem(item).GetComponent<IConsumable>();
consumable.Consume(PlayerController.player);
RemoveItemFromSlot(itemSlots[hotbarUi.GetSelectedHotbar()]);
}
}
}
private void UpdateUi()
{
inventoryUi.UpdateUi(itemSlots);
}
public Item InstantiateItem(Item item)
{
return InstantiateItem(item, this.transform);
}
public static Item InstantiateItem(Item item, Transform parent)
{
if (item != null)
{
item = Instantiate<Item>(item, parent);
item.gameObject.name = item.name;
}
return item;
}
}