From 0312986dcea94b6d4978a1ecc102bbc1d67342f3 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Thu, 11 Jul 2024 21:32:23 -0400 Subject: [PATCH] added potion and restore_health --- src/characters.cc | 25 +------------------------ src/characters.h | 9 ++++++--- src/constants.h | 23 +++++++++++++++++------ src/potion.cc | 12 ++++++++++++ src/potion.h | 30 ++++++++++++++++++++++++++++++ src/potions/restore_health.cc | 22 ++++++++++++++++++++++ src/potions/restore_health.h | 14 ++++++++++++++ 7 files changed, 102 insertions(+), 33 deletions(-) create mode 100644 src/potion.cc create mode 100644 src/potion.h create mode 100644 src/potions/restore_health.cc create mode 100644 src/potions/restore_health.h diff --git a/src/characters.cc b/src/characters.cc index 6178766..3150e0e 100644 --- a/src/characters.cc +++ b/src/characters.cc @@ -94,29 +94,6 @@ void character::apply_buff(const stat_name statn, const int amount) { } } -character_list::character_list(): - layer{layer_num::characters} {} - -void character_list::print() const { - // TODO: implement it using ncurses -} - -void character_list::print(display &display) const { - for (auto &ch : characters) - display.print_char(ch->get_position(), - CHARACTER_REP[ch->get_race()]); -} - -std::vector>::const_iterator character_list::begin() -const { - return characters.begin(); -} - -std::vector>::const_iterator character_list::end() -const { - return characters.end(); -} - direction_list character::moveable(const position_list &available_positions) const { direction_list result; @@ -166,7 +143,7 @@ result character::move(const direction dir, result character::move_or_attack(const direction dir, const position_list &available_positions, - const character_list &chlist) { + character_list &chlist) { auto res = this->move(dir, available_positions); if (res != result::fine) diff --git a/src/characters.h b/src/characters.h index a6aaf55..941de50 100644 --- a/src/characters.h +++ b/src/characters.h @@ -14,7 +14,7 @@ #include "constants.h" #include "position.h" #include "layer.h" -#include "objects.h" +#include "potion.h" #include "rng.h" // #include "inventory.h" // Reserved for later @@ -41,11 +41,12 @@ public: character_list &chlist) = 0; virtual result move_or_attack(const direction dir, const position_list &available_positions, - const character_list &chlist); + character_list &chlist); virtual result apply(direction &dir, const potion_list &potions); virtual result get_hit(const enum race &race, const int atk, const float hitrate) = 0; + enum race get_race() const; position get_position() const; int get_HP() const; @@ -72,13 +73,15 @@ protected: const enum race race; int HP; + + // IMPORTANT: keep track of ATK and DEF in game at turn time int ATK; int DEF; position pos; int gold; // characters spawn with gold - // inventory inventory; // Reserved + potion_list potions;// inventory inventory; // Reserved float base_hit_rate; // requires: between [0,1] diff --git a/src/constants.h b/src/constants.h index 4a594cb..b697615 100644 --- a/src/constants.h +++ b/src/constants.h @@ -36,14 +36,25 @@ enum layer_num {map = 0, objects, characters, shop}; const int RACE_CNT = 4; // TODO: update as you go -enum race {unknown = 0, rshade, rvampire, goblin /* TODO: fill out the other races (including enemies) */}; +enum race {rshade = 0, rvampire, rgoblin, rdrow /* TODO: fill out the other races (including enemies) */}; // TODO: fill out the other races (including enemies) -const int MAX_HP[RACE_CNT] = {0, 125, INF, 110}; -const int STARTING_HP[RACE_CNT] = {0, 125, 50, 110}; -const int STARTING_ATK[RACE_CNT] = {0, 25, 25, 15}; -const int STARTING_DEF[RACE_CNT] = {0, 25, 25, 20}; -const char CHARACTER_REP[RACE_CNT] = {'@', 'S', 'V', 'G'}; +const int MAX_HP[RACE_CNT] = {125, INF, 110, 150}; +const int STARTING_HP[RACE_CNT] = {125, 50, 110, 150}; +const int STARTING_ATK[RACE_CNT] = {25, 25, 15, 25}; +const int STARTING_DEF[RACE_CNT] = {25, 25, 20, 15}; +const char CHARACTER_REP[RACE_CNT] = {'S', 'V', 'G', 'D'}; + +enum potion_type {restore_health = 0, boost_atk, boost_def, + poison_health, wound_atk, wound_def + }; + +// Calculation priorities +const int CALC_ADD_BASE = 0; +const int CALC_MUL_BASE = 1; +const int CALC_ADD_LATER = 2; +const int CALC_MUL_LATER = 3; +const int CALC_ADD_FIXED = 4; diff --git a/src/potion.cc b/src/potion.cc new file mode 100644 index 0000000..2f9fab6 --- /dev/null +++ b/src/potion.cc @@ -0,0 +1,12 @@ +#include "potion.h" + +potion::potion(const potion_type type, const int duration): + type{type}, remaining_duration{duration} {} + +potion_type potion::get_type() const { + return type; +} + +int potion::get_duration() const { + return remaining_duration; +} diff --git a/src/potion.h b/src/potion.h new file mode 100644 index 0000000..00396bb --- /dev/null +++ b/src/potion.h @@ -0,0 +1,30 @@ +#ifndef __POTIONS_H__ +#define __POTIONS_H__ + +#include +#include "constants.h" + +// IMPORTANT: pop all potions of duration == 0, and when entering a +// new level, pop all potions of duration == -1 + +class potion { +protected: + // Use -1 to denote that the effect will last until leaving the level + // Otherwise, use a positive number + // Single use potions have a starting duration of 1 + const potion_type type; + int remaining_duration; +public: + potion(const potion_type type, const int duration); + // apply decrements remaining_duration if it's positive, and + // won't do anything if it's non-negative + virtual void apply(enum race &race, int &HP, int &ATK, int &DEF, + float &base_hit_rate) = 0; + virtual int get_priority() const = 0; + potion_type get_type() const; + int get_duration() const; +}; + +typedef std::vector potion_list; + +#endif diff --git a/src/potions/restore_health.cc b/src/potions/restore_health.cc new file mode 100644 index 0000000..8cb646b --- /dev/null +++ b/src/potions/restore_health.cc @@ -0,0 +1,22 @@ +#include "restore_health.h" + +#include + +restore_health::restore_health(): + potion{potion_type::restore_health, -1} {} + +void restore_health::apply(enum race &race, int &HP, int &ATK, int &DEF, + float &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + HP = std::max(HP + 7, MAX_HP[race]); + else + HP = std::max(HP + 5, MAX_HP[race]); + + --remaining_duration; + } +} + +int restore_health::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/potions/restore_health.h b/src/potions/restore_health.h new file mode 100644 index 0000000..37095da --- /dev/null +++ b/src/potions/restore_health.h @@ -0,0 +1,14 @@ +#ifndef __RESTORE_HEALTH_H__ +#define __RESTORE_HEALTH_H__ + +#include "../potion.h" + +class restore_health final: public potion { +public: + restore_health(); + void apply(enum race &race, int &HP, int &ATK, int &DEF, + float &base_hit_rate) override; + int get_priority() const override; +}; + +#endif