Source

zorkish / AdventureStage.hpp

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

#ifndef ADVENTURESTAGE_HPP_
#define ADVENTURESTAGE_HPP_

#include <boost/algorithm/string/case_conv.hpp>
#include "HighScoreStage.hpp"
#include "Player.hpp"
#include "util.cpp"
#include "World.hpp"

class AdventureStage : public ZorkishStage {
private:
    std::string message;
    std::unique_ptr<World> world;
    Player player;
    int moves;
    bool hiscore;

    void pick_up(std::string name) {
        std::stringstream ss;
        auto it = this->player.current_location()->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->player.current_location()->inv.add(std::move(it));
        } else {
            ss << "You're not carrying a " << name << ".";
        }

        this->message = ss.str();
    }

    void look() {
        this->message = this->player.current_location()->long_desc();
    }

    void go(std::string dirn) {
        auto it = this->player.current_location()->exits.find(dirn);
        if (it != this->player.current_location()->exits.end()) {
            this->player.move(this->world->get_location((*it).second));
            this->look();
        } else {
            this->message = "You can't go " + dirn + " from here.";
        }
    }

public:
    AdventureStage(ZorkishGame& game)
            : ZorkishStage(game) { }

    void setup(std::unique_ptr<World> world, int starting_location) {
        auto* start = world->get_location(starting_location);
        this->world.swap(world);
        this->message = start->long_desc();
        this->moves = 0;
        this->player = Player(start);
        this->hiscore = false;
    }

    virtual Status update(ActiveStageManager& smgr) {
        if (!this->player.alive) {
            if (this->hiscore) {
                auto& hs = this->game.high_score();
                hs->set_score(10, this->moves, "Test Adventure");
                smgr.set_next_stage(hs.get());
            }

            return GameStage::Status::FINISH;
        }

        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->player.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->player.alive = false;
            } else if (cmd == "LOOK") {
                this->look();
            } else if (cmd == "INV") {
                this->message = this->player.inv.list("Inventory:", "\n  * ", "You aren't carrying anything.");
            } else {
                this->message = "I don't know how to do that.";
            }
        } else {
            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 if (cmd == "GO") {
                this->go(params.substr(1, params.size() - 1));
            } else {
                this->message = "I don't know how to do that.";
            }
        }

        this->moves++;
        return GameStage::Status::CONTINUE;
    }

    virtual void draw() {
        using std::cout;
        if (!this->message.empty()) {
            cout << this->message << '\n';
            this->message.clear();
        }
        if (this->player.alive) {
            cout << ":> ";
        }
    }
};

#endif /* ADVENTURESTAGE_HPP_ */