Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
///inventory.cpp
#include "inventory.h"
#include <iostream>
Inventory::Inventory():weapon_slot(1,"Bare Fists",0),armor_slot(0,"Bare Chest",0)
{
max_inventory_space = 10;
number_of_items = 0;
}
void Inventory::add_item(Item& item,int mode_of_operation)
{
///function called mainly when adding items to inventory, but is also a part of swapping items explained in function swap_item()
if (mode_of_operation == 1){
///this mode of operation is used when swapping objects and used only
///when called by swap_item() function
std::unique_ptr<Item> ptr(&item);
Inventory_array[10] = std::move(ptr);
}
else{
///this mode of operation is used when adding object
if (number_of_items < max_inventory_space)
{
///using ptr(new Item(&item)) creates an error
std::unique_ptr<Item> ptr(&item);
Inventory_array[number_of_items] = std::move(ptr);
number_of_items += 1;
}
else{std::cout<<"Inventory is full"<<std::endl;}
}}
void Inventory::remove_item(int item_number)
{
///function for removing items
if (number_of_items < item_number){std::cout<<"Invalid item number"<<std::endl;}
else if(number_of_items <= 0){std::cout<<"Out of range"<<std::endl;}
else
{
///at first I have tried deleting the unique_ptr, but got Critical error detected c0000374
///so instead I am releasing the memory it occupies, and then deleting the object by changing its value to 0
///if there was only one item in the inventory, or the deleted item was the last one, then
///moving other items to fill the blank would not be needed, otherwise the last item from
///inventory is used to fill the blank
Inventory_array[item_number - 1].release();
Inventory_array[item_number - 1] = 0;
if(number_of_items > 1 and item_number != number_of_items)
{
Inventory_array[item_number - 1] = std::move(Inventory_array[number_of_items - 1]);
}
number_of_items -= 1;
}
}
void Inventory::display_inventory()
{
///function for displaying the inventory, it does not display Inventory_array[10] as it is used only for swapping items
for(int i = 0; i<number_of_items; i++)
{
std::cout<<i + 1<<": ";
Inventory_array[i]->display_item();
}
}
void Inventory::swap_item(int item_number,std::string item_type)
{
if (number_of_items < item_number){std::cout<<"Invalid item number"<<std::endl;}
else if(number_of_items <= 0){std::cout<<"Out of range"<<std::endl;}
else
{
if (item_type == "armor" & Inventory_array[item_number - 1]->ID == 0)
{ /// fuction for swapping armor_slot and weapon_slot with items from inventory
/// all armor has id 0, and weapon id 1, this is how it checks if the exchange is valid,
///I have tried using swap, but once again got the error Critical error detected c0000374, and nothing
///I can find on the internet helps me fix it
///The problem is that an item inside and outside array are not treated as objects from one class,
Inventory::add_item(armor_slot,1);
///bug, swapping only works one way, as the Inventory_array[10] is updated, but Inventory_array[item_number - 1] not, it is not related to releasing Inventory_array[10]
///trying to fix the bug by manualy assigning values of Inventory_array[item_number - 1] does not work, as it reverses the changes made to weapon slot
///I do not understand how a line Inventory_array[item_number - 1]->name = placeholder_name can reverse changes made to weapon_slot, but it somehow does it
Inventory_array[item_number - 1].swap(Inventory_array[10]);
armor_slot.name = Inventory_array[10]->name;
armor_slot.stat = Inventory_array[10]->stat;
Inventory_array[10].release();
Inventory_array[10] = 0;
}
else if(item_type == "weapon" & Inventory_array[item_number - 1]->ID == 1)
{
Inventory::add_item(weapon_slot,1);
Inventory_array[item_number - 1].swap(Inventory_array[10]);
weapon_slot.name = Inventory_array[10]->name;
weapon_slot.stat = Inventory_array[10]->stat;
Inventory_array[10].release();
Inventory_array[10] = 0;
}
}
}
void Inventory::inventory_menu()
{
std::cout<<">>>INVENTORY<<<"<<std::endl;
std::cout<<"WEAPON SLOT: ";
weapon_slot.display_item();
std::cout<<"ARMOR SLOT: ";
armor_slot.display_item();
Inventory::display_inventory();
std::cout<<"------------------"<<std::endl;
std::cout<<"1: REMOVE ITEM"<<std::endl;
std::cout<<"2: EQUIP WEAPON"<<std::endl;
std::cout<<"3: EQUIP ARMOR"<<std::endl;
std::cout<<"4: QUIT"<<std::endl;
std::cout<<">";
std::cin >>player_input;
switch(player_input)
{
case 1:
std::cout<<"\nwhich item to remove?"<<std::endl<<">";
std::cin >>player_input;
Inventory::remove_item(player_input);
break;
case 2:
std::cout<<"\nwhich weapon to equip?"<<std::endl<<">";
std::cin >>player_input;
Inventory::swap_item(player_input,"weapon");
break;
case 3:
std::cout<<"\nwhich armor to equip?"<<std::endl<<">";
std::cin >>player_input;
Inventory::swap_item(player_input,"armor");
break;
case 4:
break;
}
}