Commits

Rhys !  committed 4be76b4 Draft

implemented inventory (Spike 6)

  • Participants
  • Parent commits bdc1d72

Comments (0)

Files changed (7)

File ActiveStageManager.hpp

 
     void update() {
         GameStage* current_stage = this->stage_stack.top();
-        GameStage::Status css = current_stage->update(*this);
+        auto css = current_stage->update(*this);
 
         if (this->next_stage != nullptr || css == GameStage::Status::FINISH) {
             current_stage->deactivate();

File Inventory.hpp

+/*
+ * Inventory.hpp
+ *
+ *  Created on: 24/08/2012
+ *      Author: rhys
+ */
+
+#ifndef INVENTORY_HPP_
+#define INVENTORY_HPP_
+
+#include <list>
+#include <memory>
+#include "Item.hpp"
+
+class Inventory {
+private:
+    static const std::unique_ptr<Item> EMPTY_PTR;
+    std::list<std::unique_ptr<Item>> items;
+
+    auto find_it(const std::string& name)
+            -> std::list<std::unique_ptr<Item>>::iterator {
+        return std::find_if(begin(this->items), end(this->items),
+            [&](const std::unique_ptr<Item>& item) {
+                return item->identifies_as(name);
+            });
+    }
+
+public:
+    typedef std::list<std::unique_ptr<Item>>::const_iterator const_iterator;
+
+    bool empty() const {
+        return this->items.empty();
+    }
+
+    void add(std::unique_ptr<Item> item) {
+        this->items.push_back(std::move(item));
+    }
+
+    const std::unique_ptr<Item>& get(const std::string& name) {
+        auto it = this->find_it(name);
+        if (it != end(this->items)) {
+            return *it;
+        } else {
+            return Inventory::EMPTY_PTR;
+        }
+    }
+
+    std::unique_ptr<Item> take(const std::string& name) {
+        auto it = this->find_it(name);
+        if (it != end(this->items)) {
+            std::unique_ptr<Item> item = std::move(*it);
+            this->items.erase(it);
+            return item;
+        } else {
+            return std::unique_ptr<Item>();
+        }
+    }
+
+    Inventory::const_iterator cbegin() const {
+        return this->items.cbegin();
+    }
+
+    Inventory::const_iterator cend() const {
+        return this->items.cend();
+    }
+
+    void for_each(std::function<void(const std::unique_ptr<Item>&)> f) const {
+        std::for_each(this->cbegin(), this->cend(), f);
+    }
+};
+
+
+#endif /* INVENTORY_HPP_ */
+/*
+ * Item.hpp
+ *
+ *  Created on: 24/08/2012
+ *      Author: rhys
+ */
+
+#ifndef ITEM_HPP_
+#define ITEM_HPP_
+
+#include <algorithm>
+#include <functional>
+#include <vector>
+#include <boost/algorithm/string/predicate.hpp>
+
+class Item {
+private:
+    std::string _short_name;
+    std::vector<std::string> ids;
+
+public:
+    // Use std::move for names
+    Item(std::string short_name, std::vector<std::string> ids) :
+        _short_name(short_name), ids(ids) {};
+
+    Item(std::string short_name, std::initializer_list<std::string> ids) :
+        _short_name(short_name), ids(std::vector<std::string>(ids)) {};
+
+    bool identifies_as(const std::string& id) {
+        return boost::iequals(this->_short_name, id) ||
+            std::find_if(begin(ids), end(ids),
+                    [&](const std::string& td){return boost::iequals(td, id);})
+                != end(ids);
+    }
+
+    const std::string& short_name() {
+        return this->_short_name;
+    }
+};
+
+#endif /* ITEM_HPP_ */
+/*
+ * Player.hpp
+ *
+ *  Created on: 25/08/2012
+ *      Author: rhys
+ */
+
+#ifndef PLAYER_HPP_
+#define PLAYER_HPP_
+
+#include "Inventory.hpp"
+
+class Player {
+public:
+    Inventory inv;
+};
+
+
+#endif /* PLAYER_HPP_ */

File TestAdventure.hpp

 #define TESTADVENTURE_HPP_
 
 #include <iostream>
-#include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
 #include "HighScoreStage.hpp"
+#include "Player.hpp"
+#include "util.cpp"
 
 class TestAdventure : public ZorkishStage {
 private:
     std::string message;
+    Inventory world_inv;
+    Player player;
     int moves;
     bool alive;
     bool hiscore;
 
+    void pick_up(std::string name) {
+        std::stringstream ss;
+        auto it = this->world_inv.take(name);
+        if (it) {
+            ss << "You pick up " << it->short_name() << ".";
+            this->player.inv.add(std::move(it));
+        } else {
+            ss << "There is no " << name << " here.";
+        }
+
+        this->message = ss.str();
+    }
+
+    void drop(std::string name) {
+        std::stringstream ss;
+        auto it = this->player.inv.take(name);
+        if (it) {
+            ss << "You drop " << it->short_name() << ".";
+            this->world_inv.add(std::move(it));
+        } else {
+            ss << "You're not carrying a " << name << ".";
+        }
+
+        this->message = ss.str();
+    }
+
+    void list_items(const Inventory& inv) {
+        if (inv.empty()) {
+            this->message = "There is nothing there.";
+            return;
+        }
+
+        std::stringstream ss;
+        inv.for_each([&](const std::unique_ptr<Item>& item)
+                {ss << item->short_name() << '\n';});
+        this->message = ss.str();
+        this->message.resize(this->message.size() - 1);
+    }
+
 public:
     TestAdventure(ZorkishGame& game) : ZorkishStage(game) {
         this->cleanup();
     virtual Status update(ActiveStageManager& smgr) {
         if (!this->alive) {
             if (this->hiscore) {
-                this->game.high_score().set_score(10, this->moves, "Test Adventure");
-                smgr.set_next_stage(&this->game.high_score());
+                auto& hs = this->game.high_score();
+                hs.set_score(10, this->moves, "Test Adventure");
+                smgr.set_next_stage(&hs);
             }
 
             return GameStage::Status::FINISH;
         }
 
-        std::string in;
-        getline(std::cin, in);
-        boost::to_upper(in);
-        if (in == "QUIT") {
-            this->message = "Your adventure has ended without fame or fortune.";
-            this->alive = false;
-        } else if (in == "HISCORE") {
-            this->message = "You have entered the magic word and will now see the \"New High Score\" screen.";
-            this->hiscore = true;
-            this->alive = false;
+        std::string cmd;
+        std::string params;
+        std::cin >> cmd;
+        getline(std::cin, params);
+        boost::to_upper(cmd);
+        if (params.empty()) {
+            if (cmd == "QUIT") {
+                this->message = "Your adventure has ended without fame or fortune.";
+                this->alive = false;
+            } else if (cmd == "HISCORE") {
+                this->message = "You have entered the magic word and will now see the \"New High Score\" screen.";
+                this->hiscore = true;
+                this->alive = false;
+            } else if (cmd == "LOOK") {
+                this->list_items(this->world_inv);
+            } else if (cmd == "INV") {
+                this->list_items(this->player.inv);
+            } else {
+                this->message = "I don't know how to do that.";
+            }
         } else {
-            this->message = "I don't know how to do that.";
+            if (cmd == "TAKE") {
+                this->pick_up(params.substr(1, params.size() - 1));
+            } else if (cmd == "DROP") {
+                this->drop(params.substr(1, params.size() - 1));
+            } else {
+                this->message = "I don't know how to do that.";
+            }
         }
 
         this->moves++;
         this->moves = 0;
         this->alive = true;
         this->hiscore = false;
+        this->player = Player();
+        this->world_inv = Inventory();
+        this->world_inv.add(make_unique<Item>("delicious cake", std::vector<std::string> {"cake"}));
+        this->world_inv.add(make_unique<Item>("a rock", std::vector<std::string> {"rock"}));
     }
 };
 
 
 int main() {
     ZorkishGame game;
-    ActiveStageManager& smgr = game.stage_manager();
+    auto smgr = game.stage_manager();
 
     while (smgr.ready()) {
         smgr.draw();
+/*
+ * util.cpp
+ *
+ *  Created on: 25/08/2012
+ *      Author: rhys
+ */
+
+#include <memory>
+
+template<typename T, typename... Args>
+std::unique_ptr<T> make_unique(Args&&... args)
+{
+    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}