first working demo

This commit is contained in:
2024-06-30 03:24:19 -04:00
parent 6dcf0f41e8
commit 12b74f12d4
5 changed files with 22 additions and 46 deletions

View File

@ -9,7 +9,7 @@ void display::clear() {
void display::print(std::ostream &out) const { void display::print(std::ostream &out) const {
for (auto line : contents) for (auto line : contents)
out << line << std::endl; out << line;
} }
void display::print_line(const std::string &line, const int linenum) { void display::print_line(const std::string &line, const int linenum) {

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <ncurses.h>
#include "map.h" #include "map.h"
#include "races.h" #include "races.h"
#include "display.h" #include "display.h"
@ -23,6 +24,8 @@ int main() {
display.clear(); display.clear();
mmap->print(display); mmap->print(display);
display.print_position(player->get_position(), '@'); display.print_position(player->get_position(), '@');
display.print(cout);
cout << "Command: " << ch << endl;
auto available_positions = mmap->get_available_positions(); auto available_positions = mmap->get_available_positions();

View File

@ -1,23 +1,26 @@
#include "map.h" #include "map.h"
#include <sstream> #include <sstream>
#include <iostream>
#include <fstream> #include <fstream>
#include <algorithm> #include <algorithm>
game_map::game_map(int lvl): game_map::game_map(int lvl):
layer{layer_num::map}, map{MAP_HEIGHT}, level{lvl} { layer{layer_num::map} {
level = lvl;
// TODO: randomly generate a map // TODO: randomly generate a map
} }
game_map::game_map(const std::string &map_data, int lvl): game_map::game_map(const std::string &map_data, int lvl):
layer{layer_num::map}, map{MAP_HEIGHT}, level{lvl} { layer{layer_num::map} {
level = lvl;
std::istringstream iss{map_data}; std::istringstream iss{map_data};
std::string line; std::string line;
for (int i = 0; i < MAP_HEIGHT; ++i) { for (int i = 0; i < MAP_HEIGHT; ++i) {
getline(iss, line); getline(iss, line);
map.push_back(line); map[i] = line;
} }
} }
@ -32,7 +35,9 @@ std::vector<position> game_map::get_available_positions() const {
for (int line = 0; line < MAP_HEIGHT; ++line) for (int line = 0; line < MAP_HEIGHT; ++line)
for (int x = 0; x < MAP_WIDTH; ++x) for (int x = 0; x < MAP_WIDTH; ++x)
if (map[line][x] == '.') if (map[line][x] == '.' ||
map[line][x] == '#' ||
map[line][x] == '+')
result.push_back(position{x, line}); result.push_back(position{x, line});
return result; return result;
} }

View File

@ -42,14 +42,14 @@ public:
// just in case you want to retain the potion effects // just in case you want to retain the potion effects
void enter_level(character *who); void enter_level(character *who);
void leave_level(character *who); void leave_level(character *who);
private: public:
struct effect { struct effect {
character *who; character *who;
stat_name statn; stat_name statn;
int amount; int amount;
}; };
std::vector<std::string> map; std::string map[MAP_HEIGHT];
int level; int level;
// use this to remember every applied potion // use this to remember every applied potion
@ -57,31 +57,6 @@ private:
}; };
const std::string default_map = const std::string default_map =
"|-----------------------------------------------------------------------------|\ "|-----------------------------------------------------------------------------|\n| |\n| |--------------------------| |-----------------------| |\n| |..........................| |.......................| |\n| |..........................+########+.......................|-------| |\n| |..........................| # |...............................|--| |\n| |..........................| # |..................................|--| |\n| |----------+---------------| # |----+----------------|...............| |\n| # ############# |...............| |\n| # # |-----+------| |...............| |\n| # # |............| |...............| |\n| ################### |............| ######+...............| |\n| # # |............| # |...............| |\n| # # |-----+------| # |--------+------| |\n| |---------+-----------| # # # # |\n| |.....................| # # # |----+------| |\n| |.....................| ######################## |...........| |\n| |.....................| # # |...........| |\n| |.....................| # |------+--------------------|...........| |\n| |.....................| # |.......................................| |\n| |.....................+##########+.......................................| |\n| |.....................| |.......................................| |\n| |---------------------| |---------------------------------------| |\n| |\n|-----------------------------------------------------------------------------|";
| |\
| |--------------------------| |-----------------------| |\
| |..........................| |.......................| |\
| |..........................+########+.......................|-------| |\
| |..........................| # |...............................|--| |\
| |..........................| # |..................................|--| |\
| |----------+---------------| # |----+----------------|...............| |\
| # ############# |...............| |\
| # # |-----+------| |...............| |\
| # # |............| |...............| |\
| ################### |............| ######+...............| |\
| # # |............| # |...............| |\
| # # |-----+------| # |--------+------| |\
| |---------+-----------| # # # # |\
| |.....................| # # # |----+------| |\
| |.....................| ######################## |...........| |\
| |.....................| # # |...........| |\
| |.....................| # |------+--------------------|...........| |\
| |.....................| # |.......................................| |\
| |.....................+##########+.......................................| |\
| |.....................| |.......................................| |\
| |---------------------| |---------------------------------------| |\
| |\
|-----------------------------------------------------------------------------|\
";
#endif #endif

View File

@ -31,22 +31,15 @@ bool position::operator<(const position &other) const {
return y < other.y || x < other.x; return y < other.y || x < other.x;
} }
#include <iostream>
size_t find(const std::vector<position> &sorted_list, size_t find(const std::vector<position> &sorted_list,
const position &target) { const position &target) {
int l = 0; // TODO: implement binary searching
int r = sorted_list.size() - 1;
while (l <= r) { for (size_t i = 0; i < sorted_list.size(); ++i)
int mid = l + (l + r) / 2; if (sorted_list[i] == target)
return i;
if (target == sorted_list[mid])
return mid;
if (target < sorted_list[mid])
l = mid + 1;
else
r = mid - 1;
}
return sorted_list.size(); return sorted_list.size();
} }