-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInventory.cpp
More file actions
94 lines (84 loc) · 2.94 KB
/
Inventory.cpp
File metadata and controls
94 lines (84 loc) · 2.94 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
#include "Inventory.h"
// The constructor for the Intventory class
Inventory::Inventory(Game* InGame)
{
// Set the parent reference
TextAdv = InGame;
// In here we populate the list of items in our game
InventoryList.push_back(Item("key", 3, false, false, "This is room 4"));
}
// This function collects the item from the room if possible
bool Inventory::CollectItem(string ItemName, int RoomIndex)
{
// Loop through the inventory array and see if the item...
// QUESTION INV1: What is the for loop doing when we use 'Item& InvItem : InventoryList'?
for (Item& InvItem : InventoryList)
{
// ...exists, is in the room, and hasn't already been collected
if (InvItem.Name == ItemName && InvItem.Location == RoomIndex && !InvItem.bCollected)
{
// Collect the item and return a success if it is
InvItem.bCollected = true;
// Get the row and column of the required room from the size of the arrays
// QUESTION INV2: Why is the size() function being divided, should it not return the size of the array?
size_t Column = TextAdv->CurrentRoom % size(TextAdv->GameMap->GameMap[0]);
size_t Row = (TextAdv->CurrentRoom - Column) / size(TextAdv->GameMap->GameMap);
TextAdv->GameMap->GameMap[Row][Column].Description = InvItem.LatterDescription;
return true;
}
}
// Otherwise return that the take command was invalid
return false;
}
// This function consumes an item if possible
void Inventory::ConsumeItem(string ItemName)
{
// Loop through the inventory array and see if the item...
for (Item& InvItem : InventoryList)
{
// ...exists, and has been collected
if (InvItem.Name == ItemName && InvItem.bCollected)
{
// Consume the item and return
InvItem.bConsumed = true;
return;
}
}
}
// This function checks if the player has an item
bool Inventory::HasItem(string ItemName)
{
// Loop through the inventory array and see if the item...
for (Item& InvItem : InventoryList)
{
// ...exists, and hasn't been consumed
if (InvItem.Name == ItemName && InvItem.bCollected && !InvItem.bConsumed)
{
// Return the acknowledgment that the player has the item
return true;
}
}
// Otherwise inform the player that they don't have the item
return false;
}
// This function prints out all the items the player has collected
string Inventory::PrintInventory()
{
bool InventoryEmpty = true;
// QUESTION INV3: We've made a string here even though we won't use it if the inventory is empty. Why is it here, and what's the trade off/alternative?
string InventoryString = "Your inventory contains:\n";
// Loop through the inventory array and see if the item...
for (Item& InvItem : InventoryList)
{
// ... has been collected
if (InvItem.bCollected)
{
// Appends the name of the item
InventoryString += InvItem.Name + "\n";
InventoryEmpty = false;
}
}
// Return the final output, but announce it's empty if no items were found
if (InventoryEmpty) return "It looks like your inventory is empty\n";
return InventoryString;
}