diff options
Diffstat (limited to 'frontend/cmd/equip.cpp')
-rw-r--r-- | frontend/cmd/equip.cpp | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp index 4c5fd86..25a375f 100644 --- a/frontend/cmd/equip.cpp +++ b/frontend/cmd/equip.cpp @@ -1,54 +1,33 @@ -#include <memory> -#include <algorithm> - #include "backend/print.h" +#include "backend/ListIterator.h" -#include "../Player.h" +#include "../GameController.h" #include "../strings.h" using namespace std; -FollowupAction Player::cmd_equip(string & target_name) { - auto el = find_if(this->inventory.begin(), this->inventory.end(), [target_name](const auto & object) -> bool { - return str_lower(object->get_name().c_str()) == str_lower(target_name); - }); - if (el == this->inventory.end()) { - lprtf("Object \"%s\" niet gevonden.\n", target_name.c_str()); - return FollowupAction::NONE; - } - - unique_ptr<WeaponObject> weapon {dynamic_cast<WeaponObject *>((*el).get())}; - unique_ptr<ArmorObject> armor {dynamic_cast<ArmorObject *>((*el).get())}; +FollowupAction GameController::cmd_equip(string & target_name) { + for (Object * object : this->player.inventory) { + if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; - if (weapon == nullptr && armor == nullptr) { - lprtf("Object %s is niet draagbaar.\n", target_name.c_str()); - return FollowupAction::NONE; - } - - // remove from inventory w/o free()'ing data - (*el).release(); - this->inventory.erase(el); - - if (weapon != nullptr) { - lprtf("Je "); - if (this->weapon != nullptr) { - lprtf("laat %s vallen en ", this->weapon->get_name().c_str()); - this->inventory.push_back(std::move(this->weapon)); + WeaponObject * weapon = dynamic_cast<WeaponObject *>(object); + if (weapon != nullptr) { + this->player.inventory.remove(weapon); + this->player.equip(weapon); + return FollowupAction::UPDATE; } - this->weapon = std::move(weapon); - lprtf("pakt %s vast.\n", this->weapon->get_name().c_str()); - } - if (armor != nullptr) { - lprtf("Je "); - if (this->armor != nullptr) { - lprtf("trekt %s uit en ", this->armor->get_name().c_str()); - this->inventory.push_back(std::move(this->armor)); + ArmorObject * armor = dynamic_cast<ArmorObject *>(object); + if (armor != nullptr) { + this->player.inventory.remove(armor); + this->player.equip(armor); + return FollowupAction::UPDATE; } - this->armor = std::move(armor); - lprtf("doet %s aan.\n", this->armor->get_name().c_str()); + + lprtf("Object %s is niet draagbaar.\n", target_name.c_str()); + return FollowupAction::NONE; } - - return FollowupAction::UPDATE; + lprtf("Object \"%s\" niet gevonden.\n", target_name.c_str()); + return FollowupAction::NONE; } |