#include "generate_dungeon.h" #include "load_dungeon.h" #include "rl.h" #include "GameData.h" #include "strings.h" #include "GameController.h" #include "backend/Exception.h" #include "backend/print.h" #include "backend/WeaponObject.h" #include "backend/Dungeon.h" using namespace std; GameController::GameController() { // Ensure DB is accessible GameData::get_instance(); } void GameController::cmdset_default() { this->cmds.clear(); this->cmds["Kijk"] = &GameController::cmd_query; this->cmds["Zoek"] = &GameController::cmd_search; this->cmds["Ga"] = &GameController::cmd_go; this->cmds["Pak"] = &GameController::cmd_get; this->cmds["Leg"] = &GameController::cmd_put; this->cmds["Bekijk"] = &GameController::cmd_view; this->cmds["Sla"] = &GameController::cmd_hit; this->cmds["Draag"] = &GameController::cmd_equip; this->cmds["Wacht"] = &GameController::cmd_wait; this->cmds["Consumeer"] = &GameController::cmd_use; this->cmds["Help"] = &GameController::cmd_help; this->cmds["Godmode"] = &GameController::cmd_cheat; this->cmds["Quit"] = &GameController::cmd_quit; } void GameController::cmdset_death() { this->cmds.clear(); this->cmds["Help"] = &GameController::cmd_help; this->cmds["Quit"] = &GameController::cmd_quit; this->cmds["Opnieuw"] = &GameController::cmd_restart; } void GameController::cmd(string & argv) { if (argv.size() == 0) return; string cmd = str_title(str_consume_arg(argv)); if (this->cmds.contains(cmd)) return (this->*cmds.at(cmd))(argv); str_print(strings::UNKNOWN_CMD); } unique_ptr GameController::make_dungeon() noexcept { while (1) { str_print(strings::INTRO); string filename = rl(); try { if (filename.size() == 0) { return generate_dungeon(); } else { return load_dungeon(filename); } } catch (Exception & e) { lprtf("FOUT: %s\n", e.what()); } } } void GameController::gameloop() { this->dungeon = make_dungeon(); Player & player = this->dungeon->get_player(); GameData & gamedata = GameData::get_instance(); lprtf("Wat is de naam van je karakter?\n"); player.name = rl().c_str(); player.set_location(dungeon->get_start_location()); player.equip(static_cast(gamedata.create_object("Dolk"))); cmdset_default(); bool is_dead = false, last_is_dead = false; this->playing = true; while (this->playing) { string line = rl(); if (line.length() == 0) continue; try { this->cmd(line); } catch (Exception & e) { lprtf("FOUT: %s.\n", e.what()); } last_is_dead = is_dead; is_dead = player.is_dead(); bool just_died = last_is_dead == false && is_dead == true; if (!just_died) continue; cmdset_death(); gamedata.leaderbord_add(player.name.c_str(), player.gold); str_print(strings::DEATH); gamedata.leaderbord_print(); } } int GameController::main() { while (!this->quit) { this->gameloop(); } return EXIT_SUCCESS; }