diff options
Diffstat (limited to 'src/quest.cpp')
-rw-r--r-- | src/quest.cpp | 39 |
1 files changed, 34 insertions, 5 deletions
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); |