changed all non-owned unique_ptr access to reference to the object itself
This commit is contained in:
@ -121,18 +121,19 @@ feature proc_args(int argc, char **argv,
|
||||
|
||||
if (result & FEATURE_NCURSES) {
|
||||
curse = std::make_unique<cursor>();
|
||||
in = std::make_unique<curses_input>(curse);
|
||||
out = std::make_unique<curses_output>(curse);
|
||||
in = std::make_unique<curses_input>(*curse);
|
||||
out = std::make_unique<curses_output>(*curse);
|
||||
}
|
||||
|
||||
if (result & FEATURE_SEED) {
|
||||
std::istringstream iss {seed};
|
||||
unsigned int tmp;
|
||||
iss >> tmp;
|
||||
if (!iss.good())
|
||||
|
||||
if (!iss.good())
|
||||
return FEATURE_PANIC_SEED;
|
||||
|
||||
rng = std::make_unique<RNG>(tmp);
|
||||
rng = std::make_unique<RNG>(tmp);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -166,9 +167,8 @@ void panic_args(feature panic) {
|
||||
cerr << "Something must have went really, really, wrong..."
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
else if (panic & FEATURE_PANIC_SEED) {
|
||||
cerr << "Invalid seed" << endl;
|
||||
} else if (panic & FEATURE_PANIC_SEED) {
|
||||
cerr << "Invalid seed" << endl;
|
||||
} else
|
||||
cerr << "Something must have went really, really, wrong..."
|
||||
<< endl;
|
||||
|
@ -2,12 +2,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
character::character(const enum race &nrace):
|
||||
character::character(RNG &rng, const enum race &nrace):
|
||||
rng{rng},
|
||||
race{nrace}, HP{STARTING_HP[race]},
|
||||
ATK{STARTING_ATK[race]}, DEF{STARTING_DEF[race]} {}
|
||||
|
||||
character::~character() {}
|
||||
|
||||
enum race character::get_race() const {
|
||||
return race;
|
||||
}
|
||||
|
@ -19,16 +19,13 @@
|
||||
// #include "inventory.h" // Reserved for later
|
||||
|
||||
class character; // forward declaration
|
||||
extern RNG rng;
|
||||
|
||||
// Note: player should not be in the character list
|
||||
typedef std::vector<character> character_list;
|
||||
|
||||
class character {
|
||||
public:
|
||||
character(const race &nrace); // fills out the starting stats
|
||||
|
||||
~character(); // placeholder
|
||||
character(RNG &rng, const race &nrace); // fills out the starting stats
|
||||
|
||||
// usually I wouldn't do this but considering that the map has
|
||||
// a super small size an O(n) solution is acceptable
|
||||
@ -69,6 +66,7 @@ public:
|
||||
// void apply_buff(const stat_name statn, const float mul);
|
||||
// reserved for later
|
||||
protected:
|
||||
RNG &rng;
|
||||
const enum race race;
|
||||
|
||||
int HP;
|
||||
|
@ -1,10 +1,10 @@
|
||||
#include "curses_input.h"
|
||||
|
||||
curses_input::curses_input(const std::unique_ptr<cursor> &new_curse):
|
||||
curses_input::curses_input(cursor &new_curse):
|
||||
curse{new_curse} {}
|
||||
|
||||
game_command curses_input::get_command() {
|
||||
switch (curse->getcmd()) {
|
||||
switch (curse.getcmd()) {
|
||||
case 'h':
|
||||
return game_command::move_west;
|
||||
|
||||
@ -51,7 +51,7 @@ game_command curses_input::get_command() {
|
||||
return game_command_pass;
|
||||
}
|
||||
|
||||
switch (curse->getcmd()) {
|
||||
switch (curse.getcmd()) {
|
||||
case 'h':
|
||||
return game_command::apply_west;
|
||||
|
||||
|
@ -8,9 +8,9 @@
|
||||
|
||||
class curses_input final: public input {
|
||||
private:
|
||||
const std::unique_ptr<cursor> &curse;
|
||||
cursor &curse;
|
||||
public:
|
||||
curses_input(const std::unique_ptr<cursor> &new_curse);
|
||||
curses_input(cursor &new_curse);
|
||||
game_command get_command() override;
|
||||
};
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
#include "curses_output.h"
|
||||
|
||||
curses_output::curses_output(const std::unique_ptr<cursor> &new_curse):
|
||||
curses_output::curses_output(cursor &new_curse):
|
||||
curse{new_curse} {}
|
||||
|
||||
void curses_output::render() {
|
||||
curse->show();
|
||||
curse.show();
|
||||
}
|
||||
|
||||
void curses_output::clear() {
|
||||
curse->clear();
|
||||
curse.clear();
|
||||
}
|
||||
|
||||
void curses_output::print_char(const position &pos, const char ch,
|
||||
@ -16,7 +16,7 @@ void curses_output::print_char(const position &pos, const char ch,
|
||||
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
|
||||
return;
|
||||
|
||||
curse->print_char(pos, ch, attrs);
|
||||
curse.print_char(pos, ch, attrs);
|
||||
}
|
||||
|
||||
void curses_output::print_str(const position &pos, const std::string &str,
|
||||
@ -27,7 +27,7 @@ void curses_output::print_str(const position &pos, const std::string &str,
|
||||
position tmp = pos;
|
||||
|
||||
for (std::size_t i = 0; i < str.length(); ++i) {
|
||||
curse->print_char(tmp, str[i], attrs);
|
||||
curse.print_char(tmp, str[i], attrs);
|
||||
tmp += {1, 0};
|
||||
|
||||
if (tmp.x >= DISPLAY_WIDTH)
|
||||
|
@ -8,9 +8,9 @@
|
||||
|
||||
class curses_output final : public display {
|
||||
private:
|
||||
const std::unique_ptr<cursor> &curse;
|
||||
cursor &curse;
|
||||
public:
|
||||
curses_output(const std::unique_ptr<cursor> &new_curse);
|
||||
curses_output(cursor &new_curse);
|
||||
|
||||
void render() override;
|
||||
void clear() override;
|
||||
|
16
src/game.h
16
src/game.h
@ -13,17 +13,17 @@
|
||||
class game final {
|
||||
private:
|
||||
feature features;
|
||||
const std::unique_ptr<input> ∈
|
||||
const std::unique_ptr<display> &out;
|
||||
const std::unique_ptr<logger> &log;
|
||||
const std::unique_ptr<RNG> &rng;
|
||||
input ∈
|
||||
display &out;
|
||||
logger &log;
|
||||
RNG &rng;
|
||||
std::unique_ptr<character> player;
|
||||
public:
|
||||
game(const feature enabled_features,
|
||||
const std::unique_ptr<input> &new_in,
|
||||
const std::unique_ptr<display> &new_out,
|
||||
const std::unique_ptr<logger> &new_log,
|
||||
const std::unique_ptr<RNG> &new_rng);
|
||||
input &new_in,
|
||||
display &new_out,
|
||||
logger &new_log,
|
||||
RNG &new_rng);
|
||||
game_status run();
|
||||
private:
|
||||
int getcmd() const;
|
||||
|
@ -2,16 +2,24 @@
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
goblin::goblin(RNG &rng, const position_list &available_positions):
|
||||
character{rng, race::rshade} {
|
||||
pos = available_positions[rng.rand_under(available_positions.size())];
|
||||
gold = 0;
|
||||
hostile = true;
|
||||
}
|
||||
|
||||
result goblin::attack(const direction dir, const character_list &chlist) {
|
||||
result goblin::attack(const direction dir, 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 (tmp == ch.get_position()) {
|
||||
auto res = ch.get_hit(race, ATK, base_hit_rate);
|
||||
|
||||
if (res == result::died) {
|
||||
gold += GAIN_GOLD;
|
||||
gold += GAIN_GOLD;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -19,8 +27,8 @@ result goblin::attack(const direction dir, const character_list &chlist) {
|
||||
}
|
||||
|
||||
result goblin::get_hit(const enum race &race, const int atk,
|
||||
const float hitrate) {
|
||||
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||
const float hitrate) {
|
||||
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||
HP = std::max(HP - calc_dmg(atk, DEF), 0);
|
||||
|
||||
if (HP == 0)
|
||||
|
@ -6,11 +6,12 @@ const int GAIN_GOLD = 5;
|
||||
|
||||
class goblin final: public character {
|
||||
public:
|
||||
goblin(const position_list &available_positions); // spawn at a random place
|
||||
goblin(RNG &rng,
|
||||
const position_list &available_positions);
|
||||
virtual result attack(const direction dir,
|
||||
const character_list &chlist) override;
|
||||
character_list &chlist) override;
|
||||
virtual result get_hit(const enum race &race, const int atk,
|
||||
const float hit_rate) override;
|
||||
const float hit_rate) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -19,7 +19,7 @@ int main(int argc, char **argv) {
|
||||
return RETURN_PANICKED;
|
||||
}
|
||||
|
||||
game game_proc(enabled_features, in, out, log, rng);
|
||||
game game_proc(enabled_features, *in, *out, *log, *rng);
|
||||
|
||||
while (game_proc.run() != game_status::terminated)
|
||||
out->render();
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef __RESTORE_HEALTH_H__
|
||||
#define __RESTORE_HEALTH_H__
|
||||
|
||||
#include "../potion.h"
|
||||
#include "potion.h"
|
||||
|
||||
class restore_health final: public potion {
|
||||
public:
|
@ -3,8 +3,8 @@
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
shade::shade(const position_list &available_positions):
|
||||
character{race::rshade} {
|
||||
shade::shade(RNG &rng, const position_list &available_positions):
|
||||
character{rng, race::rshade} {
|
||||
pos = available_positions[rng.rand_under(available_positions.size())];
|
||||
gold = 0;
|
||||
hostile = true;
|
||||
|
@ -5,7 +5,8 @@
|
||||
|
||||
class shade final: public character {
|
||||
public:
|
||||
shade(const position_list &available_positions); // spawn at a random place
|
||||
shade(RNG &rng, const position_list
|
||||
&available_positions); // spawn at a random place
|
||||
virtual result attack(const direction dir,
|
||||
character_list &chlist) override;
|
||||
virtual result get_hit(const enum race &race, const int atk,
|
||||
|
@ -2,16 +2,24 @@
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
vampire::vampire(RNG &rng, const position_list &available_positions):
|
||||
character{rng, race::rshade} {
|
||||
pos = available_positions[rng.rand_under(available_positions.size())];
|
||||
gold = 0;
|
||||
hostile = true;
|
||||
}
|
||||
|
||||
result vampire::attack(const direction dir, const character_list &chlist) {
|
||||
result vampire::attack(const direction dir, 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 (tmp == ch.get_position()) {
|
||||
auto res = ch.get_hit(race, ATK, base_hit_rate);
|
||||
|
||||
if (res != result::miss) {
|
||||
HP += GAIN_HP;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -19,8 +27,8 @@ result vampire::attack(const direction dir, const character_list &chlist) {
|
||||
}
|
||||
|
||||
result vampire::get_hit(const enum race &race, const int atk,
|
||||
const float hitrate) {
|
||||
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||
const float hitrate) {
|
||||
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||
HP = std::max(HP - calc_dmg(atk, DEF), 0);
|
||||
|
||||
if (HP == 0)
|
||||
|
@ -6,11 +6,12 @@ const int GAIN_HP = 5;
|
||||
|
||||
class vampire final: public character {
|
||||
public:
|
||||
vampire(const position_list &available_positions); // spawn at a random place
|
||||
vampire(RNG &rng,
|
||||
const position_list &available_positions);
|
||||
virtual result attack(const direction dir,
|
||||
const character_list &chlist) override;
|
||||
character_list &chlist) override;
|
||||
virtual result get_hit(const enum race &race, const int atk,
|
||||
const float hit_rate) override;
|
||||
const float hit_rate) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user