]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
inventory remembers slot positions
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 20 Sep 2017 15:25:26 +0000 (11:25 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 20 Sep 2017 15:25:26 +0000 (11:25 -0400)
include/components/dialog.hpp
include/inventory.hpp
src/inventory.cpp
src/systems/dialog.cpp
xml/!town.xml
xml/bobshouse.xml

index 8d26b92104852bb4751f79306ac9403fe4c07aa2..0bc23e832a1aefb2619425e8aab0eb329adacc99 100644 (file)
@@ -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);
        }
index f340a244132ecd44febe914203339f31c085318b..f8581ccf9ea8cefc46d96ac135b049b1cab120ce 100644 (file)
@@ -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);
 
index 1fdaf63ba4d4c6c90195022438b8ec2030056030..36cc0419ae2d0071683ea4006cda12bbd1ad2897 100644 (file)
@@ -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);
                        }
                }
        }
index f4f479f5a508215650b648f56dc2618f5cb0029b..3c4b4fc1a8f4506cf58400a8357687c2afd1dc62 100644 (file)
@@ -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();
index a86461a2a5b1b6aacb2ac6b39edf221d8fdb921a..ffebf19ec3947cdd3ba7df9316ff42336ab1522c 100644 (file)
        <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&apos;swag, man!" desc="Go inside Bob&apos;s house and check out the stuff he has.">
+        <quest assign="Check out m&apos;swag, man!" desc="Go inside Bob&apos;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>
index 0b287b6b1419339dc7f0f83a3da257f2618d7b4a..d7c81725eb3ec23bbf21a1582c2c4cd6f22af3ba 100644 (file)
        <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>
+