diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 13:18:50 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 13:18:50 +0100 |
commit | 5a675c5e6833e98b92b55396594bc0d607b98903 (patch) | |
tree | b252fafe921e7734a13d796b950e5ab817a22a1f /frontend/cmd/view.cpp | |
parent | 671a25463419691bc6c8d11bd57d932bdea9632b (diff) |
clean up code + implement teleport consumable
Diffstat (limited to 'frontend/cmd/view.cpp')
-rw-r--r-- | frontend/cmd/view.cpp | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp index 91eaf94..bd87227 100644 --- a/frontend/cmd/view.cpp +++ b/frontend/cmd/view.cpp @@ -1,5 +1,6 @@ #include "../GameController.h" #include "../strings.h" +#include "../util.h" #include "backend/Exception.h" #include "backend/print.h" @@ -9,9 +10,8 @@ using namespace std; void GameController::cmd_view(string & target) { - if (target.size() == 0) { + if (target.size() == 0) throw Exception("gebruik: Bekijk <Zelf|VIJHAND|OBJECT>"); - } Player & player = this->dungeon->get_player(); Location & location = player.get_location(); @@ -43,38 +43,31 @@ void GameController::cmd_view(string & target) { return; } - // try to find visible object in location - for (Object * object : location.get_visible_objects()) { - if (str_lower(object->get_name().c_str()) != str_lower(target)) continue; - - lprtf("%s\n", object->get_description().c_str()); - return; - } - - // try to find object in inventory - for (auto & object : player.inventory) { - if (str_lower(object->get_name().c_str()) != str_lower(target)) continue; - - lprtf("%s\n", object->get_description().c_str()); + // try to find visible object in location or inventory + auto object_it = find_if_range(location.get_visible_objects(), by_name_case_insensitive(target)); + if (!object_it) object_it = find_if_range(player.inventory.range(), by_name_case_insensitive(target)); + if (!!object_it) { + Object & object = **object_it; + lprtf("%s\n", object.get_description().c_str()); return; } // try to find enemy by name - for (Enemy * enemy : location.get_enemies()) { - if (str_lower(enemy->get_name().c_str()) != str_lower(target)) continue; - - lprtf("%s\n", enemy->get_description().c_str()); - unsigned enemy_health = enemy->get_health(); - lprtf("%s heeft %u levenspunten.\n", enemy->get_name().c_str(), enemy_health); - - if (!enemy->is_dead()) + auto enemy_it = find_if_range(location.get_enemies(), by_name_case_insensitive(target)); + if (!!enemy_it) { + Enemy & enemy = **enemy_it; + lprtf("%s\n", enemy.get_description().c_str()); + unsigned enemy_health = enemy.get_health(); + lprtf("%s heeft %u levenspunten.\n", enemy.get_name().c_str(), enemy_health); + + if (!enemy.is_dead()) return; bool snatched = false; - for (Object * object : enemy->get_hidden_objects()) { - if (!snatched) lprtf("%s had de volgende voorwerpen:\n", enemy->get_displayname().c_str()); + for (Object * object : enemy.get_hidden_objects()) { + if (!snatched) lprtf("%s had de volgende voorwerpen:\n", enemy.get_displayname().c_str()); lprtf("- %s\n", object->get_displayname().c_str()); - enemy->remove_hidden_object(object); + enemy.remove_hidden_object(object); location.add_visible_object(object); snatched = true; } |