diff --git a/gui/fsm.cpp b/gui/fsm.cpp index d544be7..cb55847 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -73,7 +73,7 @@ namespace gui { void FSM::MOVING(Event ) { // this should be an optional that returns a point if(auto move_to = $main_ui.play_move()) { - System::plan_motion(Game::current(), *move_to); + System::plan_motion(*move_to); run_systems(); $main_ui.dirty(); state(State::IDLE); @@ -84,7 +84,7 @@ namespace gui { using enum Event; switch(ev) { case TICK: { - System::combat(Game::current(), $temp_attack_id); + System::combat($temp_attack_id); run_systems(); state(State::IN_COMBAT); } break; @@ -195,7 +195,7 @@ namespace gui { auto gui_id = std::any_cast(data); auto& slot_name = $status_ui.$gui.name_for(gui_id); - if(System::use_item(Game::current(), slot_name)) { + if(System::use_item(slot_name)) { $status_ui.update(); } } break; @@ -207,7 +207,7 @@ namespace gui { mouse_action({1 << guecs::ModBit::hover}); } break; case AIM_CLICK: - System::pickup(Game::current()); + System::pickup(); break; default: break; // ignore everything else @@ -362,9 +362,8 @@ namespace gui { event(Event::LOOT_OPEN); break; case KEY::Z: { - auto& level = Game::current(); auto& player_pos = Game::player_position(); - System::distribute_loot(level, {player_pos.aiming_at}); + System::distribute_loot({player_pos.aiming_at}); } break; case KEY::X: event(Event::STAIRS_DOWN); @@ -422,14 +421,13 @@ namespace gui { } void FSM::run_systems() { - auto& level = Game::current(); - System::generate_paths(level); - System::enemy_ai_initialize(level); - System::enemy_pathing(level); - System::collision(level); - System::motion(level); - System::lighting(level); - System::death(level); + System::generate_paths(); + System::enemy_ai_initialize(); + System::enemy_pathing(); + System::collision(); + System::motion(); + System::lighting(); + System::death(); } bool FSM::active() { @@ -504,7 +502,7 @@ namespace gui { event(Event::LOOT_OPEN); } break; case eGUI::HP_STATUS: - System::player_status(Game::current()); + System::player_status(); break; case eGUI::NEW_RITUAL: $combat_ui.init(); diff --git a/gui/fsm.hpp b/gui/fsm.hpp index 1f572f2..0d082f7 100644 --- a/gui/fsm.hpp +++ b/gui/fsm.hpp @@ -1,7 +1,6 @@ #pragma once #include "constants.hpp" -#include "levelmanager.hpp" #include "simplefsm.hpp" #include "gui/debug_ui.hpp" #include "gui/main_ui.hpp" diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index 2b9573f..22edc38 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -128,7 +128,7 @@ namespace gui { } void LootUI::drop_item(DinkyECS::Entity item_id) { - System::drop_item($level, item_id); + System::drop_item(item_id); update(); } @@ -137,14 +137,14 @@ namespace gui { } bool LootUI::occupied(guecs::Entity slot) { - return System::inventory_occupied($level, $target, $gui.name_for(slot)); + return System::inventory_occupied($target, $gui.name_for(slot)); } void LootUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) { if(gui_a != gui_b) { auto& a_name = $gui.name_for(gui_a); auto& b_name = $gui.name_for(gui_b); - System::inventory_swap($level, $target, a_name, b_name); + System::inventory_swap($target, a_name, b_name); } update(); diff --git a/gui/map_view.cpp b/gui/map_view.cpp index abac584..c1a189b 100644 --- a/gui/map_view.cpp +++ b/gui/map_view.cpp @@ -51,7 +51,7 @@ namespace gui { void MapViewUI::render(sf::RenderWindow &window, int compass_dir) { $gui.render(window); - System::draw_map($level, $map_tiles, $entity_map); + System::draw_map($map_tiles, $entity_map); System::render_map($map_tiles, $entity_map, *$map_render, compass_dir, $player_display); $map_sprite.setTexture($map_render->getTexture(), true); window.draw($map_sprite); diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index 56808af..8b989ab 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -127,7 +127,7 @@ namespace gui { } void StatusUI::drop_item(DinkyECS::Entity item_id) { - System::drop_item($level, item_id); + System::drop_item(item_id); update(); } @@ -142,7 +142,7 @@ namespace gui { if(gui_a != gui_b) { auto& a_name = $gui.name_for(gui_a); auto& b_name = $gui.name_for(gui_b); - System::inventory_swap($level, $level.player, a_name, b_name); + System::inventory_swap($level.player, a_name, b_name); } update(); @@ -150,6 +150,6 @@ namespace gui { bool StatusUI::occupied(guecs::Entity slot) { auto player = $level.world->get_the(); - return System::inventory_occupied($level, player.entity, $gui.name_for(slot)); + return System::inventory_occupied(player.entity, $gui.name_for(slot)); } } diff --git a/raycaster.cpp b/raycaster.cpp index af220ea..c652763 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -196,7 +196,7 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT; - shared_ptr effect = System::sprite_effect($level, rec.entity); + shared_ptr effect = System::sprite_effect(rec.entity); if(effect) { apply_sprite_effect(effect, sprite_width, sprite_height); diff --git a/systems.cpp b/systems.cpp index 9af40d1..a0210f4 100644 --- a/systems.cpp +++ b/systems.cpp @@ -17,6 +17,7 @@ #include "shaders.hpp" #include "inventory.hpp" #include "game_level.hpp" +#include "levelmanager.hpp" using std::string; using namespace fmt; @@ -32,10 +33,11 @@ void System::set_position(World& world, SpatialMap& collision, Entity entity, Po collision.insert(pos.location, entity, has_collision); } -void System::lighting(GameLevel &level) { - auto &light = *level.lights; - auto &world = *level.world; - auto &map = *level.map; +void System::lighting() { + auto& level = Game::current(); + auto& light = *level.lights; + auto& world = *level.world; + auto& map = *level.map; light.reset_light(); @@ -55,16 +57,18 @@ void System::lighting(GameLevel &level) { } -void System::generate_paths(GameLevel &level) { +void System::generate_paths() { + auto& level = Game::current(); const auto &player_pos = Game::player_position(); level.map->set_target(player_pos.location); level.map->make_paths(); } -void System::enemy_ai_initialize(GameLevel &level) { - auto &world = *level.world; - auto &map = *level.map; +void System::enemy_ai_initialize() { + auto& level = Game::current(); + auto& world = *level.world; + auto& map = *level.map; world.query([&](const auto ent, auto& pos, auto& config) { if(world.has(ent)) { @@ -89,9 +93,10 @@ void System::enemy_ai_initialize(GameLevel &level) { }); } -void System::enemy_pathing(GameLevel &level) { - auto &world = *level.world; - auto &map = *level.map; +void System::enemy_pathing() { + auto& level = Game::current(); + auto& world = *level.world; + auto& map = *level.map; const auto &player_pos = Game::player_position(); world.query([&](auto ent, auto &position, auto &motion) { @@ -130,7 +135,8 @@ inline void move_entity(SpatialMap &collider, Map &game_map, Position &position, position.location = move_to; } -void System::motion(GameLevel &level) { +void System::motion() { + auto& level = Game::current(); level.world->query( [&](auto ent, auto &position, auto &motion) { // don't process entities that don't move @@ -140,7 +146,8 @@ void System::motion(GameLevel &level) { }); } -void System::distribute_loot(GameLevel &level, Position target_pos) { +void System::distribute_loot(Position target_pos) { + auto& level = Game::current(); auto& world = *level.world; auto& config = world.get_the(); int inventory_count = Random::uniform(0, 3); @@ -164,8 +171,9 @@ void System::distribute_loot(GameLevel &level, Position target_pos) { level.world->send(Events::GUI::ENTITY_SPAWN, loot_entity, {}); } -void System::death(GameLevel &level) { - auto &world = *level.world; +void System::death() { + auto& level = Game::current(); + auto& world = *level.world; auto player = world.get_the(); std::vector dead_things; @@ -204,7 +212,7 @@ void System::death(GameLevel &level) { level.collision->remove(pos.location, ent); // distribute_loot is then responsible for putting something there - System::distribute_loot(level, pos); + System::distribute_loot(pos); world.destroy(ent); } @@ -221,9 +229,10 @@ inline void animate_entity(World &world, Entity entity) { } } -void System::combat(GameLevel &level, int attack_id) { - auto &collider = *level.collision; - auto &world = *level.world; +void System::combat(int attack_id) { + auto& level = Game::current(); + auto& collider = *level.collision; + auto& world = *level.world; auto& the_belt = world.get_the(); if(!the_belt.has(attack_id)) return; @@ -277,9 +286,10 @@ void System::combat(GameLevel &level, int attack_id) { } -void System::collision(GameLevel &level) { - auto &collider = *level.collision; - auto &world = *level.world; +void System::collision() { + auto& level = Game::current(); + auto& collider = *level.collision; + auto& world = *level.world; const auto& player_pos = Game::player_position(); // this is guaranteed to not return the given position @@ -309,7 +319,8 @@ void System::collision(GameLevel &level) { * This isn't for destroying something, but just removing it * from the world for say, putting into a container or inventory. */ -void System::remove_from_world(GameLevel &level, Entity entity) { +void System::remove_from_world(Entity entity) { + auto& level = Game::current(); 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 @@ -317,9 +328,10 @@ void System::remove_from_world(GameLevel &level, Entity entity) { level.world->remove(entity); } -void System::pickup(GameLevel &level) { - auto &world = *level.world; - auto &collision = *level.collision; +void System::pickup() { + auto& level = Game::current(); + auto& world = *level.world; + auto& collision = *level.collision; auto pos = Game::player_position(); if(!collision.something_there(pos.aiming_at)) return; @@ -337,7 +349,7 @@ void System::pickup(GameLevel &level) { // use spatial find to find an item with inventory... if(world.has(entity)) { // NOTE: this might need to be a separate system so that people can leave stuff alone - remove_from_world(level, entity); + remove_from_world(entity); if(world.has(entity)) { auto& pile = world.get(entity); @@ -381,7 +393,8 @@ void System::device(World &world, Entity actor, Entity item) { } } -void System::plan_motion(GameLevel& level, Position move_to) { +void System::plan_motion(Position move_to) { + auto& level = Game::current(); auto& player_pos = Game::player_position(); player_pos.aiming_at = move_to.aiming_at; @@ -392,7 +405,8 @@ void System::plan_motion(GameLevel& level, Position move_to) { } -void System::player_status(GameLevel &level) { +void System::player_status() { + auto& level = Game::current(); auto& combat = level.world->get(level.player); float percent = float(combat.hp) / float(combat.max_hp); @@ -409,15 +423,16 @@ void System::player_status(GameLevel &level) { } } -std::shared_ptr System::sprite_effect(GameLevel &level, Entity entity) { - if(level.world->has(entity)) { - auto& se = level.world->get(entity); +std::shared_ptr System::sprite_effect(Entity entity) { + auto world = Game::current_world(); + if(world->has(entity)) { + auto& se = world->get(entity); if(se.frames > 0) { se.frames--; return se.effect; } else { - level.world->remove(entity); + world->remove(entity); return nullptr; } } else { @@ -435,11 +450,11 @@ Entity System::spawn_item(World& world, const std::string& name) { return item_id; } -void System::drop_item(GameLevel& level, Entity item) { +void System::drop_item(Entity item) { + auto& level = Game::current(); auto& world = *level.world; auto& map = *level.map; - - auto player_pos = world.get(level.player); + auto player_pos = Game::player_position(); dbc::check(map.can_move(player_pos.location), "impossible, the player can't be in a wall"); @@ -481,7 +496,8 @@ void System::remove_from_container(World& world, Entity cont_id, const std::stri } -void System::inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name) { +void System::inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name) { + auto& level = Game::current(); 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); @@ -491,14 +507,16 @@ void System::inventory_swap(GameLevel &level, Entity container_id, const std::st inventory.swap(a_ent, b_ent); } -bool System::inventory_occupied(GameLevel& level, Entity container_id, const std::string& name) { - auto& inventory = level.world->get(container_id); +bool System::inventory_occupied(Entity container_id, const std::string& name) { + auto world = Game::current_world(); + auto& inventory = world->get(container_id); return inventory.has(name); } -void System::draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map) { - World &world = *level.world; +void System::draw_map(Matrix& grid, EntityGrid& entity_map) { + auto& level = Game::current(); + auto& world = *level.world; Map &map = *level.map; Matrix &fow = level.lights->$fow; size_t view_x = matrix::width(grid) - 1; @@ -586,7 +604,8 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture render.display(); } -bool System::use_item(GameLevel& level, const string& slot_name) { +bool System::use_item(const string& slot_name) { + auto& level = Game::current(); auto& world = *level.world; auto& inventory = world.get(level.player); auto& player_combat = world.get(level.player); diff --git a/systems.hpp b/systems.hpp index 151d33b..e74d21a 100644 --- a/systems.hpp +++ b/systems.hpp @@ -1,45 +1,46 @@ #pragma once #include "components.hpp" -#include "levelmanager.hpp" #include +#include "map.hpp" +#include "spatialmap.hpp" namespace System { using namespace components; using namespace DinkyECS; - using std::string; + using std::string, matrix::Matrix; - void lighting(GameLevel &level); - void motion(GameLevel &level); - void collision(GameLevel &level); - void death(GameLevel &level); - void generate_paths(GameLevel &level); - void enemy_pathing(GameLevel &level); - void enemy_ai_initialize(GameLevel &level); + void lighting(); + void motion(); + void collision(); + void death(); + void generate_paths(); + void enemy_pathing(); + void enemy_ai_initialize(); void device(World &world, Entity actor, Entity item); - void plan_motion(GameLevel& level, Position move_to); + void plan_motion(Position move_to); Entity spawn_item(World& world, const string& name); - void drop_item(GameLevel& level, Entity item); + void drop_item(Entity item); - void enemy_ai(GameLevel &level); - void combat(GameLevel& level, int attack_id); + void enemy_ai(); + void combat(int attack_id); - std::shared_ptr sprite_effect(GameLevel &level, Entity entity); - void player_status(GameLevel &level); - void distribute_loot(GameLevel &level, Position target_pos); + std::shared_ptr sprite_effect(Entity entity); + void player_status(); + void distribute_loot(Position target_pos); - void pickup(GameLevel &level); + void pickup(); bool place_in_container(World& world, Entity cont_id, const string& name, Entity world_entity); void remove_from_container(World& world, Entity cont_id, const std::string& name); - void remove_from_world(GameLevel &level, Entity entity); - void inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name); - bool inventory_occupied(GameLevel& level, Entity container_id, const std::string& name); + void remove_from_world(Entity entity); + void inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name); + bool inventory_occupied(Entity container_id, const std::string& name); - void draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map); + void draw_map(Matrix& grid, EntityGrid& entity_map); void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display); void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos); - bool use_item(GameLevel& level, const std::string& slot_name); + bool use_item(const std::string& slot_name); } diff --git a/tests/map.cpp b/tests/map.cpp index 3c571af..8543784 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -99,7 +99,7 @@ TEST_CASE("map image test", "[map]") { for(matrix::each_row it{level.map->walls()}; it.next();) { player_pos.location.x = it.x; player_pos.location.y = it.y; - System::draw_map(level, map_tiles, entity_map); + System::draw_map(map_tiles, entity_map); System::render_map(map_tiles, entity_map, *render, 2, player_display); #ifdef TEST_RENDER