aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 21:40:55 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 21:40:55 +0100
commitd7012045bb61f117fb7b9c51ddd03e4c54f25fe6 (patch)
tree28cadae7a828865aa4bd13f62fb83f81c600b063 /frontend/cmd
parent82dcf9e2dd3596b28ef846f4a217dc19c21cf781 (diff)
more more more more WIP
Diffstat (limited to 'frontend/cmd')
-rw-r--r--frontend/cmd/equip.cpp49
-rw-r--r--frontend/cmd/hit.cpp26
2 files changed, 70 insertions, 5 deletions
diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp
index 79ab8d1..4c5fd86 100644
--- a/frontend/cmd/equip.cpp
+++ b/frontend/cmd/equip.cpp
@@ -1,9 +1,54 @@
+#include <memory>
+#include <algorithm>
+
+#include "backend/print.h"
+
#include "../Player.h"
+#include "../strings.h"
using namespace std;
-FollowupAction Player::cmd_equip(string & argv) {
- // TODO
+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())};
+
+ 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));
+ }
+ 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));
+ }
+ this->armor = std::move(armor);
+ lprtf("doet %s aan.\n", this->armor->get_name().c_str());
+ }
+
return FollowupAction::UPDATE;
}
diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp
index 86d596a..d3cb683 100644
--- a/frontend/cmd/hit.cpp
+++ b/frontend/cmd/hit.cpp
@@ -1,9 +1,29 @@
+#include "backend/Enemy.h"
+#include "backend/Location.h"
+#include "backend/RNG.h"
+#include "backend/print.h"
+
#include "../Player.h"
+#include "../strings.h"
using namespace std;
-FollowupAction Player::cmd_hit(string & argv) {
- // TODO
- return FollowupAction::UPDATE;
+FollowupAction Player::cmd_hit(string & target_name) {
+ RNG & rng = RNG::get();
+ for (Enemy * enemy : this->location.get_enemies()) {
+ if (str_lower(enemy->get_name().c_str()) != str_lower(target_name)) continue;
+
+ if (rng.rand_double() > this->get_attack()) {
+ lprtf("Je hebt gemist!\n");
+ } else {
+ unsigned damage = rng.rand_int(this->weapon->get_damage_min(), this->weapon->get_damage_max() + 1);
+ enemy->take_damage(damage);
+ lprtf("Je hebt %s geraakt en %d schade aangericht!\n", enemy->get_displayname().c_str(), damage);
+ }
+ return FollowupAction::UPDATE;
+ }
+
+ lprtf("Vijand \"%s\" niet gevonden.\n", target_name.c_str());
+ return FollowupAction::NONE;
}