Source

zorkish / SelectAdventureStage.hpp

/*
 * SelectAdventureStage.hpp
 *
 *  Created on: 20/08/2012
 *      Author: rhys
 */

#ifndef SELECTADVENTURESTAGE_HPP_
#define SELECTADVENTURESTAGE_HPP_

#include <fstream>
#include "AdventureStage.hpp"

class SelectAdventureStage : public ZorkishStage {
private:
    std::string message;

    std::unique_ptr<World> test_world() {
        auto w = make_unique<World>();
        auto* sloc = new Location("Void World", "This world is simple and pointless. Used it to test Zorkish phase 1 spec.");
        sloc->inv.add(make_unique<Item>("delicious cake", Item::Ids {"cake"}));
        sloc->inv.add(make_unique<Item>("a rock", Item::Ids {"rock"}));
        w->add_location(0, sloc);
        return w;
    }

    std::unique_ptr<World> load() {
        std::ifstream f;
        auto w = make_unique<World>();
        f.open("world.txt");
        while (!f.eof()) {
            int id;
            f >> id;
            f.ignore(1, '|');
            std::string name;
            getline(f, name, '|');
            std::string desc;
            getline(f, desc, '|');
            auto* loc = new Location(name, desc);
            while (f.peek() != '\n' && !f.eof()) { // peek sets eof
                std::string link;
                getline(f, link, ';');

                auto seploc = link.find('=');
                auto to = boost::lexical_cast<int>(link.substr(seploc + 1, link.length()));
                loc->exits[link.substr(0, seploc)] = to;
            }
            w->add_location(id, loc);
        }
        return w;
    }

    GameStage::Status setup_adventure(ActiveStageManager& smgr,
            std::unique_ptr<World> world) {
        this->game.adventure()->setup(std::move(world), 0);
        smgr.set_next_stage(this->game.adventure().get());
        return GameStage::Status::FINISH;
    }

public:
    SelectAdventureStage(ZorkishGame& game) : ZorkishStage(game) {};

    virtual Status update(ActiveStageManager& smgr) {
        switch (this->input_int()) {
        case 1:
            return this->setup_adventure(smgr, this->test_world());
        case 2:
            return this->setup_adventure(smgr, this->load());
        default:
            this->message = "Please select a number listed above.";
            break;
        }

        return GameStage::Status::CONTINUE;
    }

    virtual void draw() {
        using std::cout;
        cout << "===Select Adventure===\n";
        cout << "Choose your adventure:\n";
        cout << "1. Test world\n";
        cout << "2. Load from world.txt\n";
        if (!this->message.empty()) {
            cout << this->message << '\n';
            this->message.clear();
        }
        cout << "Select 1-2: ";
    }
};

#endif /* SELECTADVENTURESTAGE_HPP_ */