#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 // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element #include // for Render #include // for ftxui #include #include #include #include #include #include #include "dbc.hpp" #include "gui.hpp" #include "rand.hpp" #include "components.hpp" #include "systems.hpp" using std::string; using namespace fmt; using namespace std::chrono_literals; using namespace ftxui; using namespace Components; std::array VALUES{ sf::Color{1, 4, 2}, // black sf::Color{9, 29, 16}, // dark dark sf::Color{14, 50, 26}, // dark mid sf::Color{0, 109, 44}, // dark light sf::Color{63, 171, 92}, // mid sf::Color{161, 217, 155}, // light dark sf::Color{199, 233, 192}, // light mid sf::Color{229, 245, 224}, // light light sf::Color{255, 255, 255}, // white sf::Color::Transparent, // white }; sf::Color GUI::color(int val) { return VALUES[size_t(val)]; } sf::Color GUI::color(Value val) { return VALUES[size_t(val)]; } GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y), $canvas(VIEW_PORT_X * 2, VIEW_PORT_Y * 4), $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $screen(SCREEN_X, SCREEN_Y), $map_screen(VIEW_PORT_X, VIEW_PORT_Y) { int res = $hit_buf.loadFromFile("./assets/hit.wav"); dbc::check(res, "failed to load hit.wav"); $hit_sound.setBuffer($hit_buf); $font.loadFromFile("./assets/text.otf"); $ui_text.setFont($font); $ui_text.setPosition(0,0); $ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setFillColor(color(Value::LIGHT_LIGHT)); $game_map.generate(); } void GUI::create_renderer() { auto player = $world.get(); $map_view = Renderer([&] { System::draw_map($world, $game_map, $canvas, VIEW_PORT_X, VIEW_PORT_Y); return canvas($canvas); }); $document = Renderer([&, player]{ const auto& player_combat = $world.component(player.entity); return hbox({ hflow( vbox( text(format("HP: {}", player_combat.hp)) | border, text($status_text) | border ) | xflex_grow ), separator(), hbox(), }); }); } bool GUI::handle_events() { sf::Event event; bool event_happened = false; while($window.pollEvent(event)) { if(event.type == sf::Event::Closed) { $window.close(); } else if(event.type == sf::Event::KeyPressed) { auto player = $world.get(); auto& player_motion = $world.component(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; } } } return event_happened; } void GUI::run_systems() { auto player = $world.get(); System::enemy_pathing($world, $game_map, player); System::motion($world, $game_map); System::combat($world, player); } void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { if(clear) $window.clear(); std::string screenout = $screen.ToString(); std::string map_screenout = $map_screen.ToString(); std::wstring main_screen_utf8 = $converter.from_bytes(screenout); $ui_text.setString(main_screen_utf8); $window.draw($ui_text); std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout); sf::Texture ftext = $font.getTexture(MAP_FONT_SIZE); float y = 0.0f; float x = GAME_MAP_POS; const float line_spacing = $font.getLineSpacing(MAP_FONT_SIZE); for(size_t i = 0; i < map_screen_utf8.size(); i++) { wchar_t tile = map_screen_utf8[i]; sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false); sf::Sprite sprite(ftext); sprite.setTextureRect(glyph.textureRect); sprite.setPosition({x, y}); if(tile == L'█') { sprite.setColor(sf::Color(100,100,100)); } else if(tile == L'☺') { sprite.setColor(sf::Color::Blue); } else if(tile == L'Ω') { sprite.setColor(sf::Color::Red); } else if(tile == L'·') { sprite.setColor(color(Value::DARK_MID)); } else if(tile == L'\r') { continue; // skip these, just windows junk } else if(tile == L'\n') { // newline y += line_spacing; x = GAME_MAP_POS; continue; } else { sprite.setColor(color(Value::MID)); } $window.draw(sprite); x += glyph.advance; } $window.display(); } void GUI::shake() { $hit_sound.play(); 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 draw_screen(true, x, y); std::this_thread::sleep_for(1ms); } } void GUI::configure_world() { dbc::check($game_map.room_count() > 1, "not enough rooms in map."); // configure a player as a fact of the world Player player{$world.entity()}; $world.set(player); $world.assign(player.entity, {$game_map.place_entity(0)}); $world.assign(player.entity, {0, 0}); $world.assign(player.entity, {100, 10}); $world.assign(player.entity, {PLAYER_TILE}); auto enemy = $world.entity(); $world.assign(enemy, {$game_map.place_entity(1)}); $world.assign(enemy, {0,0}); $world.assign(enemy, {20, 10}); $world.assign(enemy, {ENEMY_TILE}); auto enemy2 = $world.entity(); $world.assign(enemy2, {$game_map.place_entity(2)}); $world.assign(enemy2, {0,0}); $world.assign(enemy2, {20, 10}); $world.assign(enemy2, {"*"}); auto gold = $world.entity(); $world.assign(gold, {$game_map.place_entity($game_map.room_count() - 1)}); $world.assign(gold, {100}); $world.assign(gold, {"$"}); } void GUI::render_scene() { Render($map_screen, $map_view->Render()); Render($screen, $document->Render()); draw_screen(); } int GUI::main() { configure_world(); create_renderer(); run_systems(); while($window.isOpen()) { render_scene(); if(handle_events()) { run_systems(); } std::this_thread::sleep_for(10ms); } return 0; }