diff options
-rw-r--r-- | include/quest.hpp | 5 | ||||
-rw-r--r-- | src/components.cpp | 2 | ||||
-rw-r--r-- | src/inventory.cpp | 3 | ||||
-rw-r--r-- | src/quest.cpp | 39 | ||||
-rw-r--r-- | xml/!town.xml | 13 |
5 files changed, 52 insertions, 10 deletions
diff --git a/include/quest.hpp b/include/quest.hpp index 27b533d..b74438d 100644 --- a/include/quest.hpp +++ b/include/quest.hpp @@ -7,6 +7,7 @@ #include <entityx/entityx.h> +#include <forward_list> #include <string> #include <vector> @@ -21,6 +22,10 @@ struct Quest std::string name; /**< the quest's title */ std::string desc; /**< the quest's description */ + + using Req = std::pair<std::string, int>; + std::forward_list<Req> reqs; /**< the quest's item requirements */ + std::forward_list<Req> rewards; /**< the quest's rewards */ }; /** diff --git a/src/components.cpp b/src/components.cpp index cbec9fc..dfce75c 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -257,7 +257,7 @@ void DialogSystem::receive(const MouseClickEvent &mce) else { qname = qxml->Attribute("check"); if (qname != nullptr) { - if (qname != nullptr && qsys->hasQuest(qname) && qsys->finish(qname) == 0) { + if (qname != nullptr && qsys->finish(qname) == 0) { d.index = 9999; } else { ui::dialogBox(name.name, "", false, "Finish my quest u nug"); diff --git a/src/inventory.cpp b/src/inventory.cpp index bf6b6bb..3995e3a 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -240,11 +240,12 @@ bool InventorySystem::take(const std::string& name, int count) return ie.item != nullptr && ie.item->name == name; }); - if (i == items.end()) + if (i >= items.end()) break; toDelete.push_front(i); taken += i->count; + next = i + 1; } if (taken < count) diff --git a/src/quest.cpp b/src/quest.cpp index a0f9a6d..71a37fd 100644 --- a/src/quest.cpp +++ b/src/quest.cpp @@ -2,8 +2,20 @@ #include <algorithm> +#include <engine.hpp> +#include <inventory.hpp> #include <tokens.hpp> +std::string& trim(std::string& s) +{ + auto start = std::find_if(s.begin(), s.end(), + [](char c) { return !isspace(c); }) - s.begin(); + auto end = std::find_if(s.rbegin(), s.rend(), + [](char c) { return !isspace(c); }).base() - s.begin(); + s = s.substr(start, end - start); + return s; +} + void QuestSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; @@ -13,14 +25,25 @@ void QuestSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, int QuestSystem::assign(std::string title, std::string desc, std::string req) { + current.emplace_back(title, desc); + + auto* list = ¤t.back().reqs; + std::string reqTitle; for (auto s : tokens(req, ',')) { - s.erase(std::remove_if(s.begin(), s.end(), - [](char c) { return isspace(c); }), s.end()); + trim(s); - std::cout << s << '\n'; + if (s == "Reward") { + list = ¤t.back().rewards; + } else { + if (!reqTitle.empty()) { + list->emplace_front(reqTitle, std::stoi(s)); + reqTitle.clear(); + } else { + reqTitle = s; + } + } } - current.emplace_back(title, desc); return 0; } @@ -39,7 +62,13 @@ int QuestSystem::finish(std::string title) if (quest == std::end(current)) return -1; - // TODO requirements + for (const auto& r : quest->reqs) { + if (!game::engine.getSystem<InventorySystem>()->take(r.first, r.second)) + return -1; + } + + for (const auto& r : quest->rewards) + game::engine.getSystem<InventorySystem>()->add(r.first, r.second); drop(title); diff --git a/xml/!town.xml b/xml/!town.xml index 9ab4c1c..d256af9 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -7,6 +7,7 @@ <weather>Sunny</weather> <link right="!town2.xml"/> <spawnx>-300</spawnx> + <time>8000</time> <npc name="Sanc" hasDialog="true"/> <npc name="Bob" hasDialog="true" position="50.0,100.0"/> <structure type="1" position="300.0,100.0"/> @@ -15,20 +16,26 @@ </World> <Dialog name="Bob"> - <text id="0" nextid="1" pause="true"> + <text id="0" nextid="1"> <give name="Dank MayMay" count="10"/> <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"> + <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."> - Debug, 5 + Debug, 5, + Reward, + Dank MayMay, 50 </quest> <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> + <text id="2"> + <quest check="Check out m'swag, man!" /> + <content>Hooray!</content> + </text> </Dialog> <Dialog name="Sanc"> |