]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
quest requirements/rewards
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 12 Feb 2017 20:27:02 +0000 (15:27 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 12 Feb 2017 20:27:02 +0000 (15:27 -0500)
include/quest.hpp
src/components.cpp
src/inventory.cpp
src/quest.cpp
xml/!town.xml

index 27b533d8e2c44abc397998295ee9cd83b1d2fdf3..b74438dc31a224986e4f81b0609f1a220c725f9c 100644 (file)
@@ -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 */
 };
 
 /**
index cbec9fc63b81ae95ef682bfd4c87db6e586bcc59..dfce75c85fa44953d18ca5cb207bbf3740239785 100644 (file)
@@ -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");
index bf6b6bbef36bd52570362f730ceeebc2549a5956..3995e3ab71c7628973111266df21a0404c62ae86 100644 (file)
@@ -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)
index a0f9a6d395565289e3f6d56443690feac3ef47ae..71a37fd464f617995206472cf55b554ffe508769 100644 (file)
@@ -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 = &current.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 = &current.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);
 
index 9ab4c1c47c67d825a72ed61a2fb53c9e2f74a956..d256af9342fa4bf0d2cc4e626b1937113dad7737 100644 (file)
@@ -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"/>
 </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&apos;swag, man!" desc="Go inside Bob&apos;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&apos;swag, man!" />
+               <content>Hooray!</content>
+       </text>
 </Dialog>
 
 <Dialog name="Sanc">