diff options
Diffstat (limited to 'frontend/GameController.cpp')
-rw-r--r-- | frontend/GameController.cpp | 68 |
1 files changed, 58 insertions, 10 deletions
diff --git a/frontend/GameController.cpp b/frontend/GameController.cpp index a75767c..bbdd0c1 100644 --- a/frontend/GameController.cpp +++ b/frontend/GameController.cpp @@ -1,7 +1,10 @@ +#include "generate_dungeon.h" +#include "load_dungeon.h" #include "rl.h" #include "GameData.h" #include "strings.h" #include "GameController.h" +#include "Exception.h" #include "backend/print.h" #include "backend/WeaponObject.h" @@ -9,16 +12,13 @@ using namespace std; -GameController::GameController(Dungeon & dungeon) : dungeon(dungeon), player(dungeon.get_player()) { - lprtf("Wat is de naam van je karakter?\n"); - string name = rl(); - player.name = name.c_str(); - cmdset_default(); - player.set_location(dungeon.get_start_location()); - player.equip(static_cast<WeaponObject *>(GameData::get_instance().create_object("Dolk"))); +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; @@ -35,20 +35,68 @@ void GameController::cmdset_default() { } 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; } -FollowupAction GameController::cmd(string & argv) { - if (argv.size() == 0) return FollowupAction::NONE; +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<Dungeon> 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(); + + 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<WeaponObject *>(GameData::get_instance().create_object("Dolk"))); + + cmdset_default(); + + 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()); + } + + if (player.is_dead()) + cmdset_death(); + } +} - return FollowupAction::NONE; +int GameController::main() { + while (!this->quit) { + this->gameloop(); + } + return EXIT_SUCCESS; } |