#include "systems.hpp" #include #include #include #include "rand.hpp" #include "collider.hpp" #include "events.hpp" #include "ftxui/screen/color.hpp" #include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor #include "dbc.hpp" using std::string; using namespace fmt; using namespace components; using ftxui::Color; void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) { // TODO: this will be on each enemy not a global thing const auto &config = world.get_the(); const auto &player_position = world.get(player.entity); game_map.set_target(player_position.location); game_map.make_paths(); world.query([&](const auto &ent, auto &position, auto &motion) { if(ent != player.entity) { Point out = position.location; // copy if(game_map.distance(out) < config.HEARING_DISTANCE) { game_map.neighbors(out, false); motion = { int(out.x - position.location.x), int(out.y - position.location.y)}; } } }); game_map.clear_target(player_position.location); } void System::init_positions(DinkyECS::World &world) { auto &collider = world.get_the(); world.query([&](const auto &ent, auto &pos, auto &combat) { if(!combat.dead) { collider.insert(pos.location, ent); } }); world.query([&](const auto &ent, auto &pos, auto &loot) { collider.insert(pos.location, ent); }); } inline void move_entity(spatial_map &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(DinkyECS::World &world, Map &game_map) { auto &collider = world.get_the(); world.query([&](const auto &ent, auto &position, auto &motion) { // don't process entities that don't move if(motion.dx != 0 || motion.dy != 0) { move_entity(collider, game_map, position, motion, ent); } }); } void System::death(DinkyECS::World &world) { auto &collider = world.get_the(); world.query([&](const auto &ent, auto &position, auto &combat) { // bring out yer dead if(combat.hp <= 0 && !combat.dead) { combat.dead = true; // take them out of collision map collider.remove(position.location); // remove their motion so they're dead world.remove(ent); } }); } void System::combat(DinkyECS::World &world, Player &player) { auto& collider = 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) { // save some keystrokes using eGUI = Events::GUI; for(auto entity : nearby) { if(world.has(entity)) { auto& enemy_combat = world.get(entity); int player_dmg = player_combat.attack(enemy_combat); if(player_dmg > 0) { world.send(eGUI::HIT, entity); } else { world.send(eGUI::MISS, entity); } if(enemy_combat.hp > 0) { int enemy_dmg = enemy_combat.attack(player_combat); if(enemy_dmg > 0) { world.send(eGUI::HIT, player.entity); } else { world.send(eGUI::MISS, player.entity); } } else { world.send(eGUI::DEAD, entity); } } else if(world.has(entity)) { world.send(eGUI::LOOT, entity); } else { println("UNKNOWN COLLISION TYPE {}", entity); } } } }; void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) { world.query([&](const auto &ent, 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 = game_map.map_to_camera(pos.location, cam_orig); // 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.chr, Color::RGB(255, 50, 50)); } }); } void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) { const auto& config = world.get_the(); const auto& player = world.get_the(); const auto& player_position = world.get(player.entity); Point start = game_map.center_camera(player_position.location, view_x, view_y); Matrix &walls = game_map.walls(); size_t end_x = std::min(view_x, game_map.width() - start.x); size_t end_y = std::min(view_y, game_map.height() - start.y); for(size_t x = 0; x < end_x; ++x) { for(size_t y = 0; y < end_y; ++y) { string tile = walls[start.y+y][start.x+x] == 1 ? config.WALL_TILE : config.FLOOR_TILE; // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing if(tile == config.WALL_TILE) { canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70)); } else { canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20)); } } } System::draw_entities(world, game_map, canvas, start, view_x, view_y); }