#include "systems.hpp" #include #include #include #include "rand.hpp" #include "spatialmap.hpp" #include "dbc.hpp" #include "lights.hpp" #include "inventory.hpp" #include "events.hpp" #include "sound.hpp" using std::string; using namespace fmt; using namespace components; using lighting::LightSource; using ftxui::Color; void System::lighting(GameLevel &level) { auto &light = *level.lights; auto &world = *level.world; auto &map = *level.map; light.reset_light(); world.query([&](auto, auto &position) { light.set_light_target(position.location); }); light.path_light(map.walls()); world.query([&](auto, auto &position, auto &lightsource) { light.render_light(lightsource, position.location); }); } void System::enemy_pathing(GameLevel &level) { auto &world = *level.world; auto &map = *level.map; auto player = world.get_the(); // TODO: this will be on each enemy not a global thing const auto &player_position = world.get(player.entity); map.set_target(player_position.location); map.make_paths(); world.query([&](auto ent, auto &position, auto &motion) { if(ent != player.entity) { dbc::check(world.has(ent), "enemy is missing config"); const auto &config = world.get(ent); Point out = position.location; // copy if(map.distance(out) < config.hearing_distance) { map.neighbors(out, motion.random); motion = { int(out.x - position.location.x), int(out.y - position.location.y)}; } } }); map.clear_target(player_position.location); } void System::init_positions(DinkyECS::World &world, SpatialMap &collider) { // BUG: instead of separate things maybe just one // BUG: Collision component that references what is collide world.query([&](auto ent, auto &pos) { if(world.has(ent)) { const auto& combat = world.get(ent); if(!combat.dead) { collider.insert(pos.location, ent); } } else { collider.insert(pos.location, ent); } }); } inline void move_entity(SpatialMap &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) { Point move_to = { position.location.x + motion.dx, position.location.y + motion.dy }; motion = {0,0}; // clear it after getting it // it's a wall, skip if(!game_map.can_move(move_to)) return; // there's collision skip if(collider.occupied(move_to)) return; // all good, do the move collider.move(position.location, move_to, ent); position.location = move_to; } void System::motion(GameLevel &level) { auto &map = *level.map; auto &world = *level.world; auto &collider = *level.collision; world.query([&](auto ent, auto &position, auto &motion) { // don't process entities that don't move if(motion.dx != 0 || motion.dy != 0) { move_entity(collider, map, position, motion, ent); } }); } void System::death(GameLevel &level, components::ComponentMap& components) { auto &world = *level.world; auto player = world.get_the(); auto& config = world.get_the(); std::vector dead_things; world.query([&](auto ent, auto &combat) { // bring out yer dead if(combat.hp <= 0 && !combat.dead) { combat.dead = true; if(ent != player.entity) { // we won't change out the player's components later dead_things.push_back(ent); } // we need to send this event for everything that dies world.send(Events::GUI::DEATH, ent, {}); } }); // this goes through everything that died and changes them to a gravestone // NOTE: this could be a separate system but also could be a function in // components:: for(auto ent : dead_things) { // remove their enemy setting world.remove(ent); world.remove(ent); world.remove(ent); world.remove(ent); if(auto snd = world.get_if(ent)) { sound::stop(snd->attack); sound::play(snd->death); } auto entity_data = config.items["GRAVE_STONE"]; components::configure_entity(components, world, ent, entity_data["components"]); if(entity_data["inventory_count"] > 0) { // right here use a std::any that's already converted instead? world.set(ent, {entity_data["inventory_count"], entity_data}); } } } void System::combat(GameLevel &level) { auto &collider = *level.collision; auto &world = *level.world; auto player = world.get_the(); const auto& player_position = world.get(player.entity); auto& player_combat = world.get(player.entity); // this is guaranteed to not return the given position auto [found, nearby] = collider.neighbors(player_position.location); if(found) { for(auto entity : nearby) { if(world.has(entity)) { auto& enemy_combat = world.get(entity); Events::Combat result { player_combat.attack(enemy_combat), enemy_combat.attack(player_combat) }; if(world.has(entity)) { auto& animation = world.get(entity); animation.play(); } if(auto snd = world.get_if(entity)) { sound::play(snd->attack); } world.send(Events::GUI::COMBAT, entity, result); } } } } void System::collision(GameLevel &level) { auto &collider = *level.collision; auto &world = *level.world; auto player = world.get_the(); const auto& player_position = world.get(player.entity); // this is guaranteed to not return the given position auto [found, nearby] = collider.neighbors(player_position.location); int combat_count = 0; // BUG: this logic is garbage, needs a refactor for(auto entity : nearby) { if(world.has(entity)) { auto combat = world.get(entity); if(!combat.dead) { combat_count++; world.send(Events::GUI::COMBAT_START, entity, entity); } } else if(world.has(entity)) { auto item = world.get(entity); auto& item_pos = world.get(entity); auto& inventory = world.get(player.entity); if(world.has(entity)) { inventory.add(item); world.remove(entity); } if(world.has(entity)) { inventory.add(item); world.remove(entity); } if(world.has(entity)) { auto &loot = world.get(entity); inventory.gold += loot.amount; world.remove(entity); } if(world.has(entity)) { inventory.add(item); world.remove(entity); } if(auto snd = world.get_if(entity)) { sound::play(snd->attack); } collider.remove(item_pos.location); world.remove(entity); world.remove(entity); world.send(Events::GUI::LOOT, entity, item); } else if(world.has(entity)) { System::device(world, player.entity, entity); } else { println("UNKNOWN COLLISION TYPE {}", entity); } } if(combat_count == 0) { world.send(Events::GUI::NO_NEIGHBORS, player.entity, player.entity); } } void System::device(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::Entity item) { auto& device = world.get(item); for(auto event : device.events) { fmt::println("Device event received {}", event); if(event == "Events::GUI::STAIRS_DOWN") { world.send(Events::GUI::STAIRS_DOWN, actor, device); } else { dbc::log(fmt::format("EVENT IGNORED {}", event)); } } println("entity {} INTERACTED WITH DEVICE {}", actor, item); } void System::plan_motion(DinkyECS::World& world, Point move_to) { auto& player = world.get_the(); auto& player_position = world.get(player.entity); auto& motion = world.get(player.entity); motion.dx = move_to.x - player_position.location.x; motion.dy = move_to.y - player_position.location.y; } /* * This one is called inside the MapViewUI very often so * just avoide GameMap unlike the others. */ void System::draw_entities(DinkyECS::World &world, Map &map, const Matrix &lights, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) { auto &tiles = map.tiles(); world.query([&](auto, auto &pos, auto &tile) { if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x && pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) { Point loc = map.map_to_camera(pos.location, cam_orig); float light_value = lights[pos.location.y][pos.location.x] * PERCENT; const Tile& cell = tiles.at(pos.location.x, pos.location.y); // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing canvas.DrawText(loc.x*2, loc.y*4, tile.display, [tile, light_value, cell](auto &pixel) { pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value); pixel.background_color = Color::HSV(cell.background[0], cell.background[1], cell.background[2] * light_value); }); } }); }