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) {
return 0;
} else if (race == race::rmerchant) {
lvl->add_gold({GOLD_MERCHANT, pos});
lvl->add_gold(gold{pos, GOLD_MERCHANT});
return 0;
} 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);
lvl->add_gold({GOLD_NORMAL,
rng->get_rand_in_vector(plist)});
lvl->add_gold(gold{rng->get_rand_in_vector(plist), GOLD_NORMAL});
return 0;
}

View File

@ -78,7 +78,7 @@ character *game::move_enemies() {
levels[curr_level]->erase_enemy(ch);
if (g)
levels[curr_level]->add_gold({g, ch->get_pos()});
levels[curr_level]->add_gold(gold{ch->get_pos(), g});
}
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 ret = {0, {0, 0}};
gold ret({0, 0}, 0);
for (size_t i = 0; i < glist.size(); ++i)
if (glist[i].pos == pos) {
if (glist[i].get_pos() == pos) {
ret = glist[i];
glist.erase(i + glist.begin());
return ret;
@ -38,3 +38,17 @@ int rand_gold_drop(RNG *rng) {
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 "position.h"
#include "rng.h"
#include "item.h"
struct gold {
class gold : public item {
private:
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;

View File

@ -47,8 +47,9 @@ gold_list level::dragon_hoard() {
gold_list result;
for (auto g : glist)
if (g.amount == GOLD_DRAGON)
result.push_back({g.amount, g.pos});
if (g.get_amount() == GOLD_DRAGON)
result.push_back(g);
return result;
}
@ -65,17 +66,17 @@ void level::gen_enemies(RNG *rng, std::vector<position_list> &tiles) {
position_list spots;
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)
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) :
dhoard[i].pos;
pelist.push_back(new_dragon(rng, pos, dhoard[i].pos,
dhoard[i].get_pos();
pelist.push_back(new_dragon(rng, pos, dhoard[i].get_pos(),
enabled_features,
map.which_room(dhoard[i].pos)));
map.which_room(dhoard[i].get_pos())));
int room = map.which_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);
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) {
@ -148,7 +149,7 @@ void level::print(output *out) const {
plist[i]->print(out);
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)
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)
for (size_t i = 0; i < glist.size(); ++i)
if (pos == glist[i].pos)
if (pos == glist[i].get_pos())
return false;
return true;
@ -198,7 +199,7 @@ bool level::is_available_all(const position &pos) const {
return false;
for (size_t i = 0; i < glist.size(); ++i)
if (pos == glist[i].pos)
if (pos == glist[i].get_pos())
return false;
return true;

View File

@ -1,5 +1,5 @@
#ifndef __DISPLAY_H__
#define __DISPLAY_H__
#ifndef __OUTPUT_H__
#define __OUTPUT_H__
#include <ncurses.h>
#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]);
gold g = get_gold_at(pos, lvl->get_glist());
gold_cnt += g.amount;
gold_cnt += g.get_amount();
if (g.amount)
res.msg += "PC picked up " + std::to_string(g.amount) +
if (g.get_amount())
res.msg += "PC picked up " + std::to_string(g.get_amount()) +
" pieces of gold. ";
if (enabled_features & FEATURE_INVENTORY) {

View File

@ -3,7 +3,7 @@
#include "constants.h"
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 {
return type;
@ -13,37 +13,7 @@ int potion::get_duration() const {
return remaining_duration;
}
position potion::get_pos() 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) {
void potion::print(output *out) const {
out->print_char(pos, 'P', COLOR_PAIR(COLOR_GREEN));
}

View File

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