From 5a675c5e6833e98b92b55396594bc0d607b98903 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 2 Nov 2024 13:18:50 +0100 Subject: clean up code + implement teleport consumable --- frontend/cmd/equip.cpp | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'frontend/cmd/equip.cpp') diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp index 9d2a006..fe951b1 100644 --- a/frontend/cmd/equip.cpp +++ b/frontend/cmd/equip.cpp @@ -3,33 +3,35 @@ #include "backend/Exception.h" #include "../GameController.h" -#include "../strings.h" +#include "../util.h" using namespace std; void GameController::cmd_equip(string & target_name) { Player & player = this->dungeon->get_player(); - for (Object * object : player.inventory) { - if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; - - WeaponObject * weapon = dynamic_cast(object); - if (weapon != nullptr) { - player.inventory.remove(weapon); - player.equip(weapon); - this->dungeon->update(); - return; - } - - ArmorObject * armor = dynamic_cast(object); - if (armor != nullptr) { - player.inventory.remove(armor); - player.equip(armor); - this->dungeon->update(); - return; - } - - throw Exception("object \"%s\" is niet draagbaar", target_name.c_str()); + + 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; + + WeaponObject * weapon = dynamic_cast(&object); + if (weapon != nullptr) { + player.inventory.remove(weapon); + player.equip(weapon); + this->dungeon->update(); + return; } - throw Exception("object \"%s\" niet gevonden", target_name.c_str()); + + ArmorObject * armor = dynamic_cast(&object); + if (armor != nullptr) { + player.inventory.remove(armor); + player.equip(armor); + this->dungeon->update(); + return; + } + + throw Exception("object \"%s\" is niet draagbaar", target_name.c_str()); } -- cgit v1.2.3