#include "Item.h" #include "safemap.h" #include "DEFINES.h" #include "Crawler.h" #include "Menu.h" INCLUDE_game INCLUDE_DATA INCLUDE_GFX safemapITEM_DATA; safemapITEM_SCRIPTS; safemap>ITEM_CATEGORIES; Item Item::BLANK; std::mapInventory::_inventory; std::map>Inventory::sortedInv; ItemInfo::ItemInfo() :customProps({nullptr,nullptr}),img(nullptr){} void ItemInfo::InitializeItems(){ InitializeScripts(); for(auto&key:DATA["ItemCategory"].GetKeys()){ ITEM_CATEGORIES[key.first]; Inventory::sortedInv[key.first]; Menu::inventoryListeners[key.first]; } for(auto&key:DATA["ItemDatabase"].GetKeys()){ std::string imgPath="assets/"+"item_img_directory"_S+key.first+".png"; Renderable&img=GFX["item_img_directory"_S+key.first+".png"]; img.Load(imgPath); std::string scriptName="",description="",category=""; for(auto&itemKey:DATA["ItemDatabase"][key.first].GetKeys()){ std::string keyName=itemKey.first; if(keyName=="Description"){ description=DATA["ItemDatabase"][key.first][keyName].GetString(); }else if(keyName=="ItemCategory"){ category=DATA["ItemDatabase"][key.first][keyName].GetString(); }else if(keyName=="ItemScript"){ scriptName=DATA["ItemDatabase"][key.first][keyName].GetString(); }else{ //THis is a custom override modifier for a script. NO-OP } } if(scriptName!=""){ if(!ITEM_SCRIPTS.count(scriptName)){ ERR("Could not load script "<HasProperty(prop)) return (*customProps)[prop].GetInt(); else return (*scriptProps)[prop].GetInt(); }; float ItemProps::GetFloatProp(std::string prop){ if(customProps->HasProperty(prop)) return (*customProps)[prop].GetReal(); else return (*scriptProps)[prop].GetReal(); }; std::string ItemProps::GetStringProp(std::string prop){ if(customProps->HasProperty(prop)) return (*customProps)[prop].GetString(); else return (*scriptProps)[prop].GetString(); }; void ItemInfo::InitializeScripts(){ ITEM_SCRIPTS["Restore"]=[](Crawler*game,ItemProps props){ game->GetPlayer()->Heal(props.GetIntProp("HP Restore")); game->GetPlayer()->Heal(game->GetPlayer()->GetMaxHealth()*props.GetIntProp("HP % Restore")/100.f); game->GetPlayer()->RestoreMana(props.GetIntProp("MP Restore")); game->GetPlayer()->RestoreMana(game->GetPlayer()->GetMaxMana()*props.GetIntProp("MP % Restore")/100.f); return true; }; ITEM_SCRIPTS.SetInitialized(); std::cout<=_inventory.at(it).Amt()){ int count=0; std::vector&sortedList=sortedInv.at(_inventory.at(it).Category()); for(std::string&itemName:sortedList){ if(itemName==it)break; count++; } sortedList.erase(sortedList.begin()+count); _inventory.erase(it); ITCategory cat=ITEM_DATA[it].category; //Callback for GUI inventories. Menu::InventorySlotsUpdated(cat); return true; }else{ _inventory.at(it).amt-=amt; return false; } } std::vector&Inventory::get(ITCategory itemCategory){ return sortedInv.at(itemCategory); } void Inventory::InsertIntoSortedInv(IT item){ sortedInv.at(ITEM_DATA[item].category).push_back(item); //This should be a callback to menus that we need to update the interface with another item slot since a new one has appeared. Menu::InventorySlotsUpdated(ITEM_DATA[item].category); } bool Inventory::ExecuteAction(IT item){ if(ITEM_SCRIPTS.count(ITEM_DATA.at(item).useFunc)){ return ITEM_SCRIPTS.at(ITEM_DATA.at(item).useFunc)(game,ITEM_DATA[item].customProps); }else{ return false; } } bool Inventory::SwapItems(ITCategory itemCategory,uint32_t slot1,uint32_t slot2){ std::vector&inv=sortedInv.at(itemCategory); int largestSlot=std::max(slot1,slot2); if(inv.size()<=largestSlot){ //The inventory is too small, so expand out blank slots to accomodate. inv.resize(largestSlot+size_t(1)); } IT&item1=inv.at(slot1); IT&item2=inv.at(slot2); std::swap(item1,item2); return true; } uint32_t Item::Amt(){ return amt; }; std::string Item::Name(){ return it->Name(); }; std::string Item::Description(){ return it->Description(); }; ITCategory Item::Category(){ return it->Category(); }; Decal*Item::Decal(){ return it->Decal(); }; ItemScript&Item::OnUseAction(){ return it->OnUseAction(); }; std::string ItemInfo::Name(){ return name; }; std::string ItemInfo::Description(){ return description; }; ITCategory ItemInfo::Category(){ return category; }; Decal*ItemInfo::Decal(){ return img; }; ItemScript&ItemInfo::OnUseAction(){ return ITEM_SCRIPTS.at(useFunc); }; bool Item::IsBlank(){ return amt==0||it==nullptr; }