#include // for operator""s, chrono_literals #include // for cout, ostream #include #include // for allocator, shared_ptr #include // for string, operator<< #include // for sleep_for #include #include #include // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element #include // for Render #include // for ftxui #include #include #include #include "dbc.hpp" #include "gui.hpp" #include "rand.hpp" #include "systems.hpp" #include "events.hpp" #include "render.hpp" #include "save.hpp" using std::string; using namespace fmt; using namespace std::chrono_literals; using namespace ftxui; using namespace components; GUI::GUI(DinkyECS::World &world, Map& game_map) : $game_map(game_map), $log({{"Welcome to the game!"}}), $view_port{0,0}, $status_screen(SCREEN_X, SCREEN_Y), $map_screen(0,0), $world(world), $sounds("./assets"), $renderer() { // this needs a config file soon // $sounds.load("ambient", "ambient_sound.mp3"); $sounds.load("loot_gold", "loot_gold.mp3"); $sounds.load("combat_player_hit", "combat_player_hit.mp3"); $sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3"); $sounds.load("combat_miss", "combat_miss.mp3"); resize_map(MAX_FONT_SIZE); } void GUI::resize_map(int new_size) { if($renderer.resize_map(new_size, $view_port)) { // set canvas to best size $canvas = Canvas($view_port.x * 2, $view_port.y * 4); $map_screen = Screen($view_port.x, $view_port.y); } } void GUI::save_world() { $log.log("Game saved!"); save::to_file("./savefile.world", $world, $game_map); } void GUI::create_renderer() { Terminal::SetColorSupport(Terminal::Color::TrueColor); auto player = $world.get_the(); $map_view = Renderer([&] { System::draw_map($world, $game_map, $canvas, $view_port.x, $view_port.y); return canvas($canvas); }); $prompt = Renderer([&] { return hbox({hflow(vbox(text("HELLO!")))}) | border; }); $status_ui = Renderer([&, player]{ const auto& player_combat = $world.get(player.entity); const auto& inventory = $world.get(player.entity); $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; std::vector log_list; for(auto msg : $log.messages) { log_list.push_back(text(msg)); } auto log_box = vbox(log_list) | yflex_grow | border; return hbox({ hflow( vbox( text(format("HP: {: >3} GOLD: {: >3}", player_combat.hp, inventory.gold)) | border, text($status_text) | border, separator(), log_box ) | flex_grow ), separator(), hbox(), }); }); } void GUI::handle_world_events() { using eGUI = Events::GUI; auto player = $world.get_the(); while($world.has_event()) { auto [evt, entity, data] = $world.recv(); switch(evt) { case eGUI::COMBAT: { auto &damage = std::any_cast(data); auto enemy_combat = $world.get(entity); if(damage.enemy_did > 0) { $log.log(format("Enemy HIT YOU for {} damage!", damage.enemy_did)); $log.log(format("-- Enemy has {} HP left.", enemy_combat.hp)); $sounds.play("combat_enemy_hit"); shake(); } else { $log.log("Enemy MISSED YOU."); } if(damage.player_did > 0) { $log.log(format("You HIT enemy for {} damage!", damage.player_did)); $sounds.play("combat_player_hit"); } else { $sounds.play("combat_miss"); $log.log("You MISSED the enemy."); } } break; case eGUI::LOOT: { auto &loot = std::any_cast(data); auto inventory = $world.get(player.entity); $sounds.play("loot_gold"); $log.log(format("You found {} gold. You have {} now.", loot.amount, inventory.gold)); } break; default: $log.log(format("INVALID EVENT! {},{}", evt, entity)); } } } bool GUI::handle_ui_events() { bool event_happened = false; sf::Event event; auto player = $world.get_the(); int map_font_size = $renderer.font_size(); while($renderer.poll_event(event)) { if(event.type == sf::Event::Closed) { $renderer.close(); } else if(event.type == sf::Event::KeyPressed) { auto& player_motion = $world.get(player.entity); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { player_motion.dx = -1; event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { player_motion.dx = 1; event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { player_motion.dy = -1; event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { player_motion.dy = 1; event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) { resize_map(map_font_size + 10); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen)) { resize_map(map_font_size - 10); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { save_world(); } } } return event_happened; } void GUI::run_systems() { auto player = $world.get_the(); System::enemy_pathing($world, $game_map, player); System::motion($world, $game_map); System::collision($world, player); System::death($world); } void GUI::shake() { for(int i = 0; i < 10; ++i) { int x = Random::uniform(-10,10); int y = Random::uniform(-10,10); // add x/y back to draw screen $renderer.draw_screen($map_screen, x, y); std::this_thread::sleep_for(1ms); } } void GUI::render_scene() { $renderer.clear(); $status_screen.Clear(); Render($status_screen, $status_ui->Render()); $renderer.draw_text_ui($status_screen, 0, 0); $map_screen.Clear(); Render($map_screen, $map_view->Render()); $renderer.draw_screen($map_screen); /* Screen prompt_screen(30,10); Render(prompt_screen, $prompt->Render()); $renderer.draw_text_ui(prompt_screen, 700, 300); */ $renderer.display(); } int GUI::main() { // $sounds.play("ambient"); create_renderer(); run_systems(); while($renderer.is_open()) { render_scene(); if(handle_ui_events()) { run_systems(); } handle_world_events(); std::this_thread::sleep_for(10ms); } return 0; }