diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 14:44:25 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 14:44:25 +0100 |
commit | 369e3d71aa79783d95166739cfa93a480defe6ea (patch) | |
tree | cc4e590da6d98fc891ce3488cc150ffa10173520 /frontend/cmd | |
parent | 815ec66a68c01dc4a8f0c5ec6c9193a71e7547e2 (diff) |
more cleanup + add give command to test things
Diffstat (limited to 'frontend/cmd')
-rw-r--r-- | frontend/cmd/cheat.cpp | 23 | ||||
-rw-r--r-- | frontend/cmd/equip.cpp | 20 | ||||
-rw-r--r-- | frontend/cmd/get.cpp | 8 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 18 |
4 files changed, 47 insertions, 22 deletions
diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp index 8c0af8d..981bca1 100644 --- a/frontend/cmd/cheat.cpp +++ b/frontend/cmd/cheat.cpp @@ -1,13 +1,36 @@ +#include <memory> + #include "backend/print.h" #include "backend/Dungeon.h" #include "../GameController.h" +#include "../GameData.h" using namespace std; +constexpr const char * cmd_give_key = "Manifesteer"; +void GameController::cmd_give(string & item_name) { + Player & player = this->dungeon->get_player(); + GameData & gamedata = GameData::get_instance(); + auto object = unique_ptr<Object>(gamedata.create_object(item_name)); + lprtf("Object aangemaakt: %s\n", object->get_displayname().c_str()); + player.inventory.push_back(object.release()); +} + void GameController::cmd_cheat(string &) { Player & player = this->dungeon->get_player(); player.cheating = !player.cheating; lprtf("Cheats staan nu %s.\n", player.cheating ? "aan" : "uit"); + this->cmdset_cheats(); +} + +void GameController::cmdset_cheats() { + Player & player = this->dungeon->get_player(); + + if (player.cheating) { + this->cmds[cmd_give_key] = &GameController::cmd_give; + } else { + this->cmds.erase(cmd_give_key); + } } diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp index fe951b1..8f37c0c 100644 --- a/frontend/cmd/equip.cpp +++ b/frontend/cmd/equip.cpp @@ -16,21 +16,21 @@ void GameController::cmd_equip(string & target_name) { Object & object = **it; - WeaponObject * weapon = dynamic_cast<WeaponObject *>(&object); - if (weapon != nullptr) { - player.inventory.remove(weapon); - player.equip(weapon); + try { + WeaponObject & weapon = dynamic_cast<WeaponObject &>(object); + player.inventory.remove(&weapon); + player.equip(&weapon); this->dungeon->update(); return; - } + } catch (std::bad_cast &) {}; - ArmorObject * armor = dynamic_cast<ArmorObject *>(&object); - if (armor != nullptr) { - player.inventory.remove(armor); - player.equip(armor); + try { + ArmorObject & armor = dynamic_cast<ArmorObject &>(object); + player.inventory.remove(&armor); + player.equip(&armor); this->dungeon->update(); return; - } + } catch (std::bad_cast &) {}; throw Exception("object \"%s\" is niet draagbaar", target_name.c_str()); } diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index e774168..ea1f69b 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -22,13 +22,13 @@ void GameController::cmd_get(string & target_name) { location.remove_visible_object(object.get()); // gold objects are collected and (implicitly) destroyed - GoldObject * gold = dynamic_cast<GoldObject *>(object.get()); - if (gold != nullptr) { - int count = gold->get_count(); + try { + GoldObject & gold = dynamic_cast<GoldObject &>(*object); + int count = gold.get_count(); player.gold += count; lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken"); return; - } + } catch (std::bad_cast &) {}; // other objects go in the inventory lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str()); 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()); + } } |