diff options
Diffstat (limited to 'frontend')
-rw-r--r-- | frontend/GameController.h | 3 | ||||
-rw-r--r-- | frontend/cmd/cheat.cpp | 23 | ||||
-rw-r--r-- | frontend/cmd/equip.cpp | 20 | ||||
-rw-r--r-- | frontend/cmd/get.cpp | 8 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 18 |
5 files changed, 50 insertions, 22 deletions
diff --git a/frontend/GameController.h b/frontend/GameController.h index 9f0d6ee..ca19972 100644 --- a/frontend/GameController.h +++ b/frontend/GameController.h @@ -30,6 +30,7 @@ private: private: void cmdset_default(); void cmdset_death(); + void cmdset_cheats(); private: std::unordered_map<std::string, void(GameController::*)(std::string &)> cmds; @@ -48,6 +49,8 @@ private: Cmd cmd_quit; Cmd cmd_restart; + Cmd cmd_give; + private: std::unique_ptr<Dungeon> dungeon = nullptr; bool playing = true; diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp index 8c0af8d..981bca1 100644 --- a/frontend/cmd/cheat.cpp +++ b/frontend/cmd/cheat.cpp @@ -1,13 +1,36 @@ +#include <memory> + #include "backend/print.h" #include "backend/Dungeon.h" #include "../GameController.h" +#include "../GameData.h" using namespace std; +constexpr const char * cmd_give_key = "Manifesteer"; +void GameController::cmd_give(string & item_name) { + Player & player = this->dungeon->get_player(); + GameData & gamedata = GameData::get_instance(); + auto object = unique_ptr<Object>(gamedata.create_object(item_name)); + lprtf("Object aangemaakt: %s\n", object->get_displayname().c_str()); + player.inventory.push_back(object.release()); +} + void GameController::cmd_cheat(string &) { Player & player = this->dungeon->get_player(); player.cheating = !player.cheating; lprtf("Cheats staan nu %s.\n", player.cheating ? "aan" : "uit"); + this->cmdset_cheats(); +} + +void GameController::cmdset_cheats() { + Player & player = this->dungeon->get_player(); + + if (player.cheating) { + this->cmds[cmd_give_key] = &GameController::cmd_give; + } else { + this->cmds.erase(cmd_give_key); + } } diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp index fe951b1..8f37c0c 100644 --- a/frontend/cmd/equip.cpp +++ b/frontend/cmd/equip.cpp @@ -16,21 +16,21 @@ void GameController::cmd_equip(string & target_name) { Object & object = **it; - WeaponObject * weapon = dynamic_cast<WeaponObject *>(&object); - if (weapon != nullptr) { - player.inventory.remove(weapon); - player.equip(weapon); + try { + WeaponObject & weapon = dynamic_cast<WeaponObject &>(object); + player.inventory.remove(&weapon); + player.equip(&weapon); this->dungeon->update(); return; - } + } catch (std::bad_cast &) {}; - ArmorObject * armor = dynamic_cast<ArmorObject *>(&object); - if (armor != nullptr) { - player.inventory.remove(armor); - player.equip(armor); + try { + ArmorObject & armor = dynamic_cast<ArmorObject &>(object); + player.inventory.remove(&armor); + player.equip(&armor); this->dungeon->update(); return; - } + } catch (std::bad_cast &) {}; throw Exception("object \"%s\" is niet draagbaar", target_name.c_str()); } diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index e774168..ea1f69b 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -22,13 +22,13 @@ void GameController::cmd_get(string & target_name) { location.remove_visible_object(object.get()); // gold objects are collected and (implicitly) destroyed - GoldObject * gold = dynamic_cast<GoldObject *>(object.get()); - if (gold != nullptr) { - int count = gold->get_count(); + try { + GoldObject & gold = dynamic_cast<GoldObject &>(*object); + int count = gold.get_count(); player.gold += count; lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken"); return; - } + } catch (std::bad_cast &) {}; // other objects go in the inventory lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str()); diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index 3134210..78d8233 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -1,4 +1,4 @@ -#include <algorithm> +#include <memory> #include "backend/ConsumableObject.h" #include "backend/Exception.h" @@ -14,16 +14,18 @@ void GameController::cmd_use(string & target_name) { Location & location = player.get_location(); auto it = find_if_range(player.inventory.range(), by_name_case_insensitive(target_name)); - if (!it) throw Exception("object \"%s\" niet gevonden", target_name.c_str()); + Object & object = **it; - auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(*it)); - if (consumable == nullptr) - throw Exception("%s is niet consumeerbaar", (*it)->get_name().c_str()); + try { + auto consumable = unique_ptr<ConsumableObject>(&dynamic_cast<ConsumableObject &>(object)); + player.inventory.remove(consumable.get()); - lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str()); - player.inventory.remove(consumable.get()); - consumable->consume(player); + lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str()); + consumable->consume(player); + } catch (std::bad_cast &) { + throw Exception("%s is niet consumeerbaar", object.get_name().c_str()); + } } |