Conver to using \ for member variables in classes. In structs just use the name.

main
Zed A. Shaw 4 days ago
parent 187edb898e
commit 5cf66aad02
  1. 2
      entity.cpp
  2. 3
      entity.hpp
  3. 6
      fsm.hpp
  4. 122
      gui.cpp
  5. 32
      gui.hpp
  6. 96
      map.cpp
  7. 30
      map.hpp
  8. 2
      tests/fsm.cpp

@ -5,7 +5,7 @@ void Entity::move(Point loc) {
} }
void Entity::event(EntityEvent ev) { void Entity::event(EntityEvent ev) {
switch(_state) { switch($state) {
FSM_STATE(EntityState, START, ev); FSM_STATE(EntityState, START, ev);
FSM_STATE(EntityState, HUNTING, ev); FSM_STATE(EntityState, HUNTING, ev);
FSM_STATE(EntityState, DEAD, ev); FSM_STATE(EntityState, DEAD, ev);

@ -11,8 +11,7 @@ enum class EntityEvent {
}; };
class Entity : public DeadSimpleFSM<EntityState, EntityEvent> { struct Entity : public DeadSimpleFSM<EntityState, EntityEvent> {
public:
Point location{0,0}; Point location{0,0};
int hp = 20; int hp = 20;
int damage = 10; int damage = 10;

@ -12,17 +12,17 @@ template<typename S, typename E>
class DeadSimpleFSM { class DeadSimpleFSM {
protected: protected:
// BUG: don't put this in your class because state() won't work // BUG: don't put this in your class because state() won't work
S _state = S::START; S $state = S::START;
public: public:
template<typename... Types> template<typename... Types>
void event(E event, Types... args); void event(E event, Types... args);
void state(S next_state) { void state(S next_state) {
_state = next_state; $state = next_state;
} }
bool in_state(S state) { bool in_state(S state) {
return _state == state; return $state == state;
} }
}; };

@ -40,80 +40,80 @@ sf::Color GUI::color(Value val) {
return VALUES[size_t(val)]; return VALUES[size_t(val)];
} }
GUI::GUI() : game_map_(50, 20), GUI::GUI() : $game_map(50, 20),
canvas_(60 * 2, 20 * 4), $canvas(60 * 2, 20 * 4),
window_(sf::VideoMode(1600,900), "Roguish"), $window(sf::VideoMode(1600,900), "Roguish"),
screen_(0,0) $screen(0,0)
{ {
int res = hit_buf_.loadFromFile("./assets/hit.wav"); int res = $hit_buf.loadFromFile("./assets/hit.wav");
dbc::check(res, "failed to load hit.wav"); dbc::check(res, "failed to load hit.wav");
hit_sound_.setBuffer(hit_buf_); $hit_sound.setBuffer($hit_buf);
font_.loadFromFile("./assets/text.otf"); $font.loadFromFile("./assets/text.otf");
text_.setFont(font_); $text.setFont($font);
text_.setCharacterSize(30); $text.setCharacterSize(30);
text_.setFillColor(color(Value::LIGHT_DARK)); $text.setFillColor(color(Value::LIGHT_DARK));
game_map_.generate(); $game_map.generate();
player_.location = game_map_.place_entity(0); $player.location = $game_map.place_entity(0);
enemy_.location = game_map_.place_entity(1); $enemy.location = $game_map.place_entity(1);
goal_ = game_map_.place_entity(game_map_.room_count() - 1); $goal = $game_map.place_entity($game_map.room_count() - 1);
screen_ = Screen::Create(Dimension::Full()); $screen = Screen::Create(Dimension::Full());
} }
void GUI::create_renderer() { void GUI::create_renderer() {
map_view_ = Renderer([&] { $map_view = Renderer([&] {
Matrix &walls = game_map_.walls(); Matrix &walls = $game_map.walls();
game_map_.set_target(player_.location); $game_map.set_target($player.location);
game_map_.make_paths(); $game_map.make_paths();
Matrix &paths = game_map_.paths(); Matrix &paths = $game_map.paths();
if(player_.in_state(EntityState::DEAD)) { if($player.in_state(EntityState::DEAD)) {
status_text_ = "DEAD!"; $status_text = "DEAD!";
} }
for(size_t x = 0; x < walls[0].size(); ++x) { for(size_t x = 0; x < walls[0].size(); ++x) {
for(size_t y = 0; y < walls.size(); ++y) { for(size_t y = 0; y < walls.size(); ++y) {
string tile = walls[y][x] == 1 ? "#" : format("{}", paths[y][x]); string tile = walls[y][x] == 1 ? "#" : format("{}", paths[y][x]);
if(tile == "#") { if(tile == "#") {
canvas_.DrawText(x*2, y*4, tile); $canvas.DrawText(x*2, y*4, tile);
} else if(show_paths_) { } else if($show_paths) {
//int pnum = paths[y][x]; //int pnum = paths[y][x];
canvas_.DrawText(x*2, y*4, tile); $canvas.DrawText(x*2, y*4, tile);
} else { } else {
canvas_.DrawText(x*2, y*4, "."); $canvas.DrawText(x*2, y*4, ".");
} }
} }
} }
canvas_.DrawText(enemy_.location.x*2, enemy_.location.y*4, "!"); $canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, "!");
canvas_.DrawText(player_.location.x*2, player_.location.y*4, "@"); $canvas.DrawText($player.location.x*2, $player.location.y*4, "@");
canvas_.DrawText(goal_.x*2, goal_.y*4, "$"); $canvas.DrawText($goal.x*2, $goal.y*4, "$");
return canvas(canvas_); return canvas($canvas);
}); });
document_ = Renderer([&]{ $document = Renderer([&]{
return hbox({ return hbox({
hflow( hflow(
vbox( vbox(
text(format("HP: {}", player_.hp)) | border, text(format("HP: {}", $player.hp)) | border,
text(status_text_) | border text($status_text) | border
) | xflex_grow ) | xflex_grow
), ),
separator(), separator(),
hbox(map_view_->Render()), hbox($map_view->Render()),
}); });
}); });
} }
void GUI::handle_events() { void GUI::handle_events() {
sf::Event event; sf::Event event;
while(window_.pollEvent(event)) { while($window.pollEvent(event)) {
if(event.type == sf::Event::Closed) { if(event.type == sf::Event::Closed) {
window_.close(); $window.close();
} else if(event.type == sf::Event::KeyPressed) { } else if(event.type == sf::Event::KeyPressed) {
size_t x = player_.location.x; size_t x = $player.location.x;
size_t y = player_.location.y; size_t y = $player.location.y;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
x -= 1; x -= 1;
@ -125,44 +125,44 @@ void GUI::handle_events() {
y += 1; y += 1;
} }
if(game_map_.inmap(x,y) && !game_map_.iswall(x,y)) { if($game_map.inmap(x,y) && !$game_map.iswall(x,y)) {
game_map_.clear_target(player_.location); $game_map.clear_target($player.location);
player_.move({x, y}); $player.move({x, y});
} else { } else {
hit_sound_.play(); $hit_sound.play();
} }
// move enemy_ here // move $enemy here
bool found = game_map_.neighbors(enemy_.location, true); bool found = $game_map.neighbors($enemy.location, true);
if(!found) { if(!found) {
status_text_ = "ENEMY STUCK!"; $status_text = "ENEMY STUCK!";
} }
if(enemy_.location.x == player_.location.x && enemy_.location.y == player_.location.y) { if($enemy.location.x == $player.location.x && $enemy.location.y == $player.location.y) {
player_.event(EntityEvent::HIT); $player.event(EntityEvent::HIT);
} else if(goal_.x == player_.location.x && goal_.y == player_.location.y) { } else if($goal.x == $player.location.x && $goal.y == $player.location.y) {
status_text_ = "YOU WIN!"; $status_text = "YOU WIN!";
} }
} }
} }
} }
void GUI::render_scene() { void GUI::render_scene() {
Render(screen_, document_->Render()); Render($screen, $document->Render());
std::string screen_out = screen_.ToString(); std::string $screenout = $screen.ToString();
std::wstring utf8 = converter_.from_bytes(screen_out); std::wstring utf8 = $converter.from_bytes($screenout);
text_.setString(utf8); $text.setString(utf8);
text_.setPosition({0,0}); $text.setPosition({0,0});
window_.clear(); $window.clear();
window_.draw(text_); $window.draw($text);
window_.display(); $window.display();
} }
int GUI::main() { int GUI::main() {
create_renderer(); create_renderer();
while(window_.isOpen()) { while($window.isOpen()) {
render_scene(); render_scene();
handle_events(); handle_events();
} }

@ -24,22 +24,22 @@ enum class Value {
class GUI { class GUI {
sf::Color color(Value val); sf::Color color(Value val);
Map game_map_; Map $game_map;
sf::SoundBuffer hit_buf_; sf::SoundBuffer $hit_buf;
sf::Sound hit_sound_; sf::Sound $hit_sound;
bool show_paths_ = false; bool $show_paths = false;
string status_text_ = "NOT DEAD"; string $status_text = "NOT DEAD";
Entity player_; Entity $player;
Entity enemy_; Entity $enemy;
Point goal_; Point $goal;
Component document_; Component $document;
Component map_view_; Component $map_view;
Canvas canvas_; Canvas $canvas;
sf::Font font_; sf::Font $font;
sf::Text text_; sf::Text $text;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter_; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
sf::RenderWindow window_; sf::RenderWindow $window;
Screen screen_; Screen $screen;
public: public:
GUI(); GUI();

@ -5,8 +5,8 @@
#include <random> #include <random>
#include <utility> #include <utility>
std::random_device g_rng; std::random_device $RNG;
std::mt19937 g_generator(g_rng()); std::mt19937 $GENERATOR($RNG());
using std::vector, std::pair; using std::vector, std::pair;
using namespace fmt; using namespace fmt;
@ -45,20 +45,20 @@ inline void add_neighbors(PointList &neighbors, Matrix &closed, size_t y, size_t
* can run make_rooms and generate on. It will * can run make_rooms and generate on. It will
* NOT be valid until you actually run generate. * NOT be valid until you actually run generate.
*/ */
Map::Map(size_t width, size_t height) : limit_(1000) { Map::Map(size_t width, size_t height) : $limit(1000) {
walls_ = Matrix(height, MatrixRow(width, INV_WALL)); $walls = Matrix(height, MatrixRow(width, INV_WALL));
input_map_ = Matrix(height, MatrixRow(width, 1)); $input_map = Matrix(height, MatrixRow(width, 1));
} }
void Map::make_paths() { void Map::make_paths() {
size_t h = input_map_.size(); size_t h = $input_map.size();
size_t w = input_map_[0].size(); size_t w = $input_map[0].size();
// Initialize the new array with every pixel at limit distance // Initialize the new array with every pixel at limit distance
// NOTE: this is normally ones() * limit // NOTE: this is normally ones() * limit
int limit = limit_ == 0 ? h * w : limit_; int limit = $limit == 0 ? h * w : $limit;
Matrix new_arr = Matrix(h, MatrixRow(w, limit)); Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = walls_; Matrix closed = $walls;
PointList starting_pixels; PointList starting_pixels;
PointList open_pixels; PointList open_pixels;
@ -66,7 +66,7 @@ void Map::make_paths() {
for(size_t counter = 0; counter < h * w; counter++) { for(size_t counter = 0; counter < h * w; counter++) {
size_t x = counter % w; size_t x = counter % w;
size_t y = counter / w; size_t y = counter / w;
if(input_map_[y][x] == 0) { if($input_map[y][x] == 0) {
new_arr[y][x] = 0; new_arr[y][x] = 0;
closed[y][x] = 1; closed[y][x] = 1;
starting_pixels.push_back({.x=x,.y=y}); starting_pixels.push_back({.x=x,.y=y});
@ -94,7 +94,7 @@ void Map::make_paths() {
new_arr[sp.y][sp.x] = counter; new_arr[sp.y][sp.x] = counter;
} }
paths_ = new_arr; $paths = new_arr;
} }
void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) { void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
@ -104,10 +104,10 @@ void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
dbc::pre("h out of bounds", h <= height()); dbc::pre("h out of bounds", h <= height());
for(size_t y = origin_y; y < origin_y + h; ++y) { for(size_t y = origin_y; y < origin_y + h; ++y) {
dbc::check(y < walls_.size(), "y is out of bounds"); dbc::check(y < $walls.size(), "y is out of bounds");
for(size_t x = origin_x; x < origin_x + w; ++x) { for(size_t x = origin_x; x < origin_x + w; ++x) {
dbc::check(x < walls_[y].size(), "x is out of bounds"); dbc::check(x < $walls[y].size(), "x is out of bounds");
walls_[y][x] = INV_SPACE; $walls[y][x] = INV_SPACE;
} }
} }
} }
@ -117,13 +117,13 @@ inline int make_split(Room &cur, bool horiz) {
int min = dimension / 4; int min = dimension / 4;
int max = dimension - min; int max = dimension - min;
std::uniform_int_distribution<int> rand_dim(min, max); std::uniform_int_distribution<int> rand_dim(min, max);
return rand_dim(g_generator); return rand_dim($GENERATOR);
} }
void Map::partition_map(Room &cur, int depth) { void Map::partition_map(Room &cur, int depth) {
if(cur.width >= 5 && cur.width <= 10 && if(cur.width >= 5 && cur.width <= 10 &&
cur.height >= 5 && cur.height <= 10) { cur.height >= 5 && cur.height <= 10) {
rooms_.push_back(cur); $rooms.push_back(cur);
return; return;
} }
@ -158,7 +158,7 @@ void Map::partition_map(Room &cur, int depth) {
} }
void Map::place_rooms(Room &cur) { void Map::place_rooms(Room &cur) {
for(auto &cur : rooms_) { for(auto &cur : $rooms) {
cur.x += 2; cur.x += 2;
cur.y += 2; cur.y += 2;
cur.width -= 4; cur.width -= 4;
@ -178,7 +178,7 @@ bool Map::neighbors(Point &out, bool greater) {
}}; }};
int zero_i = -1; int zero_i = -1;
int cur = paths_[out.y][out.x]; int cur = $paths[out.y][out.x];
if(cur == 1000) { if(cur == 1000) {
// BUG: sometimes the generation clips a door and we // BUG: sometimes the generation clips a door and we
@ -188,7 +188,7 @@ bool Map::neighbors(Point &out, bool greater) {
for(int i = 0; i < 4; ++i) { for(int i = 0; i < 4; ++i) {
Point dir = dirs[i]; Point dir = dirs[i];
int diff = inmap(dir.x, dir.y) ? cur - paths_[dir.y][dir.x] : -1000; int diff = inmap(dir.x, dir.y) ? cur - $paths[dir.y][dir.x] : -1000;
if(diff == 1) { if(diff == 1) {
out = {.x=dir.x, .y=dir.y}; out = {.x=dir.x, .y=dir.y};
@ -211,8 +211,8 @@ bool Map::inmap(size_t x, size_t y) {
} }
void Map::set_door(Room &room, int value) { void Map::set_door(Room &room, int value) {
walls_[room.entry.y][room.entry.x] = value; $walls[room.entry.y][room.entry.x] = value;
walls_[room.exit.y][room.exit.x] = value; $walls[room.exit.y][room.exit.x] = value;
} }
void rand_side(Room &room, Point &door) { void rand_side(Room &room, Point &door) {
@ -220,22 +220,22 @@ void rand_side(Room &room, Point &door) {
std::uniform_int_distribution<int> rand_x(0, room.width - 1); std::uniform_int_distribution<int> rand_x(0, room.width - 1);
std::uniform_int_distribution<int> rand_y(0, room.height - 1); std::uniform_int_distribution<int> rand_y(0, room.height - 1);
switch(rand_side(g_generator)) { switch(rand_side($GENERATOR)) {
case 0: // north case 0: // north
door.x = room.x + rand_x(g_generator); door.x = room.x + rand_x($GENERATOR);
door.y = room.y-1; door.y = room.y-1;
break; break;
case 1: // south case 1: // south
door.x = room.x + rand_x(g_generator); door.x = room.x + rand_x($GENERATOR);
door.y = room.y + room.height; door.y = room.y + room.height;
break; break;
case 2: // east case 2: // east
door.x = room.x + room.width; door.x = room.x + room.width;
door.y = room.y + rand_y(g_generator); door.y = room.y + rand_y($GENERATOR);
break; break;
case 3: // west case 3: // west
door.x = room.x - 1; door.x = room.x - 1;
door.y = room.y + rand_y(g_generator); door.y = room.y + rand_y($GENERATOR);
break; break;
default: default:
dbc::sentinel("impossible side"); dbc::sentinel("impossible side");
@ -249,15 +249,15 @@ void Map::add_door(Room &room) {
bool Map::walk(Point &src, Point &target) { bool Map::walk(Point &src, Point &target) {
// this sets the target for the path // this sets the target for the path
dbc::check(input_map_[target.y][target.x] == 0, "target point not set to 0"); dbc::check($input_map[target.y][target.x] == 0, "target point not set to 0");
walls_[src.y][src.x] = INV_WALL; $walls[src.y][src.x] = INV_WALL;
walls_[target.y][target.x] = INV_WALL; $walls[target.y][target.x] = INV_WALL;
// for the walk this needs to be walls since it's inverted? // for the walk this needs to be walls since it's inverted?
dbc::check(walls_[src.y][src.x] == INV_WALL, dbc::check($walls[src.y][src.x] == INV_WALL,
"src room has a wall at exit door"); "src room has a wall at exit door");
dbc::check(walls_[target.y][target.x] == INV_WALL, dbc::check($walls[target.y][target.x] == INV_WALL,
"target room has a wall at entry door"); "target room has a wall at entry door");
make_paths(); make_paths();
@ -266,11 +266,11 @@ bool Map::walk(Point &src, Point &target) {
int count = 0; int count = 0;
do { do {
walls_[out.y][out.x] = INV_SPACE; $walls[out.y][out.x] = INV_SPACE;
found = neighbors(out, true); found = neighbors(out, true);
if(paths_[out.y][out.x] == 0) { if($paths[out.y][out.x] == 0) {
walls_[out.y][out.x] = INV_SPACE; $walls[out.y][out.x] = INV_SPACE;
return true; return true;
} }
} while(found && out.x > 0 && out.y > 0 && ++count < 100); } while(found && out.x > 0 && out.y > 0 && ++count < 100);
@ -279,17 +279,17 @@ bool Map::walk(Point &src, Point &target) {
} }
void Map::set_target(Point &at, int value) { void Map::set_target(Point &at, int value) {
input_map_[at.y][at.x] = 0; $input_map[at.y][at.x] = 0;
} }
void Map::clear_target(Point &at) { void Map::clear_target(Point &at) {
input_map_[at.y][at.x] = 1; $input_map[at.y][at.x] = 1;
} }
Point Map::place_entity(size_t room_index) { Point Map::place_entity(size_t room_index) {
dbc::check(room_index < rooms_.size(), "room_index is out of bounds, not enough rooms"); dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
Room &start = rooms_[room_index]; Room &start = $rooms[room_index];
return {start.x+1, start.y+1}; return {start.x+1, start.y+1};
} }
@ -304,9 +304,9 @@ void Map::generate() {
partition_map(root, 10); partition_map(root, 10);
place_rooms(root); place_rooms(root);
for(size_t i = 0; i < rooms_.size() - 1; i++) { for(size_t i = 0; i < $rooms.size() - 1; i++) {
Room &src = rooms_[i]; Room &src = $rooms[i];
Room &target = rooms_[i+1]; Room &target = $rooms[i+1];
set_target(target.entry); set_target(target.entry);
bool found = walk(src.exit, target.entry); bool found = walk(src.exit, target.entry);
if(!found) { if(!found) {
@ -315,8 +315,8 @@ void Map::generate() {
clear_target(target.entry); clear_target(target.entry);
} }
Room &src = rooms_.back(); Room &src = $rooms.back();
Room &target = rooms_.front(); Room &target = $rooms.front();
set_target(target.entry); set_target(target.entry);
walk(src.exit, target.entry); walk(src.exit, target.entry);
@ -324,17 +324,17 @@ void Map::generate() {
for(size_t y = 0; y < height(); ++y) { for(size_t y = 0; y < height(); ++y) {
for(size_t x = 0; x < width(); ++x) { for(size_t x = 0; x < width(); ++x) {
walls_[y][x] = !walls_[y][x]; $walls[y][x] = !$walls[y][x];
} }
} }
} }
bool Map::iswall(size_t x, size_t y) { bool Map::iswall(size_t x, size_t y) {
return walls_[y][x] == WALL_VALUE; return $walls[y][x] == WALL_VALUE;
} }
void Map::dump() { void Map::dump() {
dump_map("PATHS", paths_); dump_map("PATHS", $paths);
dump_map("WALLS", walls_); dump_map("WALLS", $walls);
dump_map("INPUT", input_map_); dump_map("INPUT", $input_map);
} }

@ -34,17 +34,17 @@ void dump_map(const std::string &msg, Matrix &map);
void add_neighbors(Matrix &closed, size_t j, size_t i); void add_neighbors(Matrix &closed, size_t j, size_t i);
class Map { class Map {
Matrix input_map_; Matrix $input_map;
Matrix walls_; Matrix $walls;
Matrix paths_; Matrix $paths;
std::vector<Room> rooms_; std::vector<Room> $rooms;
int limit_ = 0; int $limit = 0;
public: public:
// make explicit // make explicit
Map(Matrix input_map, Matrix walls_map, int limit) : Map(Matrix input_map, Matrix walls_map, int limit) :
input_map_(input_map), $input_map(input_map),
walls_(walls_map), limit_(limit) { $walls(walls_map), $limit(limit) {
} }
// make random // make random
@ -53,18 +53,18 @@ public:
// disable copying // disable copying
Map(Map &map) = delete; Map(Map &map) = delete;
Matrix& paths() { return paths_; } Matrix& paths() { return $paths; }
Matrix& input_map() { return input_map_; } Matrix& input_map() { return $input_map; }
Matrix& walls() { return walls_; } Matrix& walls() { return $walls; }
int limit() { return limit_; } int limit() { return $limit; }
size_t width() { return walls_[0].size(); } size_t width() { return $walls[0].size(); }
size_t height() { return walls_.size(); } size_t height() { return $walls.size(); }
Room &room(size_t at) { Room &room(size_t at) {
return rooms_[at]; return $rooms[at];
} }
size_t room_count() { size_t room_count() {
return rooms_.size(); return $rooms.size();
} }
void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height); void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height);

@ -17,7 +17,7 @@ enum class MyEvent {
class MyFSM : public DeadSimpleFSM<MyState, MyEvent> { class MyFSM : public DeadSimpleFSM<MyState, MyEvent> {
public: public:
void event(MyEvent ev, string data="") { void event(MyEvent ev, string data="") {
switch(_state) { switch($state) {
FSM_STATE(MyState, START, ev); FSM_STATE(MyState, START, ev);
FSM_STATE(MyState, RUNNING, ev, data); FSM_STATE(MyState, RUNNING, ev, data);
FSM_STATE(MyState, END, ev); FSM_STATE(MyState, END, ev);

Loading…
Cancel
Save