diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 21:40:55 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 21:40:55 +0100 |
commit | d7012045bb61f117fb7b9c51ddd03e4c54f25fe6 (patch) | |
tree | 28cadae7a828865aa4bd13f62fb83f81c600b063 | |
parent | 82dcf9e2dd3596b28ef846f4a217dc19c21cf781 (diff) |
more more more more WIP
-rw-r--r-- | backend/Dungeon.cpp | 2 | ||||
-rw-r--r-- | backend/Enemy.cpp | 15 | ||||
-rw-r--r-- | backend/Enemy.h | 2 | ||||
-rw-r--r-- | backend/String.cpp | 5 | ||||
-rw-r--r-- | backend/String.h | 3 | ||||
-rw-r--r-- | backend/WeaponObject.cpp | 10 | ||||
-rw-r--r-- | backend/WeaponObject.h | 2 | ||||
-rw-r--r-- | frontend/Player.cpp | 3 | ||||
-rw-r--r-- | frontend/cmd/equip.cpp | 49 | ||||
-rw-r--r-- | frontend/cmd/hit.cpp | 26 |
10 files changed, 105 insertions, 12 deletions
diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index b1e89ba..30d6068 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -16,7 +16,7 @@ void Dungeon::update(Location * player_location) { } void Dungeon::update_attacks(ListRange<Enemy *> & enemies) { - printf("TODO: de vijanden vallen aan!\n"); + lprtf(":: De vijand%s in je locatie vallen aan! ::\n", enemies.size() == 1 ? "" : "en"); } void Dungeon::update_movement() { diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp index 9d3b26d..15e66ed 100644 --- a/backend/Enemy.cpp +++ b/backend/Enemy.cpp @@ -10,3 +10,18 @@ const String & Enemy::get_description() const { return this->description; } unsigned Enemy::get_health() const { return this->health_points; } +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; +} + +static inline unsigned min(unsigned a, unsigned b) { + return a < b ? a : b; +} + +void Enemy::take_damage(unsigned int dmg) { + dmg = min(dmg, this->health_points); + this->health_points -= dmg; +} + diff --git a/backend/Enemy.h b/backend/Enemy.h index ea08864..23d988d 100644 --- a/backend/Enemy.h +++ b/backend/Enemy.h @@ -9,6 +9,8 @@ public: void set_description(const String & description); const String & get_description() const; unsigned get_health() const; + void take_damage(unsigned int dmg); + virtual const String & get_displayname() const; private: friend class EnemyFactory; diff --git a/backend/String.cpp b/backend/String.cpp index a8b648a..2f998be 100644 --- a/backend/String.cpp +++ b/backend/String.cpp @@ -10,6 +10,11 @@ String::String() { this->set(""); } + +String & String::operator = (const String & other) { + this->set(other.data(), other.size()); + return *this; +} String::String(const String & other) { this->set(other.data(), other.size()); } diff --git a/backend/String.h b/backend/String.h index e41222e..cf4304f 100644 --- a/backend/String.h +++ b/backend/String.h @@ -19,6 +19,9 @@ public: size_t size() const; bool empty() const; +public: + String & operator = (const String &); + private: void set(const char * data); void set(const char * data, size_t data_len); diff --git a/backend/WeaponObject.cpp b/backend/WeaponObject.cpp index a92f9c8..58db34d 100644 --- a/backend/WeaponObject.cpp +++ b/backend/WeaponObject.cpp @@ -1,12 +1,10 @@ #include "WeaponObject.h" -void WeaponObject::set_damage_min(int damage_min) { - this->damage_min = damage_min; -} +void WeaponObject::set_damage_min(int damage_min) { this->damage_min = damage_min; } +int WeaponObject::get_damage_min() const { return this->damage_min; } -void WeaponObject::set_damage_max(int damage_max) { - this->damage_max = damage_max; -} +void WeaponObject::set_damage_max(int damage_max) { this->damage_max = damage_max; } +int WeaponObject::get_damage_max() const { return this->damage_max; } const String & WeaponObject::get_displayname() const { static String display = String::fmt("%s (%d - %d schade)", this->get_name().c_str(), this->damage_min, this->damage_max); diff --git a/backend/WeaponObject.h b/backend/WeaponObject.h index e1347ec..5ac4f87 100644 --- a/backend/WeaponObject.h +++ b/backend/WeaponObject.h @@ -7,7 +7,9 @@ class WeaponObject : public Object { public: void set_damage_min(int damage_min); + int get_damage_min() const; void set_damage_max(int damage_max); + int get_damage_max() const; virtual const String & get_displayname() const; private: diff --git a/frontend/Player.cpp b/frontend/Player.cpp index 85c4fff..fdd2dc3 100644 --- a/frontend/Player.cpp +++ b/frontend/Player.cpp @@ -1,6 +1,8 @@ +#include "GameData.h" #include "strings.h" #include "Player.h" +#include "backend/WeaponObject.h" #include "backend/Dungeon.h" using namespace std; @@ -10,6 +12,7 @@ Player::Player(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() { 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; } |