- impl goblin character class

This commit is contained in:
a25liang
2024-07-02 21:58:56 -04:00
parent bd372f7a47
commit 9086f0773a
7 changed files with 59 additions and 14 deletions

View File

@ -33,7 +33,7 @@ int character::get_gold() const {
} }
float character::get_hitrate() const { float character::get_hitrate() const {
return base_hitrate; return base_hit_rate;
} }
bool character::is_hostile() const { bool character::is_hostile() const {
@ -61,7 +61,7 @@ void character::set_gold(const int ngold) {
} }
void character::set_hitrate(const float nhitrate) { void character::set_hitrate(const float nhitrate) {
base_hitrate = nhitrate; base_hit_rate = nhitrate;
} }
void character::set_hostile(const bool is_hostile) { void character::set_hostile(const bool is_hostile) {

View File

@ -90,7 +90,7 @@ protected:
int gold; // characters spawn with gold int gold; // characters spawn with gold
// inventory inventory; // Reserved // inventory inventory; // Reserved
float base_hitrate; // requires: between [0,1] float base_hit_rate; // requires: between [0,1]
bool hostile; bool hostile;
}; };

View File

@ -8,7 +8,7 @@
const int INF = 0x3F3F3F3F; const int INF = 0x3F3F3F3F;
enum error {none}; enum error {none};
// TODO: update result to include subject // TODO: update result to include subject
enum result {fine, died, go_down, hit, moved, miss}; enum result {fine, died, go_down, hit, moved, miss};
@ -18,16 +18,16 @@ enum stat_name {HP, ATK, DEF, hostile};
const int LAYER_CNT = 4; // TODO: update as you go const int LAYER_CNT = 4; // TODO: update as you go
enum layer_num {map = 0, objects, characters, shop}; enum layer_num {map = 0, objects, characters, shop};
const int RACE_CNT = 3; // TODO: update as you go const int RACE_CNT = 4; // TODO: update as you go
enum race {unknown = 0, rshade , rvampire /* TODO: fill out the other races (including enemies) */}; enum race {unknown = 0, rshade , rvampire, goblin /* TODO: fill out the other races (including enemies) */};
// 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}; const int MAX_HP[RACE_CNT] = {0, 125, INF, 110};
const int STARTING_HP[RACE_CNT] = {0, 125, 50}; const int STARTING_HP[RACE_CNT] = {0, 125, 50, 110};
const int STARTING_ATK[RACE_CNT] = {0, 25, 25}; const int STARTING_ATK[RACE_CNT] = {0, 25, 25, 15};
const int STARTING_DEF[RACE_CNT] = {0, 25, 25}; const int STARTING_DEF[RACE_CNT] = {0, 25, 25, 20};
const char CHARACTER_REP[RACE_CNT] = {'@', 'S'}; const char CHARACTER_REP[RACE_CNT] = {'@', 'S', 'V', 'G'};

30
src/goblin.cc Normal file
View File

@ -0,0 +1,30 @@
#include "goblin.h"
#include <algorithm>
#include <math.h>
result goblin::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_hit_rate);
if (res == result::died) {
gold += GAIN_GOLD;
}
return res;
}
return result::fine;
}
result goblin::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;
}

16
src/goblin.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef __GOBLIN_H__
#define __GOBLIN_H__
#include "characters.h"
const int GAIN_GOLD = 5;
class goblin final: public character {
public:
goblin(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;
};
#endif

View File

@ -15,7 +15,7 @@ result shade::attack(const direction dir, const character_list &chlist) {
for (auto &ch : chlist) for (auto &ch : chlist)
if (tmp == ch->get_position()) { if (tmp == ch->get_position()) {
return ch->get_hit(race, ATK, base_hitrate); return ch->get_hit(race, ATK, base_hit_rate);
} }
return result::fine; return result::fine;

View File

@ -1,5 +1,4 @@
#include "vampire.h" #include "vampire.h"
#include <algorithm> #include <algorithm>
#include <math.h> #include <math.h>
@ -9,7 +8,7 @@ result vampire::attack(const direction dir, const character_list &chlist) {
for (auto &ch : chlist) for (auto &ch : chlist)
if (tmp == ch->get_position()) { if (tmp == ch->get_position()) {
auto res = ch->get_hit(race, ATK, base_hitrate); auto res = ch->get_hit(race, ATK, base_hit_rate);
if (res != result::miss) { if (res != result::miss) {
HP += GAIN_HP; HP += GAIN_HP;
} }