aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/use.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 14:44:25 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 14:44:25 +0100
commit369e3d71aa79783d95166739cfa93a480defe6ea (patch)
treecc4e590da6d98fc891ce3488cc150ffa10173520 /frontend/cmd/use.cpp
parent815ec66a68c01dc4a8f0c5ec6c9193a71e7547e2 (diff)
more cleanup + add give command to test things
Diffstat (limited to 'frontend/cmd/use.cpp')
-rw-r--r--frontend/cmd/use.cpp18
1 files changed, 10 insertions, 8 deletions
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());
+ }
}