diff --git a/gui.cpp b/gui.cpp index 055722b..46fda91 100644 --- a/gui.cpp +++ b/gui.cpp @@ -20,7 +20,6 @@ #include "dbc.hpp" #include "gui.hpp" #include "rand.hpp" -#include "components.hpp" #include "systems.hpp" #include "collider.hpp" #include "events.hpp" @@ -59,11 +58,16 @@ GUI::GUI() : $map_screen(0,0), $view_port{0,0}, $map_font_size(BASE_MAP_FONT_SIZE), - $line_spacing(0) + $line_spacing(0), + $sounds("./assets"), + $log({{"Welcome to the game!"}}) { + // this needs a config file soon $font.loadFromFile("./assets/text.otf"); resize_map(BASE_MAP_FONT_SIZE); + $sounds.load("hit", "hit.wav"); + $ui_text.setFont($font); $ui_text.setPosition(0,0); $ui_text.setCharacterSize(UI_FONT_SIZE); @@ -82,12 +86,10 @@ void GUI::create_renderer() { $document = Renderer([&, player]{ const auto& player_combat = $world.get(player.entity); - const auto& log = $world.get_the(); - $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; std::vector log_list; - for(auto msg : log.messages) { + for(auto msg : $log.messages) { log_list.push_back(text(msg)); } auto log_box = vbox(log_list) | yflex_grow | border; @@ -107,12 +109,8 @@ void GUI::create_renderer() { }); } -bool GUI::handle_events() { - sf::Event event; - bool event_happened = false; - auto& log = $world.get_the(); +void GUI::handle_world_events() { auto player = $world.get_the(); - auto sounds = $world.get_the(); while($world.has_event()) { auto [evt, entity] = $world.recv(); @@ -121,29 +119,35 @@ bool GUI::handle_events() { auto combat = $world.get(entity); if(entity == player.entity) { - log.log(format("Enemy HIT YOU, you have {} HP!", combat.hp)); - sounds.play("hit"); + $log.log(format("Enemy HIT YOU, you have {} HP!", combat.hp)); + $sounds.play("hit"); shake(); } else { - log.log(format("You HIT enemy, they have {} HP!", combat.hp)); - sounds.play("hit"); + $log.log(format("You HIT enemy, they have {} HP!", combat.hp)); + $sounds.play("hit"); shake(); } } break; case GUIEvent::MISS: if(entity == player.entity) { - log.log("You MISSED the enemy."); + $log.log("You MISSED the enemy."); } else { - log.log("Enemy MISSED YOU."); + $log.log("Enemy MISSED YOU."); } break; case GUIEvent::DEAD: - log.log("--- ENEMY DEAD!"); + $log.log("--- ENEMY DEAD!"); break; default: - log.log(format("INVALID EVENT! {},{}", evt, entity)); + $log.log(format("INVALID EVENT! {},{}", evt, entity)); } } +} + +bool GUI::handle_ui_events() { + sf::Event event; + bool event_happened = false; + auto player = $world.get_the(); while($window.pollEvent(event)) { if(event.type == sf::Event::Closed) { @@ -293,8 +297,8 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { void GUI::shake() { for(int i = 0; i < 10; ++i) { - int x = Random::uniform(-20,20); - int y = Random::uniform(-20,20); + 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); @@ -302,9 +306,7 @@ void GUI::shake() { } void GUI::configure_world() { - SoundManager sounds("./assets"); - sounds.load("hit", "hit.wav"); - $world.set_the(sounds); + // this sets up the gui event system $world.set_the(GUIEvent::START); dbc::check($game_map.room_count() > 1, "not enough rooms in map."); @@ -312,9 +314,6 @@ void GUI::configure_world() { Player player{$world.entity()}; $world.set_the(player); - ActionLog log{{"Welcome to the game!"}}; - $world.set_the(log); - spatial_map collider; $world.set_the(collider); @@ -360,9 +359,11 @@ int GUI::main() { while($window.isOpen()) { render_scene(); - if(handle_events()) { + if(handle_ui_events()) { run_systems(); } + + handle_world_events(); std::this_thread::sleep_for(10ms); } diff --git a/gui.hpp b/gui.hpp index d2a6462..3510f53 100644 --- a/gui.hpp +++ b/gui.hpp @@ -12,6 +12,7 @@ #include #include "map.hpp" #include "dinkyecs.hpp" +#include "components.hpp" #include "sound.hpp" using std::string; @@ -54,6 +55,8 @@ class GUI { int $map_font_size; sf::Glyph $base_glyph; float $line_spacing; + SoundManager $sounds; + Components::ActionLog $log; public: GUI(); @@ -64,7 +67,8 @@ public: sf::Color color(int val); void create_renderer(); void render_scene(); - bool handle_events(); + bool handle_ui_events(); + void handle_world_events(); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void shake(); void configure_world(); diff --git a/sound.cpp b/sound.cpp index fe4e029..571e6f2 100644 --- a/sound.cpp +++ b/sound.cpp @@ -16,7 +16,7 @@ void SoundManager::load(const std::string name, const std::string sound_path) { dbc::check(fs::exists(full_path), format("sound file {} does not exist", sound_path)); // create the buffer and keep in the buffer map - SoundPair *pair = new SoundPair(); + std::shared_ptr pair = std::make_shared(); $sounds[name] = pair; bool good = pair->buffer.loadFromFile(full_path.string()); @@ -31,13 +31,13 @@ void SoundManager::load(const std::string name, const std::string sound_path) { void SoundManager::play(const std::string name) { dbc::check($sounds.contains(name), format("sound {} is not loaded in map", name)); // get the sound from the sound map - SoundPair *pair = $sounds.at(name); + auto pair = $sounds.at(name); // play it pair->sound.play(); } void SoundManager::playAt(const std::string name, float x, float y, float z) { - SoundPair *pair = $sounds.at(name); + auto pair = $sounds.at(name); pair->sound.setPosition(x, y, z); pair->sound.play(); } diff --git a/sound.hpp b/sound.hpp index 2eb5f4e..23bff94 100644 --- a/sound.hpp +++ b/sound.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include @@ -11,7 +12,7 @@ struct SoundPair { struct SoundManager { std::filesystem::path $base_path; - std::unordered_map $sounds; + std::unordered_map > $sounds; SoundManager(std::string base_path);