added item base class for gold and potions

This commit is contained in:
2024-07-18 12:54:41 -04:00
parent 8704fdb7aa
commit dfea74ed42
9 changed files with 51 additions and 65 deletions

View File

@ -90,13 +90,12 @@ int enemy_base::dies(level *lvl) {
if (race == race::rdragon) { if (race == race::rdragon) {
return 0; return 0;
} else if (race == race::rmerchant) { } else if (race == race::rmerchant) {
lvl->add_gold({GOLD_MERCHANT, pos}); lvl->add_gold(gold{pos, GOLD_MERCHANT});
return 0; return 0;
} else if (race == race::rhuman) { } else if (race == race::rhuman) {
lvl->add_gold({GOLD_NORMAL, pos}); lvl->add_gold(gold{pos, GOLD_NORMAL});
auto plist = lvl->get_available_around_all(pos); auto plist = lvl->get_available_around_all(pos);
lvl->add_gold({GOLD_NORMAL, lvl->add_gold(gold{rng->get_rand_in_vector(plist), GOLD_NORMAL});
rng->get_rand_in_vector(plist)});
return 0; return 0;
} }

View File

@ -78,7 +78,7 @@ character *game::move_enemies() {
levels[curr_level]->erase_enemy(ch); levels[curr_level]->erase_enemy(ch);
if (g) if (g)
levels[curr_level]->add_gold({g, ch->get_pos()}); levels[curr_level]->add_gold(gold{ch->get_pos(), g});
} }
msg += res.msg; msg += res.msg;

View File

@ -20,10 +20,10 @@ int rand_gold_pile(RNG *rng) {
} }
gold get_gold_at(const position &pos, gold_list &glist) { gold get_gold_at(const position &pos, gold_list &glist) {
gold ret = {0, {0, 0}}; gold ret({0, 0}, 0);
for (size_t i = 0; i < glist.size(); ++i) for (size_t i = 0; i < glist.size(); ++i)
if (glist[i].pos == pos) { if (glist[i].get_pos() == pos) {
ret = glist[i]; ret = glist[i];
glist.erase(i + glist.begin()); glist.erase(i + glist.begin());
return ret; return ret;
@ -38,3 +38,17 @@ int rand_gold_drop(RNG *rng) {
return GOLD_SMALL; return GOLD_SMALL;
} }
gold::gold(const position &pos, const int amount): item{pos}, amount{amount} {}
void gold::print(output *out) const {
out->print_char(pos, 'G', COLOR_PAIR(COLOR_YELLOW));
}
int gold::get_amount() const {
return amount;
}
void gold::set_amount(const int new_amount) {
amount = new_amount;
}

View File

@ -4,10 +4,17 @@
#include <vector> #include <vector>
#include "position.h" #include "position.h"
#include "rng.h" #include "rng.h"
#include "item.h"
struct gold { class gold : public item {
private:
int amount; int amount;
position pos; public:
gold(const position &pos, const int amount);
void print(output *out) const override;
int get_amount() const;
void set_amount(const int new_amount);
}; };
typedef std::vector<gold> gold_list; typedef std::vector<gold> gold_list;

View File

@ -47,8 +47,9 @@ gold_list level::dragon_hoard() {
gold_list result; gold_list result;
for (auto g : glist) for (auto g : glist)
if (g.amount == GOLD_DRAGON) if (g.get_amount() == GOLD_DRAGON)
result.push_back({g.amount, g.pos}); result.push_back(g);
return result; return result;
} }
@ -65,17 +66,17 @@ void level::gen_enemies(RNG *rng, std::vector<position_list> &tiles) {
position_list spots; position_list spots;
for (int dir = 0; dir < DIRECTION_CNT; ++dir) { for (int dir = 0; dir < DIRECTION_CNT; ++dir) {
position tmp = dhoard[i].pos + MOVE[dir]; position tmp = dhoard[i].get_pos() + MOVE[dir];
if (map.which_room(tmp) != -1) if (map.which_room(tmp) != -1)
spots.push_back(dhoard[i].pos + MOVE[dir]); spots.push_back(dhoard[i].get_pos() + MOVE[dir]);
} }
auto pos = spots.size() ? rng->get_rand_in_vector(spots) : auto pos = spots.size() ? rng->get_rand_in_vector(spots) :
dhoard[i].pos; dhoard[i].get_pos();
pelist.push_back(new_dragon(rng, pos, dhoard[i].pos, pelist.push_back(new_dragon(rng, pos, dhoard[i].get_pos(),
enabled_features, enabled_features,
map.which_room(dhoard[i].pos))); map.which_room(dhoard[i].get_pos())));
int room = map.which_room(pos); int room = map.which_room(pos);
remove_from_list(tiles[room], pos); remove_from_list(tiles[room], pos);
@ -116,7 +117,7 @@ void level::gen_gold(RNG *rng, std::vector<position_list> &tiles) {
glist.reserve(GOLD_CNT); glist.reserve(GOLD_CNT);
for (int i = 0; i < GOLD_CNT; ++i) for (int i = 0; i < GOLD_CNT; ++i)
glist.push_back({rand_gold_pile(rng), get_rand_pos(rng, tiles)}); glist.push_back(gold{get_rand_pos(rng, tiles), rand_gold_pile(rng)});
} }
void level::gen_potions(RNG *rng, std::vector<position_list> &tiles) { void level::gen_potions(RNG *rng, std::vector<position_list> &tiles) {
@ -148,7 +149,7 @@ void level::print(output *out) const {
plist[i]->print(out); plist[i]->print(out);
for (size_t i = 0; i < glist.size(); ++i) for (size_t i = 0; i < glist.size(); ++i)
out->print_char(glist[i].pos, 'G', COLOR_PAIR(COLOR_YELLOW)); glist[i].print(out);
for (size_t i = 0; i < elist.size(); ++i) for (size_t i = 0; i < elist.size(); ++i)
elist[i]->print(out); elist[i]->print(out);
@ -172,7 +173,7 @@ bool level::is_available(const position &pos, bool is_player) const {
if (!(enabled_features & FEATURE_WALK_OVER) && !is_player) if (!(enabled_features & FEATURE_WALK_OVER) && !is_player)
for (size_t i = 0; i < glist.size(); ++i) for (size_t i = 0; i < glist.size(); ++i)
if (pos == glist[i].pos) if (pos == glist[i].get_pos())
return false; return false;
return true; return true;
@ -198,7 +199,7 @@ bool level::is_available_all(const position &pos) const {
return false; return false;
for (size_t i = 0; i < glist.size(); ++i) for (size_t i = 0; i < glist.size(); ++i)
if (pos == glist[i].pos) if (pos == glist[i].get_pos())
return false; return false;
return true; return true;

View File

@ -1,5 +1,5 @@
#ifndef __DISPLAY_H__ #ifndef __OUTPUT_H__
#define __DISPLAY_H__ #define __OUTPUT_H__
#include <ncurses.h> #include <ncurses.h>
#include <vector> #include <vector>

View File

@ -181,10 +181,10 @@ long_result player_base::interpret_command(level *lvl, game_command cmd) {
auto res = move(lvl, pos + MOVE[cmd - move_north]); auto res = move(lvl, pos + MOVE[cmd - move_north]);
gold g = get_gold_at(pos, lvl->get_glist()); gold g = get_gold_at(pos, lvl->get_glist());
gold_cnt += g.amount; gold_cnt += g.get_amount();
if (g.amount) if (g.get_amount())
res.msg += "PC picked up " + std::to_string(g.amount) + res.msg += "PC picked up " + std::to_string(g.get_amount()) +
" pieces of gold. "; " pieces of gold. ";
if (enabled_features & FEATURE_INVENTORY) { if (enabled_features & FEATURE_INVENTORY) {

View File

@ -3,7 +3,7 @@
#include "constants.h" #include "constants.h"
potion::potion(const potion_type type, const int duration, const position &pos): potion::potion(const potion_type type, const int duration, const position &pos):
type{type}, remaining_duration{duration}, pos{pos} {} item{pos}, type{type}, remaining_duration{duration} {}
potion_type potion::get_type() const { potion_type potion::get_type() const {
return type; return type;
@ -13,37 +13,7 @@ int potion::get_duration() const {
return remaining_duration; return remaining_duration;
} }
position potion::get_pos() const { void potion::print(output *out) const {
return pos;
}
void potion::set_pos(const position &npos) {
pos = npos;
}
potion::potion(const potion &p):
type(p.type), remaining_duration(p.remaining_duration),
pos{p.pos} {}
potion::potion(potion &&p):
type(p.type), remaining_duration(p.remaining_duration),
pos{p.pos} {}
potion &potion::operator=(const potion &p) {
type = p.type;
remaining_duration = p.remaining_duration;
pos = p.pos;
return *this;
}
potion &potion::operator=(potion &&p) {
type = p.type;
remaining_duration = p.remaining_duration;
pos = p.pos;
return *this;
}
void potion::print(output *out) {
out->print_char(pos, 'P', COLOR_PAIR(COLOR_GREEN)); out->print_char(pos, 'P', COLOR_PAIR(COLOR_GREEN));
} }

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include "fraction.h" #include "fraction.h"
#include "output.h" #include "output.h"
#include "item.h"
enum potion_type : int; enum potion_type : int;
enum race : int; enum race : int;
@ -12,7 +13,7 @@ enum race : int;
// IMPORTANT: pop all potions of duration == 0, and when entering a // IMPORTANT: pop all potions of duration == 0, and when entering a
// new level, pop all potions of duration == -1 // new level, pop all potions of duration == -1
class potion { class potion : public item {
protected: protected:
// Use -1 to denote that the effect will last until leaving the level // Use -1 to denote that the effect will last until leaving the level
// Otherwise, use a positive number // Otherwise, use a positive number
@ -22,10 +23,6 @@ protected:
position pos; position pos;
constexpr static const float DROW_POTION_MUL = 1.5f; constexpr static const float DROW_POTION_MUL = 1.5f;
public: public:
potion(const potion &p);
potion(potion &&p);
potion &operator=(const potion &p);
potion &operator=(potion &&p);
potion(const potion_type type, const int duration, const position &pos); potion(const potion_type type, const int duration, const position &pos);
// apply decrements remaining_duration if it's positive, and // apply decrements remaining_duration if it's positive, and
// won't do anything if it's non-negative // won't do anything if it's non-negative
@ -34,11 +31,9 @@ public:
virtual int get_priority() const = 0; virtual int get_priority() const = 0;
potion_type get_type() const; potion_type get_type() const;
int get_duration() const; int get_duration() const;
position get_pos() const;
void set_pos(const position &npos);
virtual const char *get_name() const = 0; virtual const char *get_name() const = 0;
virtual void print(output *out); void print(output *out) const override;
}; };
typedef std::vector<potion *> potion_list; typedef std::vector<potion *> potion_list;