-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathInventory.cs
More file actions
279 lines (247 loc) · 7.61 KB
/
Inventory.cs
File metadata and controls
279 lines (247 loc) · 7.61 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
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
using System;
using UnityEngine;
using GameDevTV.Saving;
using RPG.Core;
using System.Collections.Generic;
namespace GameDevTV.Inventories
{
/// <summary>
/// Provides storage for the player inventory. A configurable number of
/// slots are available.
///
/// This component should be placed on the GameObject tagged "Player".
/// </summary>
public class Inventory : MonoBehaviour, ISaveable, IPredicateEvaluator
{
// CONFIG DATA
[Tooltip("Allowed size")]
[SerializeField] int inventorySize = 16;
// STATE
InventorySlot[] slots;
InventorySlot[] snapshot;
public struct InventorySlot
{
public InventoryItem item;
public int number;
}
// PUBLIC
/// <summary>
/// Broadcasts when the items in the slots are added/removed.
/// </summary>
public event Action inventoryUpdated;
/// <summary>
/// Convenience for getting the player's inventory.
/// </summary>
public static Inventory GetPlayerInventory()
{
var player = GameObject.FindWithTag("Player");
return player.GetComponent<Inventory>();
}
/// <summary>
/// Could this item fit anywhere in the inventory?
/// </summary>
public bool HasSpaceFor(InventoryItem item)
{
return FindSlot(item) >= 0;
}
public void TakeSnapshot()
{
snapshot = (InventorySlot[])slots.Clone();
}
public void RevertSnapshot()
{
slots = snapshot;
snapshot = null;
}
/// <summary>
/// How many slots are in the inventory?
/// </summary>
public int GetSize()
{
return slots.Length;
}
/// <summary>
/// Attempt to add the items to the first available slot.
/// </summary>
/// <param name="item">The item to add.</param>
/// <param name="number">The number to add.</param>
/// <returns>Whether or not the item could be added.</returns>
public bool AddToFirstEmptySlot(InventoryItem item, int number)
{
int i = FindSlot(item);
if (i < 0)
{
return false;
}
slots[i].item = item;
slots[i].number += number;
UpdateObservers();
return true;
}
/// <summary>
/// Is there an instance of the item in the inventory?
/// </summary>
public bool HasItem(InventoryItem item)
{
for (int i = 0; i < slots.Length; i++)
{
if (object.ReferenceEquals(slots[i].item, item))
{
return true;
}
}
return false;
}
/// <summary>
/// Return the item type in the given slot.
/// </summary>
public InventoryItem GetItemInSlot(int slot)
{
return slots[slot].item;
}
/// <summary>
/// Get the number of items in the given slot.
/// </summary>
public int GetNumberInSlot(int slot)
{
return slots[slot].number;
}
/// <summary>
/// Remove a number of items from the given slot. Will never remove more
/// that there are.
/// </summary>
public void RemoveFromSlot(int slot, int number)
{
slots[slot].number -= number;
if (slots[slot].number <= 0)
{
slots[slot].number = 0;
slots[slot].item = null;
}
UpdateObservers();
}
/// <summary>
/// Will add an item to the given slot if possible. If there is already
/// a stack of this type, it will add to the existing stack. Otherwise,
/// it will be added to the first empty slot.
/// </summary>
/// <param name="slot">The slot to attempt to add to.</param>
/// <param name="item">The item type to add.</param>
/// <param name="number">The number of items to add.</param>
/// <returns>True if the item was added anywhere in the inventory.</returns>
public bool AddItemToSlot(int slot, InventoryItem item, int number)
{
if (slots[slot].item != null)
{
return AddToFirstEmptySlot(item, number); ;
}
var i = FindStack(item);
if (i >= 0)
{
slot = i;
}
slots[slot].item = item;
slots[slot].number += number;
UpdateObservers();
return true;
}
// PRIVATE
private void Awake()
{
slots = new InventorySlot[inventorySize];
}
/// <summary>
/// Find a slot that can accomodate the given item.
/// </summary>
/// <returns>-1 if no slot is found.</returns>
private int FindSlot(InventoryItem item)
{
int i = FindStack(item);
if (i < 0)
{
i = FindEmptySlot();
}
return i;
}
/// <summary>
/// Find an empty slot.
/// </summary>
/// <returns>-1 if all slots are full.</returns>
private int FindEmptySlot()
{
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].item == null)
{
return i;
}
}
return -1;
}
/// <summary>
/// Find an existing stack of this item type.
/// </summary>
/// <returns>-1 if no stack exists or if the item is not stackable.</returns>
private int FindStack(InventoryItem item)
{
if (!item.IsStackable())
{
return -1;
}
for (int i = 0; i < slots.Length; i++)
{
if (object.ReferenceEquals(slots[i].item, item))
{
return i;
}
}
return -1;
}
private void UpdateObservers()
{
if (snapshot != null) return;
if (inventoryUpdated != null)
{
inventoryUpdated();
}
}
[System.Serializable]
private struct InventorySlotRecord
{
public string itemID;
public int number;
}
object ISaveable.CaptureState()
{
var slotStrings = new InventorySlotRecord[inventorySize];
for (int i = 0; i < inventorySize; i++)
{
if (slots[i].item != null)
{
slotStrings[i].itemID = slots[i].item.GetItemID();
slotStrings[i].number = slots[i].number;
}
}
return slotStrings;
}
void ISaveable.RestoreState(object state)
{
var slotStrings = (InventorySlotRecord[])state;
for (int i = 0; i < inventorySize; i++)
{
slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
slots[i].number = slotStrings[i].number;
}
UpdateObservers();
}
public bool? Evaluate(string predicate, string[] parameters)
{
switch (predicate)
{
case "HasInventoryItem":
return HasItem(InventoryItem.GetFromID(parameters[0]));
}
return null;
}
}
}