diff --git a/autowalker.cpp b/autowalker.cpp index 84fde14..0a8569c 100644 --- a/autowalker.cpp +++ b/autowalker.cpp @@ -9,7 +9,7 @@ int number_left() { GameDB::current_world()->query( [&](const auto ent, auto&, auto&) { - if(ent != GameDB::current().player) { + if(ent != GameDB::current_level().player) { count++; } }); @@ -19,15 +19,15 @@ int number_left() { template Pathing compute_paths() { - auto& walls_original = GameDB::current().map->$walls; + auto& walls_original = GameDB::current_level().map->$walls; auto walls_copy = walls_original; Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)}; - GameDB::current().world->query( + GameDB::current_level().world->query( [&](const auto ent, auto& position) { - if(ent != GameDB::current().player) { - if(GameDB::current().world->has(ent)) { + if(ent != GameDB::current_level().player) { + if(GameDB::current_level().world->has(ent)) { paths.set_target(position.location); } else { // this will mark that spot as a wall so we don't path there temporarily @@ -110,7 +110,7 @@ bool Autowalker::path_player(Pathing& paths, Point& target_out) { } } - if(!GameDB::current().map->can_move(target_out)) { + if(!GameDB::current_level().map->can_move(target_out)) { path_fail(paths.$paths, target_out); return false; } diff --git a/game_level.cpp b/game_level.cpp index caa1905..63a6509 100644 --- a/game_level.cpp +++ b/game_level.cpp @@ -41,17 +41,6 @@ namespace GameDB { shared_ptr LDB; bool initialized = false; - void init() { - components::init(); - textures::init(); - - if(!initialized) { - LDB = make_shared(); - initialized = true; - new_level(NULL); - } - } - LevelScaling scale_level() { return { INITIAL_MAP_W + int(LDB->current_level * 2), @@ -59,28 +48,6 @@ namespace GameDB { }; } - shared_ptr current_world() { - dbc::check(initialized, "Forgot to call GameDB::init()"); - return current().world; - } - - shared_ptr create_bossfight() { - dbc::check(initialized, "Forgot to call GameDB::init()"); - auto prev_world = current_world(); - dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null."); - auto world = clone_load_world(prev_world); - auto& config = prev_world->get_the(); - - // BUG: the jank is too strong here - auto boss_names = config.bosses.keys(); - auto& level_name = boss_names[LDB->current_level % boss_names.size()]; - auto& boss_data = config.bosses[level_name]; - - auto boss_id = world->entity(); - components::configure_entity(*world, boss_id, boss_data["components"]); - - return make_shared(world, boss_id); - } size_t new_level(std::shared_ptr prev_world) { dbc::check(initialized, "Forgot to call GameDB::init()"); @@ -106,42 +73,55 @@ namespace GameDB { return index; } - Level& create_level() { - dbc::log("current_level"); - size_t level = new_level(current_world()); - dbc::check(level == LDB->current_level + 1, "new level index is wrong"); - auto& the_level = next(); - dbc::check(level == LDB->current_level, "level didn't update?!"); - return the_level; - } + void init() { + components::init(); + textures::init(); - Level &next() { - dbc::check(initialized, "Forgot to call GameDB::init()"); - dbc::check(LDB->current_level < LDB->levels.size(), "attempt to get next level when at end"); - LDB->current_level++; - return LDB->levels.at(LDB->current_level); + if(!initialized) { + LDB = make_shared(); + initialized = true; + new_level(NULL); + } } - Level &previous() { + shared_ptr current_world() { dbc::check(initialized, "Forgot to call GameDB::init()"); - dbc::check(LDB->current_level > 0, "attempt to go to previous level when at 0"); - LDB->current_level--; - return LDB->levels.at(LDB->current_level); + return current_level().world; } - Level ¤t() { + shared_ptr create_bossfight() { dbc::check(initialized, "Forgot to call GameDB::init()"); - return LDB->levels.at(LDB->current_level); + auto prev_world = current_world(); + dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null."); + auto world = clone_load_world(prev_world); + auto& config = prev_world->get_the(); + + // BUG: the jank is too strong here + auto boss_names = config.bosses.keys(); + auto& level_name = boss_names[LDB->current_level % boss_names.size()]; + auto& boss_data = config.bosses[level_name]; + + auto boss_id = world->entity(); + components::configure_entity(*world, boss_id, boss_data["components"]); + + return make_shared(world, boss_id); } - size_t current_index() { + + Level& create_level() { dbc::check(initialized, "Forgot to call GameDB::init()"); - return LDB->current_level; + dbc::check(LDB->current_level < LDB->levels.size(), "attempt to get next level when at end"); + + size_t level = new_level(current_world()); + dbc::check(level == LDB->current_level + 1, "new level index is wrong"); + + LDB->current_level++; + return LDB->levels.at(LDB->current_level); } - Level &get(size_t index) { + Level ¤t_level() { dbc::check(initialized, "Forgot to call GameDB::init()"); - return LDB->levels.at(index); + return LDB->levels.at(LDB->current_level); } components::Position& player_position() { @@ -153,6 +133,6 @@ namespace GameDB { DinkyECS::Entity the_player() { dbc::check(initialized, "Forgot to call GameDB::init()"); - return current().player; + return current_level().player; } } diff --git a/game_level.hpp b/game_level.hpp index 82cf82c..6db3470 100644 --- a/game_level.hpp +++ b/game_level.hpp @@ -24,16 +24,11 @@ namespace GameDB { }; std::shared_ptr create_bossfight(); - size_t new_level(std::shared_ptr prev_world); Level& create_level(); void init(); - Level &next(); - Level &previous(); - Level ¤t(); - size_t current_index(); + Level ¤t_level(); std::shared_ptr current_world(); - Level &get(size_t index); components::Position& player_position(); DinkyECS::Entity the_player(); } diff --git a/gui/debug_ui.cpp b/gui/debug_ui.cpp index 98e4b6c..50db359 100644 --- a/gui/debug_ui.cpp +++ b/gui/debug_ui.cpp @@ -46,7 +46,7 @@ namespace gui { void DebugUI::render(sf::RenderWindow& window) { if(active) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto player = level.world->get_the(); auto player_combat = level.world->get(player.entity); auto map = level.map; @@ -76,7 +76,7 @@ namespace gui { active = !active; if(active) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); // it's on now, enable things auto player = level.world->get_the(); auto& player_combat = level.world->get(player.entity); diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 48b8166..179f9ee 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -267,7 +267,7 @@ namespace gui { } void FSM::try_move(int dir, bool strafe) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); using enum State; // prevent moving into occupied space Point move_to = $main_ui.plan_move(dir, strafe); diff --git a/gui/main_ui.cpp b/gui/main_ui.cpp index fe7f353..3a3b8da 100644 --- a/gui/main_ui.cpp +++ b/gui/main_ui.cpp @@ -32,7 +32,7 @@ namespace gui { } DinkyECS::Entity MainUI::camera_aim() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); // what happens if there's two things at that spot if(level.collision->something_there($rayview->aiming_at)) { return level.collision->get($rayview->aiming_at); @@ -98,7 +98,7 @@ namespace gui { } void MainUI::update_level() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& player_position = GameDB::player_position(); auto player = player_position.location; diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index d0f65ce..ebe37fa 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -112,7 +112,7 @@ namespace gui { } bool StatusUI::place_slot(guecs::Entity gui_id, DinkyECS::Entity world_entity) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& slot_name = $gui.name_for(gui_id); auto& inventory = level.world->get(level.player); diff --git a/systems.cpp b/systems.cpp index fb7f6f8..26997d1 100644 --- a/systems.cpp +++ b/systems.cpp @@ -33,7 +33,7 @@ void System::set_position(World& world, SpatialMap& collision, Entity entity, Po } void System::lighting() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& light = *level.lights; auto& world = *level.world; auto& map = *level.map; @@ -57,7 +57,7 @@ void System::lighting() { } void System::generate_paths() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); const auto &player_pos = GameDB::player_position(); level.map->set_target(player_pos.location); @@ -65,7 +65,7 @@ void System::generate_paths() { } void System::enemy_ai_initialize() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& map = *level.map; @@ -93,7 +93,7 @@ void System::enemy_ai_initialize() { } void System::enemy_pathing() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& map = *level.map; const auto &player_pos = GameDB::player_position(); @@ -135,7 +135,7 @@ inline void move_entity(SpatialMap &collider, Map &game_map, Position &position, } void System::motion() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); level.world->query( [&](auto ent, auto &position, auto &motion) { // don't process entities that don't move @@ -146,7 +146,7 @@ void System::motion() { } void System::distribute_loot(Position target_pos) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& config = world.get_the(); int inventory_count = Random::uniform(0, 3); @@ -171,7 +171,7 @@ void System::distribute_loot(Position target_pos) { } void System::death() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto player = world.get_the(); std::vector dead_things; @@ -229,7 +229,7 @@ inline void animate_entity(World &world, Entity entity) { } void System::combat(int attack_id) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& collider = *level.collision; auto& world = *level.world; auto& the_belt = world.get_the(); @@ -286,7 +286,7 @@ void System::combat(int attack_id) { void System::collision() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& collider = *level.collision; auto& world = *level.world; const auto& player_pos = GameDB::player_position(); @@ -319,7 +319,7 @@ void System::collision() { * from the world for say, putting into a container or inventory. */ void System::remove_from_world(Entity entity) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& item_pos = level.world->get(entity); level.collision->remove(item_pos.location, entity); // if you don't do this you get the bug that you can pickup @@ -328,7 +328,7 @@ void System::remove_from_world(Entity entity) { } void System::pickup() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& collision = *level.collision; auto pos = GameDB::player_position(); @@ -393,7 +393,7 @@ void System::device(World &world, Entity actor, Entity item) { } void System::plan_motion(Position move_to) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& player_pos = GameDB::player_position(); player_pos.aiming_at = move_to.aiming_at; @@ -405,7 +405,7 @@ void System::plan_motion(Position move_to) { void System::player_status() { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& combat = level.world->get(level.player); float percent = float(combat.hp) / float(combat.max_hp); @@ -450,7 +450,7 @@ Entity System::spawn_item(World& world, const std::string& name) { } void System::drop_item(Entity item) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& map = *level.map; auto player_pos = GameDB::player_position(); @@ -498,7 +498,7 @@ void System::remove_from_container(Entity cont_id, const std::string& slot_id) { void System::inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); dbc::check(a_name != b_name, "Attempt to inventory swap the same slot, you should check this and avoid calling me."); auto& inventory = level.world->get(container_id); @@ -516,7 +516,7 @@ bool System::inventory_occupied(Entity container_id, const std::string& name) { void System::draw_map(Matrix& grid, EntityGrid& entity_map) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; Map &map = *level.map; Matrix &fow = level.lights->$fow; @@ -606,7 +606,7 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture } bool System::use_item(const string& slot_name) { - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& world = *level.world; auto& inventory = world.get(level.player); auto& player_combat = world.get(level.player); diff --git a/tests/lighting.cpp b/tests/lighting.cpp index d72cb3c..54617c8 100644 --- a/tests/lighting.cpp +++ b/tests/lighting.cpp @@ -11,7 +11,7 @@ using namespace lighting; TEST_CASE("lighting a map works", "[lighting]") { GameDB::init(); - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& map = *level.map; Point light1, light2; diff --git a/tests/map.cpp b/tests/map.cpp index e669901..7875481 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -18,7 +18,7 @@ json load_test_data(const string &fname) { TEST_CASE("camera control", "[map]") { GameDB::init(); - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); auto& map = *level.map; Point center = map.center_camera({10,10}, 5, 5); @@ -81,7 +81,7 @@ TEST_CASE("dijkstra algo test", "[map]") { TEST_CASE("map image test", "[map]") { GameDB::init(); - auto& level = GameDB::current(); + auto& level = GameDB::current_level(); Matrix map_tiles = matrix::make(7,7); EntityGrid entity_map; diff --git a/tests/matrix.cpp b/tests/matrix.cpp index aa4d9bb..ff6e9eb 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -17,7 +17,7 @@ using matrix::Matrix; std::shared_ptr make_map() { GameDB::init(); - return GameDB::current().map; + return GameDB::current_level().map; } TEST_CASE("basic matrix iterator", "[matrix]") {