-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemContainer.cs
More file actions
51 lines (47 loc) · 1.59 KB
/
ItemContainer.cs
File metadata and controls
51 lines (47 loc) · 1.59 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
// Inventory & Equip both use slots and some common functions. might as well
// abstract them to save code.
using Mirror;
namespace uMMORPG
{
public abstract class ItemContainer : NetworkBehaviour
{
// the slots
public readonly SyncList<ItemSlot> slots = new SyncList<ItemSlot>();
// helper function to find an item in the slots
public int GetItemIndexByName(string itemName)
{
// (avoid FindIndex to minimize allocations)
for (int i = 0; i < slots.Count; ++i)
{
ItemSlot slot = slots[i];
if (slot.amount > 0 && slot.item.name == itemName)
return i;
}
return -1;
}
// durability //////////////////////////////////////////////////////////////
// calculate total missing durability
public int GetTotalMissingDurability()
{
int total = 0;
foreach (ItemSlot slot in slots)
if (slot.amount > 0 && slot.item.data.maxDurability > 0)
total += slot.item.data.maxDurability - slot.item.durability;
return total;
}
// repair all items. can be used by Npc.
[Server]
public void RepairAllItems()
{
for (int i = 0; i < slots.Count; ++i)
{
if (slots[i].amount > 0)
{
ItemSlot slot = slots[i];
slot.item.durability = slot.item.maxDurability;
slots[i] = slot;
}
}
}
}
}