Refactor the entity out.

main
Zed A. Shaw 2 months ago
parent cac7017563
commit a7f6357e12
  1. 37
      entity.cpp
  2. 33
      entity.hpp
  3. 59
      main.cpp
  4. 76
      map.cpp
  5. 30
      map.hpp
  6. 2
      meson.build

@ -0,0 +1,37 @@
#include "entity.hpp"
void Entity::move(Point loc) {
location = loc;
}
void Entity::event(EntityEvent ev) {
switch(_state) {
FSM_STATE(EntityState, START, ev);
FSM_STATE(EntityState, HUNTING, ev);
FSM_STATE(EntityState, DEAD, ev);
}
}
void Entity::START(EntityEvent ev) {
state(EntityState::HUNTING);
}
void Entity::HUNTING(EntityEvent ev) {
switch(ev) {
case EntityEvent::HIT:
hp -= damage;
break;
default:
state(EntityState::HUNTING);
}
if(hp <= 0) {
state(EntityState::DEAD);
} else {
state(EntityState::HUNTING);
}
}
void Entity::DEAD(EntityEvent ev) {
state(EntityState::DEAD);
}

@ -0,0 +1,33 @@
#pragma once
#include "fsm.hpp"
#include "map.hpp"
enum class EntityState {
START, HUNTING, DEAD
};
enum class EntityEvent {
GO, HIT
};
class Entity : public DeadSimpleFSM<EntityState, EntityEvent> {
public:
Point location;
int hp = 20;
int damage = 10;
Entity(Point loc) : location(loc) {
};
// disable copy
Entity(Entity &e) = delete;
void move(Point loc);
void event(EntityEvent ev);
// states
void START(EntityEvent ev);
void HUNTING(EntityEvent ev);
void DEAD(EntityEvent ev);
};

@ -27,6 +27,7 @@
#include <fmt/core.h> #include <fmt/core.h>
#include "fsm.hpp" #include "fsm.hpp"
#include "map.hpp" #include "map.hpp"
#include "entity.hpp"
#include "dbc.hpp" #include "dbc.hpp"
using std::string; using std::string;
@ -52,64 +53,6 @@ std::array<sf::Color, 10> VALUES{
sf::Color::Transparent, // white sf::Color::Transparent, // white
}; };
enum class EntityState {
START, HUNTING, DEAD
};
enum class EntityEvent {
GO, HIT
};
class Entity : public DeadSimpleFSM<EntityState, EntityEvent> {
public:
Point location;
int hp = 20;
int damage = 10;
Entity(Point loc) : location(loc) {
};
// disable copy
Entity(Entity &e) = delete;
void move(Point loc) {
location = loc;
}
void event(EntityEvent ev) {
switch(_state) {
FSM_STATE(EntityState, START, ev);
FSM_STATE(EntityState, HUNTING, ev);
FSM_STATE(EntityState, DEAD, ev);
}
}
void START(EntityEvent ev) {
state(EntityState::HUNTING);
}
void HUNTING(EntityEvent ev) {
switch(ev) {
case EntityEvent::HIT:
hp -= damage;
break;
default:
state(EntityState::HUNTING);
}
if(hp <= 0) {
state(EntityState::DEAD);
} else {
state(EntityState::HUNTING);
}
}
void DEAD(EntityEvent ev) {
state(EntityState::DEAD);
}
};
sf::SoundBuffer g_hit_buf; sf::SoundBuffer g_hit_buf;
sf::Sound g_hit_sound; sf::Sound g_hit_sound;

@ -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) : m_limit(1000) { Map::Map(size_t width, size_t height) : limit_(1000) {
m_walls = Matrix(height, MatrixRow(width, INV_WALL)); walls_ = Matrix(height, MatrixRow(width, INV_WALL));
m_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 = m_input_map.size(); size_t h = input_map_.size();
size_t w = m_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 = m_limit == 0 ? h * w : m_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 = m_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(m_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;
} }
m_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 < m_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 < m_walls[y].size(), "x is out of bounds"); dbc::check(x < walls_[y].size(), "x is out of bounds");
m_walls[y][x] = INV_SPACE; walls_[y][x] = INV_SPACE;
} }
} }
} }
@ -123,7 +123,7 @@ inline int make_split(Room &cur, bool horiz) {
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) {
m_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 : m_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 = m_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 - m_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) {
m_walls[room.entry.y][room.entry.x] = value; walls_[room.entry.y][room.entry.x] = value;
m_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) {
@ -249,26 +249,26 @@ 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(m_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");
m_walls[src.y][src.x] = INV_WALL; walls_[src.y][src.x] = INV_WALL;
m_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(m_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(m_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();
bool found = false; bool found = false;
Point out{src.x, src.y}; Point out{src.x, src.y};
do { do {
m_walls[out.y][out.x] = INV_SPACE; walls_[out.y][out.x] = INV_SPACE;
found = neighbors(out, true); found = neighbors(out, true);
if(m_paths[out.y][out.x] == 0) { if(paths_[out.y][out.x] == 0) {
m_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); } while(found && out.x > 0 && out.y > 0);
@ -277,11 +277,11 @@ bool Map::walk(Point &src, Point &target) {
} }
void Map::set_target(Point &at, int value) { void Map::set_target(Point &at, int value) {
m_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) {
m_input_map[at.y][at.x] = 1; input_map_[at.y][at.x] = 1;
} }
void Map::generate() { void Map::generate() {
@ -295,9 +295,9 @@ void Map::generate() {
partition_map(root, 10); partition_map(root, 10);
place_rooms(root); place_rooms(root);
for(size_t i = 0; i < m_rooms.size() - 1; i++) { for(size_t i = 0; i < rooms_.size() - 1; i++) {
Room &src = m_rooms[i]; Room &src = rooms_[i];
Room &target = m_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) {
@ -306,8 +306,8 @@ void Map::generate() {
clear_target(target.entry); clear_target(target.entry);
} }
Room &src = m_rooms.back(); Room &src = rooms_.back();
Room &target = m_rooms.front(); Room &target = rooms_.front();
set_target(target.entry); set_target(target.entry);
walk(src.exit, target.entry); walk(src.exit, target.entry);
@ -315,18 +315,18 @@ 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) {
m_walls[y][x] = !m_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 m_walls[y][x] == WALL_VALUE; return walls_[y][x] == WALL_VALUE;
} }
void Map::dump() { void Map::dump() {
dump_map("PATHS", m_paths); dump_map("PATHS", paths_);
dump_map("WALLS", m_walls); dump_map("WALLS", walls_);
dump_map("INPUT", m_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 m_input_map; Matrix input_map_;
Matrix m_walls; Matrix walls_;
Matrix m_paths; Matrix paths_;
std::vector<Room> m_rooms; std::vector<Room> rooms_;
int m_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) :
m_input_map(input_map), input_map_(input_map),
m_walls(walls_map), m_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 m_paths; } Matrix& paths() { return paths_; }
Matrix& input_map() { return m_input_map; } Matrix& input_map() { return input_map_; }
Matrix& walls() { return m_walls; } Matrix& walls() { return walls_; }
int limit() { return m_limit; } int limit() { return limit_; }
size_t width() { return m_walls[0].size(); } size_t width() { return walls_[0].size(); }
size_t height() { return m_walls.size(); } size_t height() { return walls_.size(); }
Room &room(size_t at) { Room &room(size_t at) {
return m_rooms[at]; return rooms_[at];
} }
size_t room_count() { size_t room_count() {
return m_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);

@ -16,6 +16,7 @@ dependencies = [catch2, fmt,
runtests = executable('runtests', [ runtests = executable('runtests', [
'dbc.cpp', 'dbc.cpp',
'map.cpp', 'map.cpp',
'entity.cpp',
'tests/fsm.cpp', 'tests/fsm.cpp',
'tests/dbc.cpp', 'tests/dbc.cpp',
'tests/map.cpp', 'tests/map.cpp',
@ -26,6 +27,7 @@ roguish = executable('roguish', [
'dbc.cpp', 'dbc.cpp',
'main.cpp', 'main.cpp',
'map.cpp', 'map.cpp',
'entity.cpp',
], ],
dependencies: dependencies) dependencies: dependencies)

Loading…
Cancel
Save