From a7e84b60366c78b131d43157980fbe4c2df655e6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 1 Nov 2024 16:03:53 +0100 Subject: small cleanup + checks --- backend/ArmorObject.cpp | 4 +++- backend/ArmorObject.h | 1 + backend/Dungeon.cpp | 2 ++ backend/Enemy.cpp | 14 ++++++++++++++ backend/Enemy.h | 12 +++++++++++- backend/Player.cpp | 2 ++ 6 files changed, 33 insertions(+), 2 deletions(-) (limited to 'backend') diff --git a/backend/ArmorObject.cpp b/backend/ArmorObject.cpp index 5b921fe..e6dc026 100644 --- a/backend/ArmorObject.cpp +++ b/backend/ArmorObject.cpp @@ -3,4 +3,6 @@ void ArmorObject::set_protection(int protection) { this->protection = protection; } - +int ArmorObject::get_protection() const { + return this->protection; +} diff --git a/backend/ArmorObject.h b/backend/ArmorObject.h index a476740..458b0ba 100644 --- a/backend/ArmorObject.h +++ b/backend/ArmorObject.h @@ -7,6 +7,7 @@ class ArmorObject : public Object { public: void set_protection(int protection); + int get_protection() const; private: int protection = 0; diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index ad721b4..84f9c79 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -20,6 +20,7 @@ void Dungeon::update_attacks(ListRange & enemies) { lprtf(":: De vijand%s in je locatie vallen aan! ::\n", enemies.size() == 1 ? "" : "en"); RNG & rng = RNG::get(); for (Enemy * enemy : enemies) { + if (enemy->is_dead()) continue; if (rng.rand_double() < enemy->get_attack()) continue; unsigned damage = rng.rand_int(enemy->get_damage_min(), enemy->get_damage_max() + 1); @@ -32,6 +33,7 @@ void Dungeon::update_movement() { bool moved = false; for (Location * location : this->locations) { for (Enemy * enemy : location->get_enemies()) { + if (enemy->is_dead()) continue; if (RNG::get().rand_double() < 0.5) continue; if (!moved) diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp index 9eadf01..c4bfff2 100644 --- a/backend/Enemy.cpp +++ b/backend/Enemy.cpp @@ -18,11 +18,25 @@ int Enemy::get_damage_min() const { return this->damage_min; } void Enemy::set_damage_max(int damage_max) { this->damage_max = damage_max; } int Enemy::get_damage_max() const { return this->damage_max; } +void Enemy::set_health(unsigned health_points) { this->health_points = health_points; } 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; } +bool Enemy::is_dead() const { + return this->health_points == 0; +} + +void Enemy::add_hidden_object(Object * object) { + this->hidden_objects.push_back(object); +} +void Enemy::remove_hidden_object(Object * object) { + this->hidden_objects.remove(object); +} +ListRange Enemy::get_hidden_objects() { + return this->hidden_objects.range(); +} const String & Enemy::get_displayname() const { static String displayname; diff --git a/backend/Enemy.h b/backend/Enemy.h index 71ad437..4db2ae8 100644 --- a/backend/Enemy.h +++ b/backend/Enemy.h @@ -1,6 +1,9 @@ #pragma once -#include "backend/String.h" +#include "String.h" +#include "PtrList.h" +#include "ListIterator.h" +#include "Object.h" class Enemy { public: @@ -8,7 +11,9 @@ public: const String & get_name() const; void set_description(const String & description); const String & get_description() const; + void set_health(unsigned); unsigned get_health() const; + bool is_dead() const; void take_damage(unsigned int dmg); virtual const String & get_displayname() const; void set_attack(float attack_chance); @@ -18,6 +23,10 @@ public: void set_damage_max(int damage_max); int get_damage_max() const; + void add_hidden_object(Object *); + void remove_hidden_object(Object *); + ListRange get_hidden_objects(); + private: friend class EnemyFactory; Enemy(const String & name, const String & description); @@ -27,6 +36,7 @@ public: private: String name; String description; + PtrList hidden_objects; unsigned int health_points = 0; float attack_chance = 0.0; int damage_min = 0; diff --git a/backend/Player.cpp b/backend/Player.cpp index a113a21..4180458 100644 --- a/backend/Player.cpp +++ b/backend/Player.cpp @@ -20,6 +20,8 @@ void Player::take_damage(unsigned int dmg) { if (this->cheating) return; dmg = min(dmg, this->health_points); + if (this->armor != nullptr) + dmg = max(0u, dmg - this->armor->get_protection()); this->health_points -= dmg; lprtf("Je hebt nog %d levenspunten over.\n", this->health_points); -- cgit v1.2.3