-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cs
More file actions
160 lines (139 loc) · 4.44 KB
/
Inventory.cs
File metadata and controls
160 lines (139 loc) · 4.44 KB
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
using UnityEngine;
using System.Collections.Generic;
// volume based inventory system
[System.Serializable]
public class Inventory
{
public float maxInventoryVolume = 50f;
public List<InventoryItem> inventory = new List<InventoryItem>();
public bool HaveEnoughList(List<InventoryItem> toCheck)
{
bool value = false;
foreach (var check in toCheck)
{
value = HaveEnough(check);
if(!value)
{
break;
}
}
return value;
}
public bool HaveEnough(InventoryItem check)
{
var itemFound = inventory.Find(invItem => invItem.item.data.ItemId == check.item.Id);
if(itemFound != null)
{
if(itemFound.quantity >= check.quantity)
{
return true;
}
}
return false;
}
public void RemoveList(List<InventoryItem> toRemove)
{
foreach (var remove in toRemove)
{
RemoveItem(remove);
}
}
public void RemoveItem(InventoryItem toRemove)
{
var existingItem = inventory.Find(invItem => invItem.item.data == toRemove.item.data);
if (existingItem != null)
{
if(existingItem.quantity <= toRemove.quantity)
{
inventory.Remove(existingItem);
}
else
{
existingItem.quantity -= toRemove.quantity;
}
}
}
public void AddList(List<InventoryItem> toAdd)
{
foreach (var add in toAdd)
{
AddItem(add);
}
}
public int AddItem(InventoryItem toAdd)
{
float requiredVolume = toAdd.item.data.volume * toAdd.quantity;
if (CanFitItemVolume(requiredVolume))
{
// Check if the item already exists in the inventory
InventoryItem existingItem = inventory.Find(invItem => invItem.item.data == toAdd.item.data);
if (existingItem != null)
{
// Calculate the available space in the inventory
float availableSpace = maxInventoryVolume - GetCurrentInventoryVolume();
if (availableSpace >= requiredVolume)
{
// There is enough space in the inventory
existingItem.quantity += toAdd.quantity;
return 0; // No leftover items
}
else
{
// Add as much as possible to the existing stack, and calculate leftover items
int addedQuantity = Mathf.FloorToInt(availableSpace / toAdd.item.data.volume);
existingItem.quantity += addedQuantity;
return toAdd.quantity - addedQuantity; // Return the leftover quantity
}
}
else
{
// Check if the new item can fit in the remaining inventory space
if (requiredVolume <= maxInventoryVolume - GetCurrentInventoryVolume())
{
// Create a new stack in the inventory
inventory.Add(new InventoryItem(toAdd.item.Id, toAdd.quantity));
return 0; // No leftover items
}
else
{
return toAdd.quantity; // Return the full quantity as leftover items
}
}
}
else
{
return toAdd.quantity; // Return the full quantity as leftover items
}
}
public List<InventoryItem> GetItems()
{
return inventory;
}
private bool CanFitItemVolume(float requiredVolume)
{
// Check if adding the new items exceeds the inventory volume limit
return GetCurrentInventoryVolume() + requiredVolume <= maxInventoryVolume;
}
public bool CanFitItemList(List<InventoryItem> toAdd)
{
float required = 0;
foreach (var item in toAdd)
{
required += item.quantity * item.item.data.volume;
}
return CanFitItemVolume(required);
}
public bool IsFull()
{
return GetCurrentInventoryVolume() == maxInventoryVolume;
}
private float GetCurrentInventoryVolume()
{
float totalVolume = 0f;
foreach (var invItem in inventory)
{
totalVolume += invItem.item.data.volume * invItem.quantity;
}
return totalVolume;
}
}