-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cs
More file actions
142 lines (129 loc) · 5.64 KB
/
Inventory.cs
File metadata and controls
142 lines (129 loc) · 5.64 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
using UnityEngine;
namespace uMMORPG
{
// not abstract because for example Monster only needs just this class
[DisallowMultipleComponent]
public class Inventory : ItemContainer
{
// helper function to count the free slots
public int SlotsFree()
{
// count manually. Linq is HEAVY(!) on GC and performance
int free = 0;
foreach (ItemSlot slot in slots)
if (slot.amount == 0)
++free;
return free;
}
// helper function to calculate the occupied slots
public int SlotsOccupied()
{
// count manually. Linq is HEAVY(!) on GC and performance
int occupied = 0;
foreach (ItemSlot slot in slots)
if (slot.amount > 0)
++occupied;
return occupied;
}
// helper function to calculate the total amount of an item type in inventory
// note: .Equals because name AND dynamic variables matter (petLevel etc.)
public int Count(Item item)
{
// count manually. Linq is HEAVY(!) on GC and performance
int amount = 0;
foreach (ItemSlot slot in slots)
if (slot.amount > 0 && slot.item.Equals(item))
amount += slot.amount;
return amount;
}
// helper function to remove 'n' items from the inventory
public bool Remove(Item item, int amount)
{
for (int i = 0; i < slots.Count; ++i)
{
ItemSlot slot = slots[i];
// note: .Equals because name AND dynamic variables matter (petLevel etc.)
if (slot.amount > 0 && slot.item.Equals(item))
{
// take as many as possible
amount -= slot.DecreaseAmount(amount);
slots[i] = slot;
// are we done?
if (amount == 0) return true;
}
}
// if we got here, then we didn't remove enough items
return false;
}
// helper function to check if the inventory has space for 'n' items of type
// -> the easiest solution would be to check for enough free item slots
// -> it's better to try to add it onto existing stacks of the same type
// first though
// -> it could easily take more than one slot too
// note: this checks for one item type once. we can't use this function to
// check if we can add 10 potions and then 10 potions again (e.g. when
// doing player to player trading), because it will be the same result
public bool CanAdd(Item item, int amount)
{
// go through each slot
for (int i = 0; i < slots.Count; ++i)
{
// empty? then subtract maxstack
if (slots[i].amount == 0)
amount -= item.maxStack;
// not empty. same type too? then subtract free amount (max-amount)
// note: .Equals because name AND dynamic variables matter (petLevel etc.)
else if (slots[i].item.Equals(item))
amount -= (slots[i].item.maxStack - slots[i].amount);
// were we able to fit the whole amount already?
if (amount <= 0) return true;
}
// if we got here than amount was never <= 0
return false;
}
// helper function to put 'n' items of a type into the inventory, while
// trying to put them onto existing item stacks first
// -> this is better than always adding items to the first free slot
// -> function will only add them if there is enough space for all of them
public bool Add(Item item, int amount)
{
// we only want to add them if there is enough space for all of them, so
// let's double check
if (CanAdd(item, amount))
{
// add to same item stacks first (if any)
// (otherwise we add to first empty even if there is an existing
// stack afterwards)
for (int i = 0; i < slots.Count; ++i)
{
// not empty and same type? then add free amount (max-amount)
// note: .Equals because name AND dynamic variables matter (petLevel etc.)
if (slots[i].amount > 0 && slots[i].item.Equals(item))
{
ItemSlot temp = slots[i];
amount -= temp.IncreaseAmount(amount);
slots[i] = temp;
}
// were we able to fit the whole amount already? then stop loop
if (amount <= 0) return true;
}
// add to empty slots (if any)
for (int i = 0; i < slots.Count; ++i)
{
// empty? then fill slot with as many as possible
if (slots[i].amount == 0)
{
int add = Mathf.Min(amount, item.maxStack);
slots[i] = new ItemSlot(item, add);
amount -= add;
}
// were we able to fit the whole amount already? then stop loop
if (amount <= 0) return true;
}
// we should have been able to add all of them
if (amount != 0) Debug.LogError("inventory add failed: " + item.name + " " + amount);
}
return false;
}
}
}