aboutsummaryrefslogtreecommitdiff
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
parentd7012045bb61f117fb7b9c51ddd03e4c54f25fe6 (diff)
more WIP (move some Player things to backend)
-rw-r--r--backend/CMakeLists.txt1
-rw-r--r--backend/Dungeon.cpp31
-rw-r--r--backend/Dungeon.h12
-rw-r--r--backend/Enemy.cpp23
-rw-r--r--backend/Enemy.h9
-rw-r--r--backend/Player.cpp59
-rw-r--r--backend/Player.h40
-rw-r--r--backend/WeaponObject.h4
-rw-r--r--backend/util.h8
-rw-r--r--backend/util.hpp14
-rw-r--r--frontend/CMakeLists.txt2
-rw-r--r--frontend/GameController.cpp49
-rw-r--r--frontend/GameController.h53
-rw-r--r--frontend/Player.cpp75
-rw-r--r--frontend/Player.h72
-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
-rw-r--r--frontend/main.cpp8
30 files changed, 377 insertions, 286 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 1f872e4..e0c377b 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -14,5 +14,6 @@ target_sources(main PUBLIC
Enemy.cpp
String.cpp
print.cpp
+ Player.cpp
)
diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp
index 30d6068..ad721b4 100644
--- a/backend/Dungeon.cpp
+++ b/backend/Dungeon.cpp
@@ -1,14 +1,15 @@
+#include <assert.h>
+
#include "Location.h"
#include "Dungeon.h"
#include "RNG.h"
-#include "backend/ListIterator.h"
+#include "ListIterator.h"
#include "print.h"
-void Dungeon::update(Location * player_location) {
- this->player_location = player_location;
-
- ListRange<Enemy *> enemies = player_location->get_enemies();
+Dungeon::Dungeon() : player(*this) { }
+void Dungeon::update() {
+ ListRange<Enemy *> enemies = this->player.get_location().get_enemies();
if (enemies.size() > 0)
this->update_attacks(enemies);
else
@@ -17,6 +18,14 @@ void Dungeon::update(Location * player_location) {
void Dungeon::update_attacks(ListRange<Enemy *> & enemies) {
lprtf(":: De vijand%s in je locatie vallen aan! ::\n", enemies.size() == 1 ? "" : "en");
+ RNG & rng = RNG::get();
+ for (Enemy * enemy : enemies) {
+ if (rng.rand_double() < enemy->get_attack()) continue;
+
+ unsigned damage = rng.rand_int(enemy->get_damage_min(), enemy->get_damage_max() + 1);
+ this->player.take_damage(damage);
+ lprtf("%s raakt en doet %d punt schade.\n", enemy->get_displayname().c_str(), damage);
+ }
}
void Dungeon::update_movement() {
@@ -31,7 +40,7 @@ void Dungeon::update_movement() {
location->remove_enemy(enemy);
Location * new_location = location->get_exit(direction);
new_location->add_enemy(enemy);
- if (player_location == new_location)
+ if (&this->player.get_location() == new_location)
lprtf("%s komt de huidige locatie binnen\n", enemy->get_name().c_str());
moved = true;
}
@@ -42,10 +51,14 @@ void Dungeon::add_location(Location * location) {
this->locations.push_back(location);
}
-Location * Dungeon::get_start_location() {
+Location & Dungeon::get_start_location() {
size_t size = this->locations.size();
- if (size == 0) return nullptr;
+ assert(size > 0);
size_t index = RNG::get().rand_int(size);
- return this->locations[index];
+ return *this->locations[index];
+}
+
+Player & Dungeon::get_player() {
+ return this->player;
}
diff --git a/backend/Dungeon.h b/backend/Dungeon.h
index 1eaa970..0ab6c36 100644
--- a/backend/Dungeon.h
+++ b/backend/Dungeon.h
@@ -2,21 +2,23 @@
#include "Location.h"
#include "PtrList.h"
-#include "backend/ListIterator.h"
+#include "ListIterator.h"
+#include "Player.h"
class Dungeon {
public:
- Dungeon() = default;
+ Dungeon();
virtual ~Dungeon() = default;
public:
- void update(Location * player_location);
+ void update();
void add_location(Location *);
- Location * get_start_location();
+ Location & get_start_location();
+ Player & get_player();
private:
PtrList<Location> locations;
- Location * player_location = nullptr;
+ Player player;
private:
void update_attacks(ListRange<Enemy *> & enemies);
diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp
index 15e66ed..9eadf01 100644
--- a/backend/Enemy.cpp
+++ b/backend/Enemy.cpp
@@ -1,4 +1,5 @@
#include "Enemy.h"
+#include "util.h"
Enemy::Enemy(const String & name, const String & description) : name(name), description(description) { }
@@ -8,20 +9,24 @@ const String & Enemy::get_name() const { return this->name; }
void Enemy::set_description(const String & description) { this->description = description; }
const String & Enemy::get_description() const { return this->description; }
-unsigned Enemy::get_health() const { return this->health_points; }
+void Enemy::set_attack(float attack_chance) { this->attack_chance = attack_chance; }
+float Enemy::get_attack() const { return this->attack_chance; }
-const String & Enemy::get_displayname() const {
- static String displayname;
- displayname = String::fmt("%s%s", this->name.c_str(), this->health_points == 0 ? " [verslagen]" : "");
- return displayname;
-}
+void Enemy::set_damage_min(int damage_min) { this->damage_min = damage_min; }
+int Enemy::get_damage_min() const { return this->damage_min; }
-static inline unsigned min(unsigned a, unsigned b) {
- return a < b ? a : b;
-}
+void Enemy::set_damage_max(int damage_max) { this->damage_max = damage_max; }
+int Enemy::get_damage_max() const { return this->damage_max; }
+unsigned Enemy::get_health() const { return this->health_points; }
void Enemy::take_damage(unsigned int dmg) {
dmg = min(dmg, this->health_points);
this->health_points -= dmg;
}
+const String & Enemy::get_displayname() const {
+ static String displayname;
+ displayname = String::fmt("%s%s", this->name.c_str(), this->health_points == 0 ? " [verslagen]" : "");
+ return displayname;
+}
+
diff --git a/backend/Enemy.h b/backend/Enemy.h
index 23d988d..71ad437 100644
--- a/backend/Enemy.h
+++ b/backend/Enemy.h
@@ -11,6 +11,12 @@ public:
unsigned get_health() const;
void take_damage(unsigned int dmg);
virtual const String & get_displayname() const;
+ void set_attack(float attack_chance);
+ float get_attack() const;
+ void set_damage_min(int damage_min);
+ int get_damage_min() const;
+ void set_damage_max(int damage_max);
+ int get_damage_max() const;
private:
friend class EnemyFactory;
@@ -22,5 +28,8 @@ private:
String name;
String description;
unsigned int health_points = 0;
+ float attack_chance = 0.0;
+ int damage_min = 0;
+ int damage_max = 0;
};
diff --git a/backend/Player.cpp b/backend/Player.cpp
new file mode 100644
index 0000000..a113a21
--- /dev/null
+++ b/backend/Player.cpp
@@ -0,0 +1,59 @@
+#include <assert.h>
+
+#include "Player.h"
+#include "print.h"
+#include "Dungeon.h"
+#include "util.h"
+
+Player::Player(Dungeon & dungeon) : dungeon(dungeon) { }
+
+float Player::get_attack() const {
+ if (this->cheating) return 1.f;
+ return this->attack_chance;
+}
+
+unsigned Player::get_health() const {
+ return this->health_points;
+}
+
+void Player::take_damage(unsigned int dmg) {
+ if (this->cheating) return;
+
+ dmg = min(dmg, this->health_points);
+ this->health_points -= dmg;
+
+ lprtf("Je hebt nog %d levenspunten over.\n", this->health_points);
+}
+
+Location & Player::get_location() const {
+ assert(this->location != nullptr);
+ return *this->location;
+}
+void Player::set_location(Location & location) {
+ this->location = &location;
+}
+
+void Player::equip(WeaponObject * weapon) {
+ if (weapon == nullptr) return;
+
+ lprtf("Je ");
+ if (this->weapon != nullptr) {
+ lprtf("laat %s vallen en ", this->weapon->get_name().c_str());
+ this->inventory.push_back(this->weapon);
+ }
+ this->weapon = weapon;
+ lprtf("pakt %s vast.\n", this->weapon->get_name().c_str());
+}
+
+void Player::equip(ArmorObject * armor) {
+ if (armor == nullptr) return;
+
+ lprtf("Je ");
+ if (this->armor != nullptr) {
+ lprtf("trekt %s uit en ", this->armor->get_name().c_str());
+ this->inventory.push_back(this->armor);
+ }
+ this->armor = armor;
+ lprtf("doet %s aan.\n", this->armor->get_name().c_str());
+}
+
diff --git a/backend/Player.h b/backend/Player.h
new file mode 100644
index 0000000..b101de2
--- /dev/null
+++ b/backend/Player.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include "WeaponObject.h"
+#include "ArmorObject.h"
+#include "PtrList.h"
+
+class Dungeon;
+class Location;
+
+class Player {
+public:
+ String name;
+ unsigned int health_points = 20;
+ float attack_chance = 0.4;
+ unsigned int gold = 0;
+ WeaponObject * weapon = nullptr;
+ ArmorObject * armor = nullptr;
+ bool cheating = false;
+ PtrList<Object> inventory;
+
+private:
+ Location * location = nullptr;
+
+public:
+ Player(Dungeon & dungeon);
+ virtual ~Player() = default;
+
+public:
+ void take_damage(unsigned int dmg);
+ float get_attack() const;
+ unsigned get_health() const;
+ Location & get_location() const;
+ void set_location(Location &);
+ void equip(WeaponObject *);
+ void equip(ArmorObject *);
+
+private:
+ Dungeon & dungeon;
+};
+
diff --git a/backend/WeaponObject.h b/backend/WeaponObject.h
index 5ac4f87..032ac15 100644
--- a/backend/WeaponObject.h
+++ b/backend/WeaponObject.h
@@ -13,8 +13,8 @@ public:
virtual const String & get_displayname() const;
private:
- int damage_min;
- int damage_max;
+ int damage_min = 0;
+ int damage_max = 0;
};
diff --git a/backend/util.h b/backend/util.h
index 83cae58..15dca58 100644
--- a/backend/util.h
+++ b/backend/util.h
@@ -7,3 +7,11 @@ void safe_free(char * & ptr);
void safe_free(const char * & ptr);
void safe_free(FILE * & ptr);
+template <typename T>
+inline T min(const T &, const T &);
+
+template <typename T>
+inline T max(const T &, const T &);
+
+#include "util.hpp"
+
diff --git a/backend/util.hpp b/backend/util.hpp
new file mode 100644
index 0000000..58b7977
--- /dev/null
+++ b/backend/util.hpp
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "util.h"
+
+template <typename T>
+inline T min(const T & a, const T & b) {
+ return a < b ? a : b;
+}
+
+template <typename T>
+inline T max(const T & a, const T & b) {
+ return a < b ? b : a;
+}
+
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
index 92d03bc..4248eb5 100644
--- a/frontend/CMakeLists.txt
+++ b/frontend/CMakeLists.txt
@@ -2,7 +2,7 @@ target_sources(main PUBLIC
main.cpp
rl.cpp
strings.cpp
- Player.cpp
+ GameController.cpp
load_dungeon.cpp
generate_dungeon.cpp
Exception.cpp
diff --git a/frontend/GameController.cpp b/frontend/GameController.cpp
new file mode 100644
index 0000000..5505380
--- /dev/null
+++ b/frontend/GameController.cpp
@@ -0,0 +1,49 @@
+#include "GameData.h"
+#include "strings.h"
+#include "GameController.h"
+
+#include "backend/WeaponObject.h"
+#include "backend/Dungeon.h"
+
+using namespace std;
+
+GameController::GameController(Dungeon & dungeon) : dungeon(dungeon), player(dungeon.get_player()) {
+ cmdset_default();
+ player.set_location(dungeon.get_start_location());
+ player.equip(static_cast<WeaponObject *>(GameData::get_instance().create_object("Dolk")));
+}
+
+void GameController::cmdset_default() {
+ this->cmds["Kijk"] = &GameController::cmd_query;
+ this->cmds["Zoek"] = &GameController::cmd_search;
+ this->cmds["Ga"] = &GameController::cmd_go;
+ this->cmds["Pak"] = &GameController::cmd_get;
+ this->cmds["Leg"] = &GameController::cmd_put;
+ this->cmds["Bekijk"] = &GameController::cmd_view;
+ this->cmds["Sla"] = &GameController::cmd_hit;
+ this->cmds["Draag"] = &GameController::cmd_equip;
+ this->cmds["Wacht"] = &GameController::cmd_wait;
+ this->cmds["Consumeer"] = &GameController::cmd_use;
+ this->cmds["Help"] = &GameController::cmd_help;
+ this->cmds["Godmode"] = &GameController::cmd_cheat;
+ this->cmds["Quit"] = &GameController::cmd_quit;
+}
+
+void GameController::cmdset_death() {
+ this->cmds["Help"] = &GameController::cmd_help;
+ this->cmds["Quit"] = &GameController::cmd_quit;
+ this->cmds["Opnieuw"] = &GameController::cmd_restart;
+}
+
+FollowupAction GameController::cmd(string & argv) {
+ if (argv.size() == 0) return FollowupAction::NONE;
+ string cmd = str_title(str_consume_arg(argv));
+
+ if (this->cmds.contains(cmd))
+ return (this->*cmds.at(cmd))(argv);
+
+ str_print(strings::UNKNOWN_CMD);
+
+ return FollowupAction::NONE;
+}
+
diff --git a/frontend/GameController.h b/frontend/GameController.h
new file mode 100644
index 0000000..afcb5b8
--- /dev/null
+++ b/frontend/GameController.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <unordered_map>
+#include <string>
+
+#include "backend/Player.h"
+
+class Dungeon;
+class Location;
+
+enum FollowupAction {
+ NONE,
+ UPDATE,
+ EXIT,
+ RESTART,
+};
+
+class GameController {
+ typedef FollowupAction Cmd(std::string &);
+
+public:
+ GameController(Dungeon & dungeon);
+ virtual ~GameController() = default;
+
+public:
+ FollowupAction cmd(std::string &);
+
+private:
+ void cmdset_default();
+ void cmdset_death();
+
+private:
+ std::unordered_map<std::string, FollowupAction(GameController::*)(std::string &)> cmds;
+ Cmd cmd_query;
+ Cmd cmd_search;
+ Cmd cmd_go;
+ Cmd cmd_get;
+ Cmd cmd_put;
+ Cmd cmd_view;
+ Cmd cmd_hit;
+ Cmd cmd_equip;
+ Cmd cmd_wait;
+ Cmd cmd_use;
+ Cmd cmd_help;
+ Cmd cmd_cheat;
+ Cmd cmd_quit;
+ Cmd cmd_restart;
+
+private:
+ Player & player;
+ Dungeon & dungeon;
+};
+
diff --git a/frontend/Player.cpp b/frontend/Player.cpp
deleted file mode 100644
index fdd2dc3..0000000
--- a/frontend/Player.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-#include "GameData.h"
-#include "strings.h"
-#include "Player.h"
-
-#include "backend/WeaponObject.h"
-#include "backend/Dungeon.h"
-
-using namespace std;
-
-Player::Player(Dungeon & dungeon) :
- dungeon(dungeon),
- location(*dungeon.get_start_location())
- {
- cmdset_default();
- this->weapon = unique_ptr<WeaponObject>(static_cast<WeaponObject *>(GameData::get_instance().create_object("Dolk")));
-}
-
-void Player::cmdset_default() {
- this->cmds["Kijk"] = &Player::cmd_query;
- this->cmds["Zoek"] = &Player::cmd_search;
- this->cmds["Ga"] = &Player::cmd_go;
- this->cmds["Pak"] = &Player::cmd_get;
- this->cmds["Leg"] = &Player::cmd_put;
- this->cmds["Bekijk"] = &Player::cmd_view;
- this->cmds["Sla"] = &Player::cmd_hit;
- this->cmds["Draag"] = &Player::cmd_equip;
- this->cmds["Wacht"] = &Player::cmd_wait;
- this->cmds["Consumeer"] = &Player::cmd_use;
- this->cmds["Help"] = &Player::cmd_help;
- this->cmds["Godmode"] = &Player::cmd_cheat;
- this->cmds["Quit"] = &Player::cmd_quit;
-}
-
-void Player::cmdset_death() {
- this->cmds["Help"] = &Player::cmd_help;
- this->cmds["Quit"] = &Player::cmd_quit;
- this->cmds["Opnieuw"] = &Player::cmd_restart;
-}
-
-FollowupAction Player::cmd(string & argv) {
- if (argv.size() == 0) return FollowupAction::NONE;
- string cmd = str_title(str_consume_arg(argv));
-
- if (this->cmds.contains(cmd))
- return (this->*cmds.at(cmd))(argv);
-
- str_print(strings::UNKNOWN_CMD);
-
- return FollowupAction::NONE;
-}
-
-float Player::get_attack() const {
- if (this->cheating) return 1.f;
- return this->attack_chance;
-}
-
-unsigned Player::get_health() const {
- return this->health_points;
-}
-
-void Player::take_damage(unsigned int dmg) {
- if (this->cheating) return;
-
- dmg = min(dmg, this->health_points);
- this->health_points -= dmg;
-
- if (this->health_points == 0) {
- this->cmdset_death();
- }
-}
-
-Location & Player::get_location() const {
- return this->location;
-}
-
diff --git a/frontend/Player.h b/frontend/Player.h
deleted file mode 100644
index 249a129..0000000
--- a/frontend/Player.h
+++ /dev/null
@@ -1,72 +0,0 @@
-#pragma once
-
-#include <unordered_map>
-#include <string>
-#include <memory>
-#include <vector>
-
-#include "backend/WeaponObject.h"
-#include "backend/ArmorObject.h"
-
-class Dungeon;
-class Location;
-
-enum FollowupAction {
- NONE,
- UPDATE,
- EXIT,
- RESTART,
-};
-
-class Player {
- typedef FollowupAction Cmd(std::string &);
-
-private:
- std::string name;
- unsigned int health_points = 20;
- float attack_chance = 0.4;
- unsigned int gold = 0;
- std::unique_ptr<WeaponObject> weapon = nullptr;
- std::unique_ptr<ArmorObject> armor = nullptr;
- Location & location;
- bool cheating = false;
- std::vector<std::unique_ptr<Object>> inventory = {};
-
-public:
- Player(Dungeon & dungeon);
- virtual ~Player() = default;
-
-public:
- FollowupAction cmd(std::string &);
-
-public:
- void take_damage(unsigned int dmg);
- float get_attack() const;
- unsigned get_health() const;
- Location & get_location() const;
-
-private:
- void cmdset_default();
- void cmdset_death();
-
-private:
- std::unordered_map<std::string, FollowupAction(Player::*)(std::string &)> cmds;
- Cmd cmd_query;
- Cmd cmd_search;
- Cmd cmd_go;
- Cmd cmd_get;
- Cmd cmd_put;
- Cmd cmd_view;
- Cmd cmd_hit;
- Cmd cmd_equip;
- Cmd cmd_wait;
- Cmd cmd_use;
- Cmd cmd_help;
- Cmd cmd_cheat;
- Cmd cmd_quit;
- Cmd cmd_restart;
-
-private:
- Dungeon & dungeon;
-};
-
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;
}
diff --git a/frontend/main.cpp b/frontend/main.cpp
index 780a649..f8e2526 100644
--- a/frontend/main.cpp
+++ b/frontend/main.cpp
@@ -6,7 +6,7 @@
#include "backend/Dungeon.h"
#include "GameData.h"
-#include "Player.h"
+#include "GameController.h"
#include "Exception.h"
#include "load_dungeon.h"
#include "generate_dungeon.h"
@@ -33,17 +33,17 @@ static unique_ptr<Dungeon> make_dungeon() noexcept {
FollowupAction game_main() {
unique_ptr<Dungeon> dungeon = make_dungeon();
- Player player { *dungeon };
+ GameController game { *dungeon };
while (1) {
string line = rl();
if (line.length() == 0) continue;
- FollowupAction action = player.cmd(line);
+ FollowupAction action = game.cmd(line);
switch (action) {
case NONE: break;
case UPDATE: {
- dungeon->update(&player.get_location());
+ dungeon->update();
break;
}
default: return action;