Source

zorkish / ActiveStageManager.hpp

Full commit
/*
 * ActiveStageManager.hpp
 *
 *  Created on: 19/08/2012
 *      Author: rhys
 */

#ifndef ACTIVESTAGEMANAGER_HPP_
#define ACTIVESTAGEMANAGER_HPP_

#include <stack>
#include "GameStage.hpp"

class ActiveStageManager {
private:
    GameStage* next_stage = nullptr;
    std::stack<GameStage*> stage_stack;

public:
    bool ready() {
        return !this->stage_stack.empty();
    }

    void set_next_stage(GameStage* stage) {
        if (this->stage_stack.empty()) {
            this->stage_stack.push(stage);
            stage->activate();
        } else {
            this->next_stage = stage;
        }
    }

    void update() {
        GameStage* current_stage = this->stage_stack.top();
        auto css = current_stage->update(*this);

        if (this->next_stage != nullptr || css == GameStage::Status::FINISH) {
            current_stage->deactivate();
        }

        if (css == GameStage::Status::FINISH) {
            current_stage->cleanup();
            this->stage_stack.pop();
        }

        if (this->next_stage != nullptr) {
            this->stage_stack.push(next_stage);
            next_stage->activate();
            next_stage = nullptr;
        }
    }

    void draw() {
        this->stage_stack.top()->draw();
    }
};

#endif /* ACTIVESTAGEMANAGER_HPP_ */