- Moved calc_dmg to characters.h

- put vampire into new source files
This commit is contained in:
a25liang
2024-07-02 20:39:48 -04:00
parent eea910ae33
commit 234f06191a
7 changed files with 74 additions and 15 deletions

View File

@ -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);
}

View File

@ -9,11 +9,14 @@
#include <vector>
#include <string>
#include <memory>
#include <math.h>
#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

View File

@ -6,30 +6,32 @@
#include <vector>
#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

View File

@ -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;
}

View File

@ -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

31
src/vampire.cc Normal file
View File

@ -0,0 +1,31 @@
#include "vampire.h"
#include <algorithm>
#include <math.h>
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;
}

17
src/vampire.h Normal file
View File

@ -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