From 07796bea15a2d5f43766f062379b63fc9e9e1b5d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 1 Nov 2024 22:47:42 +0100 Subject: WIP consumables --- frontend/cmd/go.cpp | 5 +---- frontend/cmd/use.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'frontend/cmd') 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(dynamic_cast(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()); } -- cgit v1.2.3