From 234f06191afa6db32529692d07aafb1d11101847 Mon Sep 17 00:00:00 2001 From: a25liang Date: Tue, 2 Jul 2024 20:39:48 -0400 Subject: [PATCH] - Moved calc_dmg to characters.h - put vampire into new source files --- src/characters.cc | 6 ++++++ src/characters.h | 6 ++++++ src/constants.h | 20 +++++++++++--------- src/races.cc | 5 +---- src/races.h | 4 ++-- src/vampire.cc | 31 +++++++++++++++++++++++++++++++ src/vampire.h | 17 +++++++++++++++++ 7 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/vampire.cc create mode 100644 src/vampire.h diff --git a/src/characters.cc b/src/characters.cc index 358531f..34b1592 100644 --- a/src/characters.cc +++ b/src/characters.cc @@ -174,3 +174,9 @@ result character::move_or_attack(const direction dir, return this->attack(dir,chlist); } + + +int calc_dmg(const int ATK, const int DEF) { + return ceil((100 / (100 + DEF)) * ATK); +} + diff --git a/src/characters.h b/src/characters.h index 9962f7e..88884e5 100644 --- a/src/characters.h +++ b/src/characters.h @@ -9,11 +9,14 @@ #include #include #include +#include + #include "constants.h" #include "position.h" #include "layer.h" #include "objects.h" #include "rng.h" + // #include "inventory.h" // Reserved for later class character; // forward declaration @@ -96,4 +99,7 @@ protected: position_list remove_from_list(const position_list &sorted_positions, position_list excluded); + +int calc_dmg(const int ATK, const int DEF); + #endif diff --git a/src/constants.h b/src/constants.h index 5ec97ec..1279f36 100644 --- a/src/constants.h +++ b/src/constants.h @@ -6,30 +6,32 @@ #include #include "position.h" -// IMPORTANT: added END to the end of all valued enums so that you can -// iterate over them +const int INF = 0x3F3F3F3F; enum error {none}; // TODO: update result to include subject -enum result {fine, died, go_down, hit, moved}; +enum result {fine, died, go_down, hit, moved, miss}; enum stat_name {HP, ATK, DEF, hostile}; const int LAYER_CNT = 4; // TODO: update as you go enum layer_num {map = 0, objects, characters, shop}; -const int RACE_CNT = 2; // TODO: update as you go +const int RACE_CNT = 3; // TODO: update as you go -enum race {unknown = 0, rshade /* TODO: fill out the other races (including enemies) */}; +enum race {unknown = 0, rshade , rvampire /* TODO: fill out the other races (including enemies) */}; // TODO: fill out the other races (including enemies) -const int MAX_HP[RACE_CNT] = {0, 125}; -const int STARTING_HP[RACE_CNT] = {0, 125}; -const int STARTING_ATK[RACE_CNT] = {0, 25}; -const int STARTING_DEF[RACE_CNT] = {0, 25}; +const int MAX_HP[RACE_CNT] = {0, 125, INF}; +const int STARTING_HP[RACE_CNT] = {0, 125, 50}; +const int STARTING_ATK[RACE_CNT] = {0, 25, 25}; +const int STARTING_DEF[RACE_CNT] = {0, 25, 25}; const char CHARACTER_REP[RACE_CNT] = {'@', 'S'}; + + + const int DIRECTION_CNT = 8; // IMPORTANT: east is positive for x and SOUTH is positive for y // initializes all directions to an int diff --git a/src/races.cc b/src/races.cc index 9594b3d..1724a08 100644 --- a/src/races.cc +++ b/src/races.cc @@ -21,10 +21,6 @@ result shade::attack(const direction dir, const character_list &chlist) { return result::fine; } -int calc_dmg(const int ATK, const int DEF) { - return ceil((100 / (100 + DEF)) * ATK); -} - result shade::get_hit(const enum race &race, const int atk, const float hitrate) { if (rng.rand_num() <= hitrate * (float)RAND_MAX) // This is a hit! @@ -35,3 +31,4 @@ result shade::get_hit(const enum race &race, const int atk, return result::hit; } + diff --git a/src/races.h b/src/races.h index 0351a9e..f493169 100644 --- a/src/races.h +++ b/src/races.h @@ -16,10 +16,10 @@ public: virtual result attack(const direction dir, const character_list &chlist) override; virtual result get_hit(const enum race &race, const int atk, - const float hitrate) override; + const float hit_rate) override; }; -// TODO: fill out the other races + // TODO: implement enemie movement #endif diff --git a/src/vampire.cc b/src/vampire.cc new file mode 100644 index 0000000..12cdb1d --- /dev/null +++ b/src/vampire.cc @@ -0,0 +1,31 @@ +#include "vampire.h" + +#include +#include + + +result vampire::attack(const direction dir, const character_list &chlist) { + position tmp{pos + MOVE[dir]}; + + for (auto &ch : chlist) + if (tmp == ch->get_position()) { + auto res = ch->get_hit(race, ATK, base_hitrate); + if (res != result::miss) { + HP += GAIN_HP; + } + return res; + } + + return result::fine; +} + +result vampire::get_hit(const enum race &race, const int atk, + const float hitrate) { + if (rng.rand_num() <= hitrate * (float)RAND_MAX) + HP = std::max(HP - calc_dmg(atk, DEF), 0); + + if (HP == 0) + return result::died; + + return result::hit; +} diff --git a/src/vampire.h b/src/vampire.h new file mode 100644 index 0000000..3954f95 --- /dev/null +++ b/src/vampire.h @@ -0,0 +1,17 @@ +#ifndef __VAMPIRE_H__ +#define __VAMPIRE_H__ +#include "characters.h" + + +class vampire final: public character { +public: + vampire(const position_list &available_positions); // spawn at a random place + virtual result attack(const direction dir, + const character_list &chlist) override; + virtual result get_hit(const enum race &race, const int atk, + const float hit_rate) override; +private: + const int GAIN_HP = 5; +}; + +#endif