aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 10:18:22 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 10:18:22 +0100
commit798948dbe6f012e194f053c4e862cf697f30b793 (patch)
tree32c71420d1188f98cfb41b6f0d9536c5fa4bf5a7 /frontend/cmd
parentd7012045bb61f117fb7b9c51ddd03e4c54f25fe6 (diff)
more WIP (move some Player things to backend)
Diffstat (limited to 'frontend/cmd')
-rw-r--r--frontend/cmd/cheat.cpp8
-rw-r--r--frontend/cmd/equip.cpp61
-rw-r--r--frontend/cmd/get.cpp16
-rw-r--r--frontend/cmd/go.cpp8
-rw-r--r--frontend/cmd/help.cpp4
-rw-r--r--frontend/cmd/hit.cpp10
-rw-r--r--frontend/cmd/put.cpp23
-rw-r--r--frontend/cmd/query.cpp15
-rw-r--r--frontend/cmd/quit.cpp4
-rw-r--r--frontend/cmd/restart.cpp4
-rw-r--r--frontend/cmd/search.cpp10
-rw-r--r--frontend/cmd/use.cpp4
-rw-r--r--frontend/cmd/view.cpp30
-rw-r--r--frontend/cmd/wait.cpp6
14 files changed, 94 insertions, 109 deletions
diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp
index 7a020a3..ac392d6 100644
--- a/frontend/cmd/cheat.cpp
+++ b/frontend/cmd/cheat.cpp
@@ -1,12 +1,12 @@
#include "backend/print.h"
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_cheat(string &) {
- this->cheating = !this->cheating;
- lprtf("Cheats staan nu %s.\n", this->cheating ? "aan" : "uit");
+FollowupAction GameController::cmd_cheat(string &) {
+ this->player.cheating = !this->player.cheating;
+ lprtf("Cheats staan nu %s.\n", this->player.cheating ? "aan" : "uit");
return FollowupAction::NONE;
}
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;
}
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp
index 359a5c5..652e989 100644
--- a/frontend/cmd/get.cpp
+++ b/frontend/cmd/get.cpp
@@ -1,4 +1,6 @@
-#include "../Player.h"
+#include <memory>
+
+#include "../GameController.h"
#include "../strings.h"
#include "backend/print.h"
@@ -7,12 +9,14 @@
using namespace std;
-FollowupAction Player::cmd_get(string & target_name) {
+FollowupAction GameController::cmd_get(string & target_name) {
+ Location & location = this->player.get_location();
unique_ptr<Object> target = nullptr;
- for (Object * object : this->location.get_visible_objects()) {
+
+ for (Object * object : location.get_visible_objects()) {
if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
target = unique_ptr<Object>(object);
- this->location.remove_visible_object(object);
+ location.remove_visible_object(object);
break;
}
if (target == nullptr) {
@@ -24,14 +28,14 @@ FollowupAction Player::cmd_get(string & target_name) {
GoldObject * gold = dynamic_cast<GoldObject *>(target.get());
if (gold != nullptr) {
int count = gold->get_count();
- this->gold += count;
+ this->player.gold += count;
lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken");
return FollowupAction::NONE;
}
// other objects go in the inventory
lprtf("Je voegt %s toe aan je bezit.\n", target->get_name().c_str());
- this->inventory.push_back(std::move(target));
+ this->player.inventory.push_back(target.release());
return FollowupAction::NONE;
}
diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp
index 9565e99..6a5f3b2 100644
--- a/frontend/cmd/go.cpp
+++ b/frontend/cmd/go.cpp
@@ -1,7 +1,7 @@
#include "backend/Location.h"
#include "backend/print.h"
-#include "../Player.h"
+#include "../GameController.h"
#include "../strings.h"
using namespace std;
@@ -13,7 +13,7 @@ static const unordered_map<string, Direction> direction_map = {
{ "west", Direction::WEST },
};
-FollowupAction Player::cmd_go(string & argv) {
+FollowupAction GameController::cmd_go(string & argv) {
string direction_str = str_consume_arg(argv);
if (direction_str.size() == 0 || !direction_map.contains(direction_str)) {
lprtf("Fout, gebruik: Ga <noord|zuid|oost|west>\n");
@@ -21,13 +21,13 @@ FollowupAction Player::cmd_go(string & argv) {
}
Direction direction = direction_map.at(direction_str);
- Location * next_location = this->location.get_exit(direction);
+ Location * next_location = this->player.get_location().get_exit(direction);
if (next_location == nullptr) {
lprtf("Er is geen uitgang in deze richting!\n");
return FollowupAction::NONE;
}
- this->location = *next_location;
+ this->player.set_location(*next_location);
return FollowupAction::UPDATE;
}
diff --git a/frontend/cmd/help.cpp b/frontend/cmd/help.cpp
index b655a45..e4425be 100644
--- a/frontend/cmd/help.cpp
+++ b/frontend/cmd/help.cpp
@@ -1,10 +1,10 @@
#include "backend/print.h"
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_help(string &) {
+FollowupAction GameController::cmd_help(string &) {
lprtf("De beschikbare commando's zijn: ");
bool first = true;
for (auto & [ key, _ ] : this->cmds) {
diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp
index d3cb683..2f39e8a 100644
--- a/frontend/cmd/hit.cpp
+++ b/frontend/cmd/hit.cpp
@@ -3,20 +3,20 @@
#include "backend/RNG.h"
#include "backend/print.h"
-#include "../Player.h"
+#include "../GameController.h"
#include "../strings.h"
using namespace std;
-FollowupAction Player::cmd_hit(string & target_name) {
+FollowupAction GameController::cmd_hit(string & target_name) {
RNG & rng = RNG::get();
- for (Enemy * enemy : this->location.get_enemies()) {
+ for (Enemy * enemy : this->player.get_location().get_enemies()) {
if (str_lower(enemy->get_name().c_str()) != str_lower(target_name)) continue;
- if (rng.rand_double() > this->get_attack()) {
+ if (rng.rand_double() > this->player.get_attack()) {
lprtf("Je hebt gemist!\n");
} else {
- unsigned damage = rng.rand_int(this->weapon->get_damage_min(), this->weapon->get_damage_max() + 1);
+ unsigned damage = rng.rand_int(this->player.weapon->get_damage_min(), this->player.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);
}
diff --git a/frontend/cmd/put.cpp b/frontend/cmd/put.cpp
index 5460b02..dabac19 100644
--- a/frontend/cmd/put.cpp
+++ b/frontend/cmd/put.cpp
@@ -1,6 +1,4 @@
-#include <algorithm>
-
-#include "../Player.h"
+#include "../GameController.h"
#include "../strings.h"
#include "backend/print.h"
@@ -8,18 +6,17 @@
using namespace std;
-FollowupAction Player::cmd_put(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());
+FollowupAction GameController::cmd_put(string & target_name) {
+ Location & location = this->player.get_location();
+ for (Object * object : this->player.inventory) {
+ if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
+
+ lprtf("Je legt %s neer op de locatie %s.\n", object->get_displayname().c_str(), location.get_name().c_str());
+ this->player.inventory.remove(object);
+ location.add_visible_object(object);
return FollowupAction::NONE;
}
-
- lprtf("Je legt %s neer op de locatie %s.\n", (*el)->get_displayname().c_str(), this->location.get_name().c_str());
- this->location.add_visible_object((*el).release());
- this->inventory.erase(el);
+ lprtf("Object \"%s\" niet gevonden.\n", target_name.c_str());
return FollowupAction::NONE;
}
diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp
index 19f2584..f04e053 100644
--- a/frontend/cmd/query.cpp
+++ b/frontend/cmd/query.cpp
@@ -3,7 +3,7 @@
#include "backend/Enemy.h"
#include "backend/print.h"
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
@@ -14,14 +14,15 @@ static const unordered_map<Direction, string> direction_map = {
{ Direction::WEST, "West" },
};
-FollowupAction Player::cmd_query(string &) {
- lprtf("Je staat bij de locatie %s.\n", this->location.get_name().c_str());
- lprtf("%s\n", this->location.get_description().c_str());
+FollowupAction GameController::cmd_query(string &) {
+ Location & location = this->player.get_location();
+ lprtf("Je staat bij de locatie %s.\n", location.get_name().c_str());
+ lprtf("%s\n", location.get_description().c_str());
{
lprtf("Zichtbare objecten: ");
size_t objects = 0;
- for (Object * obj : this->location.get_visible_objects()) {
+ for (Object * obj : location.get_visible_objects()) {
if (objects > 0) lprtf(", ");
lprtf("%s", obj->get_displayname().c_str());
objects++;
@@ -35,7 +36,7 @@ FollowupAction Player::cmd_query(string &) {
lprtf("Uitgangen: ");
bool first = true;
for (Direction direction : DIRECTIONS) {
- if (this->location.get_exit(direction) == nullptr) continue;
+ if (location.get_exit(direction) == nullptr) continue;
if (!first) lprtf(", ");
lprtf("%s", direction_map.at(direction).c_str());
first = false;
@@ -46,7 +47,7 @@ FollowupAction Player::cmd_query(string &) {
{
lprtf("Vijanden: ");
size_t enemies = 0;
- for (Enemy * enemy : this->location.get_enemies()) {
+ for (Enemy * enemy : location.get_enemies()) {
if (enemies > 0) lprtf(", ");
lprtf("%s", enemy->get_name().c_str());
enemies++;
diff --git a/frontend/cmd/quit.cpp b/frontend/cmd/quit.cpp
index 8f97562..b3a6e80 100644
--- a/frontend/cmd/quit.cpp
+++ b/frontend/cmd/quit.cpp
@@ -1,8 +1,8 @@
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_quit(string &) {
+FollowupAction GameController::cmd_quit(string &) {
return FollowupAction::EXIT;
}
diff --git a/frontend/cmd/restart.cpp b/frontend/cmd/restart.cpp
index d9d68e0..0ecb747 100644
--- a/frontend/cmd/restart.cpp
+++ b/frontend/cmd/restart.cpp
@@ -1,8 +1,8 @@
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_restart(string &) {
+FollowupAction GameController::cmd_restart(string &) {
return FollowupAction::RESTART;
}
diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp
index f10709a..f7fb528 100644
--- a/frontend/cmd/search.cpp
+++ b/frontend/cmd/search.cpp
@@ -1,16 +1,18 @@
-#include "../Player.h"
+#include "../GameController.h"
#include "backend/print.h"
#include "backend/Location.h"
using namespace std;
-FollowupAction Player::cmd_search(string &) {
+FollowupAction GameController::cmd_search(string &) {
+ Location & location = this->player.get_location();
+
bool found = false;
- for (Object * object : this->location.get_hidden_objects()) {
+ for (Object * object : location.get_hidden_objects()) {
if (!found) lprtf("Je vindt:\n");
lprtf("- %s\n", object->get_displayname().c_str());
- this->location.unhide_object(object);
+ location.unhide_object(object);
found = true;
}
if (!found)
diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp
index a6e876a..d83f67c 100644
--- a/frontend/cmd/use.cpp
+++ b/frontend/cmd/use.cpp
@@ -1,8 +1,8 @@
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_use(string & argv) {
+FollowupAction GameController::cmd_use(string & argv) {
// TODO
return FollowupAction::NONE;
}
diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp
index 1152efe..69eb96e 100644
--- a/frontend/cmd/view.cpp
+++ b/frontend/cmd/view.cpp
@@ -1,4 +1,4 @@
-#include "../Player.h"
+#include "../GameController.h"
#include "../strings.h"
#include "backend/print.h"
@@ -6,33 +6,35 @@
using namespace std;
-FollowupAction Player::cmd_view(string & target) {
+FollowupAction GameController::cmd_view(string & target) {
if (target.size() == 0) {
lprtf("Fout, gebruik: Bekijk <Zelf|VIJHAND|OBJECT>\n");
return FollowupAction::NONE;
}
+ Location & location = this->player.get_location();
+
// view self
if (str_lower(target) == "zelf") {
- lprtf("Je hebt %d levenspunten.\n", this->get_health());
+ lprtf("Je hebt %d levenspunten.\n", this->player.get_health());
- lprtf("Je hebt een aanvalskans van %.0f%%.\n", this->get_attack() * 100);
+ lprtf("Je hebt een aanvalskans van %.0f%%.\n", this->player.get_attack() * 100);
- if (this->weapon == nullptr)
+ if (this->player.weapon == nullptr)
lprtf("Je hebt geen wapen vast.\n");
else
- lprtf("Je hebt het volgende wapen vast: %s.\n", this->weapon->get_displayname().c_str());
+ lprtf("Je hebt het volgende wapen vast: %s.\n", this->player.weapon->get_displayname().c_str());
- if (this->armor == nullptr)
+ if (this->player.armor == nullptr)
lprtf("Je draagt geen wapenrusting.\n");
else
- lprtf("Je draagt de volgende wapenrusting: %s.\n", this->armor->get_displayname().c_str());
+ lprtf("Je draagt de volgende wapenrusting: %s.\n", this->player.armor->get_displayname().c_str());
- lprtf("Je hebt %u goundstuk%s.\n", this->gold, this->gold == 1 ? "" : "ken");
+ lprtf("Je hebt %u goundstuk%s.\n", this->player.gold, this->player.gold == 1 ? "" : "ken");
- size_t items = this->inventory.size();
+ size_t items = this->player.inventory.size();
lprtf("Je hebt %d overige object%s%s\n", items, items == 1 ? "" : "en", items > 0 ? ":" : ".");
- for (auto & object : this->inventory) {
+ for (auto & object : this->player.inventory) {
lprtf("- %s\n", object->get_displayname().c_str());
}
@@ -40,7 +42,7 @@ FollowupAction Player::cmd_view(string & target) {
}
// try to find visible object in location
- for (Object * object : this->location.get_visible_objects()) {
+ for (Object * object : location.get_visible_objects()) {
if (str_lower(object->get_name().c_str()) != str_lower(target)) continue;
lprtf("%s\n", object->get_description().c_str());
@@ -48,7 +50,7 @@ FollowupAction Player::cmd_view(string & target) {
}
// try to find object in inventory
- for (auto & object : this->inventory) {
+ for (auto & object : this->player.inventory) {
if (str_lower(object->get_name().c_str()) != str_lower(target)) continue;
lprtf("%s\n", object->get_description().c_str());
@@ -56,7 +58,7 @@ FollowupAction Player::cmd_view(string & target) {
}
// try to find enemy by name
- for (Enemy * enemy : this->location.get_enemies()) {
+ for (Enemy * enemy : location.get_enemies()) {
if (str_lower(enemy->get_name().c_str()) != str_lower(target)) continue;
lprtf("%s\n", enemy->get_description().c_str());
diff --git a/frontend/cmd/wait.cpp b/frontend/cmd/wait.cpp
index 49cb7db..0911d72 100644
--- a/frontend/cmd/wait.cpp
+++ b/frontend/cmd/wait.cpp
@@ -1,8 +1,8 @@
-#include "../Player.h"
+#include "../GameController.h"
using namespace std;
-FollowupAction Player::cmd_wait(string &) {
- return FollowupAction::NONE;
+FollowupAction GameController::cmd_wait(string &) {
+ return FollowupAction::UPDATE;
}