I'm in the midst of building a text-based game with hope I can implement an inventory system among other things to make the world interactive.
I have already created an item class:
enum ItemRange {
ShortRange,
MidRange,
LongRange,
};
enum ItemType {
Weapon,
Shield,
Consumable,
Necklance,
Ring,
Robe,
Shirt,
Pants,
Helmet,
Chestplate,
Pauldron,
Gauntlet,
Greaves,
Boots,
};
class Item
{
private:
std::string Name;
std::string material;
int dataBaseID;
std::string itemDesc;
int Attack;
double CritMulti;
int Defence;
int HealVal;
int restoreMana;
int restoreStamina;
int GoldValue;
int maxItemCharges;
int enchantVal;
ItemType type;
ItemRange range;
int stack;
int maxStack;
bool stackable;
public:
std::string displayName;
bool equipped;
Item();
~Item();
Item(std::string Name, std::string material,std::string ItemDesc, int DatabaseID, ItemType Type, int Attack, double CritMulti, int Defence, int HealVal, int restoreMana, int restoreStamina, int GoldValue, int Stack, int MaxStack, bool Stackable, int enchantVal, ItemRange range);
friend void isEquipped(std::string funcChoice, std::unordered_map<std::string, Item> funcMap);
std::string GetItem() {return Name;}
std::string GetItemDesc() {return itemDesc;}
std::string GetDispName() {return displayName;}
int GetDatabaseID() {return dataBaseID;}
ItemType GetItemType() {return type;}
ItemRange GetItemRange() {return range;}
int GetAttack() {return Attack;}
int GetCritMulti() {return CritMulti;}
int GetDefence() {return Defence;}
int GetHealVal() {return HealVal;}
int GetRestoreManaVal() {return restoreMana;}
int GetRestoreStaminaVal() {return restoreStamina;}
int GetGoldVal() {return GoldValue;}
int GetStack() {return stack;}
int GetMaxStack() {return maxStack;}
bool GetStackable() {return stackable;}
bool GetEquipped() {return equipped;}
};
Item::Item() {
Name = " ";
material = " ";
dataBaseID = 0;
itemDesc = " ";
maxItemCharges = 0;
type = Weapon;
Attack = 0;
CritMulti = 0.0;
HealVal = 0;
Defence = 0;
restoreMana = 0;
restoreStamina = 0;
GoldValue = 0;
enchantVal = 0;
stack = 0;
maxStack = 0;
stackable = false;
};
Item::~Item() {
};
Item::Item(std::string Name, std::string material,std::string ItemDesc, int DatabaseID, ItemType Type, int Attack, double CritMulti, int Defence, int HealVal, int restoreMana, int restoreStamina, int GoldValue, int Stack, int MaxStack, bool Stackable, int enchantVal, ItemRange range) {
this->Name = Name;
this->material = material;
this->displayName = material + " " + Name;
this->dataBaseID = DatabaseID;
this->itemDesc = ItemDesc;
this->maxItemCharges = enchantVal * 2 / 4;
this->type = Type;
this->enchantVal = enchantVal;
this->stack = Stack;
this->maxStack = MaxStack;
this->stackable = Stackable;
}
While I'm happy with the Item class I'm not sure the best approach to interacting with them while in the inventory.
My recent attempt at doing so:
std::string equippedStringChoice;
bool funcLoop_1 = true;
int funcChoice_1 = 0;
Item itemToCheck;
void isEquipped(std::string funcChoice, std::unordered_map<std::string, Item> funcMap) {
if (funcMap.find(funcChoice) == funcMap.end()) {
std::cout << "This item is not in your inventory!\n";
} else {
itemToCheck = funcMap.at(funcChoice);
switch(itemToCheck.type) {
case 0:
while(funcLoop_1) {
funcLoop_1 = false;
std::cout << "Which hand do you want to equip it too?\n1: Main-Hand?\n2: Off-Hand?\n";
std::cin >> funcChoice_1;
switch(funcChoice_1) {
case 1:
if (player.weaponEquipped != "empty") {
std::cout << "You already have" << player.weaponEquipped << "equipped in your main hand!\n";
} else {
std::cout << "You equipped " << itemToCheck.GetDispName() << "\n";
player.weaponEquipped = itemToCheck.GetDispName();
player.damageMin += itemToCheck.Attack;
player.damageMax += itemToCheck.Attack;
}
break;
case 2:
if (player.offhandWeaponEquipped != "empty") {
std::cout << "You already have " << player.offhandWeaponEquipped << " equipped in your main hand!\n";
} else {
player.offhandWeaponEquipped = itemToCheck.GetDispName();
player.damageMax += itemToCheck.Attack;
player.damageMin += itemToCheck.Attack;
}
break;
}
}
break;
case 1:
while(funcLoop_1) {
funcLoop_1 = false;
std::cout << "Do you want to equip " << itemToCheck.GetDispName() << "\n1: Yes\n2: No\n> ";
std::cin >> funcChoice_1;
switch(funcChoice_1) {
case 1:
if (player.offhandWeaponEquipped != "empty") {
std::cout << "You already have " << player.offhandWeaponEquipped << " equipped in your off hand!\n";
} else {
player.offhandWeaponEquipped = itemToCheck.GetDispName();
player.damageMin += itemToCheck.Attack;
player.damageMax += itemToCheck.Attack;
player.armorClass += itemToCheck.Defence;
}
break;
case 2:
std::cout << "Ok\n";
break;
default:
funcLoop_1 = true;
}
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
}
}
}
For context, I'm using an unordered_map as the inventory.
Any help is appreciated! If it wasn't already clear I'm asking what the most effective way to build this inventory system is.
What I have tried:
Experimented with Vectors and Lists and Google.