aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/equip.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 13:18:50 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 13:18:50 +0100
commit5a675c5e6833e98b92b55396594bc0d607b98903 (patch)
treeb252fafe921e7734a13d796b950e5ab817a22a1f /frontend/cmd/equip.cpp
parent671a25463419691bc6c8d11bd57d932bdea9632b (diff)
clean up code + implement teleport consumable
Diffstat (limited to 'frontend/cmd/equip.cpp')
-rw-r--r--frontend/cmd/equip.cpp46
1 files changed, 24 insertions, 22 deletions
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<WeaponObject *>(object);
- if (weapon != nullptr) {
- player.inventory.remove(weapon);
- player.equip(weapon);
- this->dungeon->update();
- return;
- }
-
- ArmorObject * armor = dynamic_cast<ArmorObject *>(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<WeaponObject *>(&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<ArmorObject *>(&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());
}