diff options
Diffstat (limited to 'frontend')
-rw-r--r-- | frontend/GameData.cpp | 20 | ||||
-rw-r--r-- | frontend/cmd/go.cpp | 5 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 24 |
3 files changed, 27 insertions, 22 deletions
diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp index a716a57..30816c8 100644 --- a/frontend/GameData.cpp +++ b/frontend/GameData.cpp @@ -49,20 +49,11 @@ Enemy * GameData::create_enemy(const string & name) const { } } -static const unordered_map<string, ObjectType> type_map = { - { "teleportatiedrank", ObjectType::CONSUMABLE }, - { "ervaringsdrank", ObjectType::CONSUMABLE }, - { "levenselixer", ObjectType::CONSUMABLE }, - { "wapenrusting", ObjectType::ARMOR }, - { "wapen", ObjectType::WEAPON }, - { "goudstukken", ObjectType::GOLD }, -}; - Object * GameData::create_object(const string & name) const { static DBStatement query = this->db.prepare(R"( select - type, omschrijving, + type, minimumwaarde, maximumwaarde, bescherming @@ -76,14 +67,11 @@ Object * GameData::create_object(const string & name) const { try { auto row = query.row(); - string type = row.col<const char *>(0); - if (!type_map.contains(type)) throw std::exception(); return ObjectFactory::create_object({ .name = name.c_str(), - .description = row.col<const char *>(1, nullptr), - .type = type_map.at(type), - .min_value = row.col<int>(2), - .max_value = row.col<int>(3), + .description = row.col<const char *>(0, nullptr), + .type = row.col<const char *>(1, nullptr), + .value = { row.col<int>(2), row.col<int>(3) }, .protection = row.col<int>(4), }); } catch (...) { diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp index 75f6345..b8bb7e2 100644 --- a/frontend/cmd/go.cpp +++ b/frontend/cmd/go.cpp @@ -1,7 +1,6 @@ #include "backend/Location.h" #include "backend/Dungeon.h" #include "backend/Exception.h" -#include "backend/print.h" #include "../GameController.h" #include "../strings.h" @@ -30,9 +29,7 @@ void GameController::cmd_go(string & argv) { this->dungeon->update(); - if (!player.is_dead()) { + if (!player.is_dead()) player.set_location(*next_location); - lprtf("Je staat nu bij de locatie %s\n", player.get_location().get_name().c_str()); - } } diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index badab10..36824b5 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -1,8 +1,28 @@ +#include "backend/ConsumableObject.h" +#include "backend/Exception.h" +#include "backend/print.h" + #include "../GameController.h" +#include "../strings.h" using namespace std; -void GameController::cmd_use(string & argv) { - // TODO +void GameController::cmd_use(string & target_name) { + Player & player = this->dungeon->get_player(); + Location & location = player.get_location(); + for (Object * object : player.inventory) { + if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; + + auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(object)); + if (consumable == nullptr) + throw Exception("%s is niet consumeerbaar", object->get_name().c_str()); + + lprtf("Je drinkt %s.\n", object->get_displayname().c_str()); + player.inventory.remove(object); + consumable->consume(player); + return; + } + + throw Exception("object \"%s\" niet gevonden", target_name.c_str()); } |