Cleaned up the maze placement so that I can have mazes without rooms and with other features.
This commit is contained in:
parent
37715f05a5
commit
20f03731e5
6 changed files with 34 additions and 25 deletions
|
@ -61,7 +61,7 @@ DinkyECS::Entity LevelManager::spawn_enemy(std::string named) {
|
||||||
|
|
||||||
WorldBuilder builder(*level.map, $components);
|
WorldBuilder builder(*level.map, $components);
|
||||||
|
|
||||||
auto entity_id = builder.configure_entity_in_map(*level.world, entity_data, 0);
|
auto entity_id = builder.configure_entity_in_room(*level.world, entity_data, 0);
|
||||||
|
|
||||||
auto& entity_pos = level.world->get<Position>(entity_id);
|
auto& entity_pos = level.world->get<Position>(entity_id);
|
||||||
auto player_pos = level.world->get<Position>(level.player);
|
auto player_pos = level.world->get<Position>(level.player);
|
||||||
|
|
25
map.cpp
25
map.cpp
|
@ -45,19 +45,26 @@ void Map::clear_target(const Point &at) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::place_entity(size_t room_index, Point &out) {
|
bool Map::place_entity(size_t room_index, Point &out) {
|
||||||
dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
|
if($rooms.size() == 0) {
|
||||||
|
dbc::log("fucking dead end?");
|
||||||
|
out = $dead_ends.at(room_index % $dead_ends.size());
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
dbc::log("fucking fuckng fuck fuck");
|
||||||
|
dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
|
||||||
|
|
||||||
Room &start = $rooms[room_index];
|
Room &start = $rooms.at(room_index);
|
||||||
|
|
||||||
for(matrix::rando_rect it{$walls, start.x, start.y, start.width, start.height}; it.next();) {
|
for(matrix::rando_rect it{$walls, start.x, start.y, start.width, start.height}; it.next();) {
|
||||||
if(!iswall(it.x, it.y)) {
|
if(!iswall(it.x, it.y)) {
|
||||||
out.x = it.x;
|
out.x = it.x;
|
||||||
out.y = it.y;
|
out.y = it.y;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::iswall(size_t x, size_t y) {
|
bool Map::iswall(size_t x, size_t y) {
|
||||||
|
|
2
maze.cpp
2
maze.cpp
|
@ -93,7 +93,7 @@ inline std::pair<Point, Point> find_coord(Matrix& maze) {
|
||||||
dbc::sentinel("failed to find coord?");
|
dbc::sentinel("failed to find coord?");
|
||||||
}
|
}
|
||||||
|
|
||||||
void maze::randomize_rooms(std::vector<Room>& rooms_out, std::vector<Point> maybe_here) {
|
void maze::randomize_rooms(std::vector<Room>& rooms_out, std::vector<Point>& maybe_here) {
|
||||||
dbc::check(maybe_here.size() >= 2, "must have at least two possible points to place rooms");
|
dbc::check(maybe_here.size() >= 2, "must have at least two possible points to place rooms");
|
||||||
|
|
||||||
while(rooms_out.size() < 2) {
|
while(rooms_out.size() < 2) {
|
||||||
|
|
2
maze.hpp
2
maze.hpp
|
@ -8,7 +8,7 @@ namespace maze {
|
||||||
|
|
||||||
void hunt_and_kill(Matrix& maze, std::vector<Room>& rooms, std::vector<Point>& dead_ends);
|
void hunt_and_kill(Matrix& maze, std::vector<Room>& rooms, std::vector<Point>& dead_ends);
|
||||||
|
|
||||||
void randomize_rooms(std::vector<Room>& rooms_out, std::vector<Point> maybe_here);
|
void randomize_rooms(std::vector<Room>& rooms_out, std::vector<Point>& maybe_here);
|
||||||
|
|
||||||
void inner_donut(Matrix& maze, float outer_rad, float inner_rad);
|
void inner_donut(Matrix& maze, float outer_rad, float inner_rad);
|
||||||
void inner_box(Matrix& map, size_t outer_size, size_t inner_size);
|
void inner_box(Matrix& map, size_t outer_size, size_t inner_size);
|
||||||
|
|
|
@ -11,14 +11,9 @@ using namespace fmt;
|
||||||
using namespace components;
|
using namespace components;
|
||||||
|
|
||||||
void WorldBuilder::generate_map() {
|
void WorldBuilder::generate_map() {
|
||||||
// run it once to find dead ends
|
maze::init($map.$walls);
|
||||||
maze::hunt_and_kill($map.$walls, $map.$rooms, $map.$dead_ends);
|
maze::hunt_and_kill($map.$walls, $map.$rooms, $map.$dead_ends);
|
||||||
|
|
||||||
// randomize rooms based on dead ends
|
|
||||||
maze::randomize_rooms($map.$rooms, $map.$dead_ends);
|
maze::randomize_rooms($map.$rooms, $map.$dead_ends);
|
||||||
|
|
||||||
// run it again to create the final map with rooms
|
|
||||||
// NOTE: hund_and_kill is responsible for clearing the map correctly
|
|
||||||
maze::hunt_and_kill($map.$walls, $map.$rooms, $map.$dead_ends);
|
maze::hunt_and_kill($map.$walls, $map.$rooms, $map.$dead_ends);
|
||||||
|
|
||||||
$map.expand();
|
$map.expand();
|
||||||
|
@ -26,6 +21,7 @@ void WorldBuilder::generate_map() {
|
||||||
}
|
}
|
||||||
|
|
||||||
DinkyECS::Entity WorldBuilder::configure_entity_in_map(DinkyECS::World &world, json &entity_data, Point pos_out) {
|
DinkyECS::Entity WorldBuilder::configure_entity_in_map(DinkyECS::World &world, json &entity_data, Point pos_out) {
|
||||||
|
dbc::log(">>>>>>>>>>> ENTER");
|
||||||
auto item = world.entity();
|
auto item = world.entity();
|
||||||
world.set<Position>(item, {pos_out.x+1, pos_out.y+1});
|
world.set<Position>(item, {pos_out.x+1, pos_out.y+1});
|
||||||
|
|
||||||
|
@ -36,14 +32,19 @@ DinkyECS::Entity WorldBuilder::configure_entity_in_map(DinkyECS::World &world, j
|
||||||
if(entity_data.contains("components")) {
|
if(entity_data.contains("components")) {
|
||||||
components::configure_entity($components, world, item, entity_data["components"]);
|
components::configure_entity($components, world, item, entity_data["components"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbc::log("<<<<<<<<<<<<< EXIT");
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
DinkyECS::Entity WorldBuilder::configure_entity_in_map(DinkyECS::World &world, json &entity_data, int in_room) {
|
DinkyECS::Entity WorldBuilder::configure_entity_in_room(DinkyECS::World &world, json &entity_data, int in_room) {
|
||||||
Point pos_out;
|
Point pos_out;
|
||||||
|
dbc::log("is it configure_entity_in_map's fault?");
|
||||||
bool placed = $map.place_entity(in_room, pos_out);
|
bool placed = $map.place_entity(in_room, pos_out);
|
||||||
dbc::check(placed, "failed to randomly place item in room");
|
dbc::check(placed, "failed to randomly place item in room");
|
||||||
return configure_entity_in_map(world, entity_data, pos_out);
|
auto entity = configure_entity_in_map(world, entity_data, pos_out);
|
||||||
|
dbc::log("<<<<<<<<<<<<<<<<<<<<<<<<<<< leaving configure_entity_in_room");
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,10 +82,10 @@ inline json& random_entity_data(GameConfig& config, json& gen_config) {
|
||||||
void WorldBuilder::randomize_entities(DinkyECS::World &world, GameConfig &config) {
|
void WorldBuilder::randomize_entities(DinkyECS::World &world, GameConfig &config) {
|
||||||
auto& gen_config = config.game["worldgen"];
|
auto& gen_config = config.game["worldgen"];
|
||||||
|
|
||||||
for(size_t room_num = $map.room_count() - 1; room_num > 0; room_num--) {
|
for(int room_num = $map.room_count() - 1; room_num > 0; room_num--) {
|
||||||
// pass that to the config as it'll be a generic json
|
// pass that to the config as it'll be a generic json
|
||||||
auto& entity_data = random_entity_data(config, gen_config);
|
auto& entity_data = random_entity_data(config, gen_config);
|
||||||
configure_entity_in_map(world, entity_data, room_num);
|
configure_entity_in_room(world, entity_data, room_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& at : $map.$dead_ends) {
|
for(auto& at : $map.$dead_ends) {
|
||||||
|
@ -97,7 +98,7 @@ void WorldBuilder::place_stairs(DinkyECS::World& world, GameConfig& config) {
|
||||||
auto& device_config = config.devices.json();
|
auto& device_config = config.devices.json();
|
||||||
auto entity_data = device_config["STAIRS_DOWN"];
|
auto entity_data = device_config["STAIRS_DOWN"];
|
||||||
int last_room = $map.room_count() - 1;
|
int last_room = $map.room_count() - 1;
|
||||||
configure_entity_in_map(world, entity_data, last_room);
|
configure_entity_in_room(world, entity_data, last_room);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldBuilder::configure_starting_items(DinkyECS::World &world) {
|
void WorldBuilder::configure_starting_items(DinkyECS::World &world) {
|
||||||
|
@ -117,12 +118,13 @@ void WorldBuilder::place_entities(DinkyECS::World &world) {
|
||||||
if(world.has_the<Player>()) {
|
if(world.has_the<Player>()) {
|
||||||
auto& player = world.get_the<Player>();
|
auto& player = world.get_the<Player>();
|
||||||
Point pos_out;
|
Point pos_out;
|
||||||
|
dbc::log("or is it in place_entities placing the player?");
|
||||||
bool placed = $map.place_entity(0, pos_out);
|
bool placed = $map.place_entity(0, pos_out);
|
||||||
dbc::check(placed, "failed to randomly place item in room");
|
dbc::check(placed, "failed to randomly place item in room");
|
||||||
world.set<Position>(player.entity, {pos_out.x+1, pos_out.y+1});
|
world.set<Position>(player.entity, {pos_out.x+1, pos_out.y+1});
|
||||||
} else {
|
} else {
|
||||||
auto player_data = config.enemies["PLAYER_TILE"];
|
auto player_data = config.enemies["PLAYER_TILE"];
|
||||||
auto player_ent = configure_entity_in_map(world, player_data, 0);
|
auto player_ent = configure_entity_in_room(world, player_data, 0);
|
||||||
|
|
||||||
// configure player in the world
|
// configure player in the world
|
||||||
Player player{player_ent};
|
Player player{player_ent};
|
||||||
|
|
|
@ -18,7 +18,7 @@ class WorldBuilder {
|
||||||
|
|
||||||
DinkyECS::Entity configure_entity_in_map(DinkyECS::World &world, nlohmann::json &entity_data, Point pos);
|
DinkyECS::Entity configure_entity_in_map(DinkyECS::World &world, nlohmann::json &entity_data, Point pos);
|
||||||
|
|
||||||
DinkyECS::Entity configure_entity_in_map(DinkyECS::World &world, nlohmann::json &entity_data, int in_room);
|
DinkyECS::Entity configure_entity_in_room(DinkyECS::World &world, nlohmann::json &entity_data, int in_room);
|
||||||
|
|
||||||
void place_entities(DinkyECS::World &world);
|
void place_entities(DinkyECS::World &world);
|
||||||
void generate(DinkyECS::World &world);
|
void generate(DinkyECS::World &world);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue