diff options
Diffstat (limited to 'backend')
-rw-r--r-- | backend/CMakeLists.txt | 1 | ||||
-rw-r--r-- | backend/Dungeon.cpp | 31 | ||||
-rw-r--r-- | backend/Dungeon.h | 12 | ||||
-rw-r--r-- | backend/Enemy.cpp | 23 | ||||
-rw-r--r-- | backend/Enemy.h | 9 | ||||
-rw-r--r-- | backend/Player.cpp | 59 | ||||
-rw-r--r-- | backend/Player.h | 40 | ||||
-rw-r--r-- | backend/WeaponObject.h | 4 | ||||
-rw-r--r-- | backend/util.h | 8 | ||||
-rw-r--r-- | backend/util.hpp | 14 |
10 files changed, 176 insertions, 25 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; +} + |