diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-20 11:25:26 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-20 11:25:26 -0400 |
commit | 8720f1f253b55fa5233626dd854671a5925d65de (patch) | |
tree | 8f6017a662288d65e5265a730a347301e596e81a | |
parent | f7ec6aff4a7312586e986c891f370c442a6ae093 (diff) |
inventory remembers slot positions
-rw-r--r-- | include/components/dialog.hpp | 2 | ||||
-rw-r--r-- | include/inventory.hpp | 1 | ||||
-rw-r--r-- | src/inventory.cpp | 53 | ||||
-rw-r--r-- | src/systems/dialog.cpp | 6 | ||||
-rw-r--r-- | xml/!town.xml | 8 | ||||
-rw-r--r-- | xml/bobshouse.xml | 17 |
6 files changed, 55 insertions, 32 deletions
diff --git a/include/components/dialog.hpp b/include/components/dialog.hpp index 8d26b92..0bc23e8 100644 --- a/include/components/dialog.hpp +++ b/include/components/dialog.hpp @@ -7,7 +7,7 @@ struct Dialog : public Component { Dialog(int idx = 0) - : index(idx), rindex((idx == 9999) ? randGet() : idx), talking(false) {} + : index(idx), rindex(randGet()), talking(false) {} Dialog(XMLElement* imp, XMLElement* def) { fromXML(imp, def); } diff --git a/include/inventory.hpp b/include/inventory.hpp index f340a24..f8581cc 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -91,6 +91,7 @@ private: static bool fullInventory; static void loadItems(void); + static void add(const std::string& name, int count, int slot); public: InventorySystem(int size = 20); diff --git a/src/inventory.cpp b/src/inventory.cpp index 1fdaf63..36cc041 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -320,30 +320,34 @@ bool InventorySystem::receive(const KeyDownEvent &kde) return true; } -void InventorySystem::add(const std::string& name, int count) +void InventorySystem::add(const std::string& name, int count, int slot) { - auto i = std::find_if(items.begin(), items.end(), - [&name](const InventoryEntry& ie) { - // either matching item that isn't filled, or empty slow - return (ie.item != nullptr && ie.item->name == name && ie.count < ie.item->stackSize) || ie.count == 0; - }); + auto& ie = items[slot]; - if (i != items.end()) { - auto& ie = *i; + if (isGoodEntry(ie)) { + if (ie.item->name != name) + return; // TODO behavior? + ie.count += count; + } else { + ie.item = &itemList[name]; + ie.count = count; + } - // update the slot - if (!isGoodEntry(ie)) { - ie.item = &itemList[name]; - ie.count = count; - } else { - ie.count += count; - } + if (ie.count > ie.item->stackSize) { + int diff = ie.count - ie.item->stackSize; + ie.count = ie.item->stackSize; + add(name, diff); + } +} - // handle overflow - if (ie.count > ie.item->stackSize) { - int diff = ie.count - ie.item->stackSize; - ie.count = ie.item->stackSize; - add(name, diff); +void InventorySystem::add(const std::string& name, int count) +{ + for (unsigned int i = 0; i < items.size(); i++) { + if ((items[i].item != nullptr && items[i].item->name == name + && items[i].count < items[i].item->stackSize) + || items[i].count == 0) { + add(name, count, static_cast<int>(i)); + break; } } } @@ -395,9 +399,11 @@ bool InventorySystem::save(void) // signature? s << "831998\n"; + int idx = 0; for (const auto& i : items) { if (i.item != nullptr && i.count > 0) - s << std::string(i.item->name) << '\n' << i.count << '\n'; + s << std::string(i.item->name) << '\n' << i.count << '\n' << idx << '\n'; + idx++; } // end of list? @@ -417,11 +423,12 @@ void InventorySystem::load(void) if (std::stoi(lines[0]) != 831998) UserError("Save file signature is invalid... (delete it)"); - for (unsigned int i = 1; i < lines.size(); i += 2) { + for (unsigned int i = 1; i < lines.size(); i += 3) { if (lines[i].size() > 0) { int count = std::stoi(lines[i + 1]); + int slot = std::stoi(lines[i + 2]); if (count > 0) - add(lines[i], count); + add(lines[i], count, slot); } } } diff --git a/src/systems/dialog.cpp b/src/systems/dialog.cpp index f4f479f..3c4b4fc 100644 --- a/src/systems/dialog.cpp +++ b/src/systems/dialog.cpp @@ -42,12 +42,12 @@ bool DialogSystem::receive(const MouseClickEvent &mce) if (!dialogRun.load()) { // copy entity, windows destroys the original after thread detach + dialogRun.store(true); std::thread([e, &pos, &dim, &d, &name] { std::string questAssignedText; int newIndex; auto exml = WorldSystem::getXML()->FirstChildElement("Dialog"); - dialogRun.store(true); if (e.has_component<Direction>()) d.talking = true; @@ -100,7 +100,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce) } else { UISystem::dialogBox(name.name, "Finish my quest u nug"); UISystem::waitForDialog(); - return; + goto END; } // oldidx = d.index; // d.index = qxml->UnsignedAttribute("fail"); @@ -139,7 +139,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce) else if (exml->QueryIntAttribute("nextid", &newIndex) == XML_NO_ERROR) d.index = newIndex; } - +END: d.talking = false; dialogRun.store(false); }).detach(); diff --git a/xml/!town.xml b/xml/!town.xml index a86461a..ffebf19 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -28,18 +28,18 @@ <text id="0" nextid="1"> <give name="Hunters Bow" count="1"/> <give name="Arrow" count="50"/> - <option name="Yes" /> - <content> + <option name="Yes"/> + <content> Hey there! The name's Bob. Good to see you've finally woken up from your nap by the cliff there... lol </content> </text> <text id="1" pause="true" nextid="2"> - <quest assign="Check out m'swag, man!" desc="Go inside Bob's house and check out the stuff he has."> + <quest assign="Check out m'swag, man!" desc="Go inside Bob's house and check out the stuff he has."> Debug, 5, Reward, Dank MayMay, 50 </quest> - <content> + <content> Looks like you've got yourself pretty empty handed... you know, I have a simple solution for that. Come on inside, I have somethin' to show you. </content> </text> diff --git a/xml/bobshouse.xml b/xml/bobshouse.xml index 0b287b6..d7c8172 100644 --- a/xml/bobshouse.xml +++ b/xml/bobshouse.xml @@ -16,5 +16,20 @@ <generation width="320"/> <time>6000</time> <!--<link outside="town.xml"/>--> - <npc name="Bob" hasDialog="false" spawnx="30"/> + <npc name="Bob" hasDialog="true" spawnx="30"/> </IndoorWorld> + +<Dialog name="Bob"> + <text id="0" nextid="1"> + <content> + Hey there lil' guy! Someone lookin' for some stuff? + </content> + </text> + <text id="1"> + <give name="Debug" count="5"/> + <content> + Here ya' go boy! + </content> + </text> +</Dialog> + |