diff --git a/assets/config.json b/assets/config.json index ea2a63c..0fdf7b4 100644 --- a/assets/config.json +++ b/assets/config.json @@ -19,6 +19,7 @@ "cinqueda": "assets/cinqueda_1-256.png", "left_gui": "assets/left_gui.png", "blood_splatter": "assets/blood_splatter-256.png", + "trash_button": "assets/trash_button.png", "hairy_spider": "assets/hairy_spider-256.png" }, "enemy": { diff --git a/combat_ui.cpp b/combat_ui.cpp index d795aea..d0a7c0a 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -9,22 +9,31 @@ namespace gui { $gui.position(RAY_VIEW_X, RAY_VIEW_HEIGHT, RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT); $gui.layout( "[*%(100,150)button_attack1 | *%(100,150)button_attack2 | *%(100,150)button_attack3 | *%(100,150)button_heal]" - "[ >.%(100,50)label_hp | *%.(200,50)bar_hp | _ ]"); - render(); + "[ >.%(100,50)label_hp | *%.(100,50)bar_hp | _ ]"); } - void CombatUI::render() { + void CombatUI::render(TexturePack& textures) { auto& world = $gui.world(); for(auto& [name, cell] : $gui.cells()) { - auto button = $gui.entity(name); - world.set(button, cell); - world.set(button, {}); - world.set(button, {100}); - world.set(button, {name}); + if(name.starts_with("button_")) { + auto button = $gui.entity(name); + world.set(button, cell); + world.set(button, {"trash_button"}); + world.set(button, {100}); + world.set(button, {name}); + } else if(name.starts_with("bar_")) { + auto meter = $gui.entity(name); + world.set(meter, cell); + world.set(meter, {}); + world.set(meter, {}); + } else { + // ignored, it's just space + $gui.entity(name); + } } - $gui.init(); + $gui.init(textures); } void CombatUI::draw(sf::RenderWindow& window) { diff --git a/combat_ui.hpp b/combat_ui.hpp index 59b70af..7fe3331 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -13,7 +13,7 @@ namespace gui { CombatUI(GameLevel level); - void render(); + void render(TexturePack& texture); void draw(sf::RenderWindow& window); void update_level(GameLevel &level) { $level = level; } void click(int x, int y); diff --git a/ecs_gui.cpp b/ecs_gui.cpp index 5986cd5..91ea706 100644 --- a/ecs_gui.cpp +++ b/ecs_gui.cpp @@ -20,7 +20,7 @@ DinkyECS::Entity GUECS::entity(std::string name) { return entity; } -void GUECS::init() { +void GUECS::init(TexturePack& textures) { $world.query([](const auto &, auto& cell, auto& rect) { rect.init(cell); }); @@ -28,6 +28,15 @@ void GUECS::init() { $world.query([this](const auto &, auto& cell, auto& text) { text.init(cell, $font); }); + + $world.query([&](const auto &, auto &cell, auto &sprite) { + auto sprite_texture = textures.get(sprite.name); + sprite.texture = sprite_texture.texture; + sprite.sprite = make_shared(*sprite.texture); + sprite.sprite->setPosition({float(cell.x + 5), float(cell.y + 5)}); + auto size = sprite.texture->getSize(); + sprite.sprite->setScale({float(cell.w - 10) / size.x, float(cell.h - 10) / size.y}); + }); } void GUECS::render(sf::RenderWindow& window) { @@ -44,6 +53,10 @@ void GUECS::render(sf::RenderWindow& window) { window.draw(*rect.shape); }); + $world.query([&](const auto &, const auto& sprite) { + window.draw(*sprite.sprite); + }); + $world.query([&](const auto &, const auto& text) { window.draw(*text.text); }); diff --git a/ecs_gui.hpp b/ecs_gui.hpp index daf572c..7c724f6 100644 --- a/ecs_gui.hpp +++ b/ecs_gui.hpp @@ -5,6 +5,7 @@ #include #include #include +#include "texture.hpp" using std::shared_ptr, std::make_shared; @@ -28,6 +29,12 @@ struct Clickable { int event = 0; }; +struct Sprite { + std::string name; + std::shared_ptr sprite = nullptr; + std::shared_ptr texture = nullptr; +}; + struct Rectangle { shared_ptr shape = nullptr; @@ -71,7 +78,7 @@ class GUECS { return $world; } - void init(); + void init(TexturePack& textures); void render(sf::RenderWindow& window); void mouse(sf::RenderWindow &window); }; diff --git a/gui.cpp b/gui.cpp index 6e8ceea..cc06e97 100644 --- a/gui.cpp +++ b/gui.cpp @@ -51,6 +51,8 @@ namespace gui { $rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y); $rayview.position_camera($player.x + 0.5, $player.y + 0.5); + $combat_view.render($textures); + $status_view.create_render(); $status_view.log("Welcome to the game!"); @@ -254,10 +256,6 @@ namespace gui { case KEY::R: $stats.reset(); break; - case KEY::G: - $combat_view.render(); - event(Event::TICK); - break; case KEY::M: event(Event::MAP_OPEN); break; diff --git a/raycaster.cpp b/raycaster.cpp index 672d4e5..47eadd6 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -353,7 +353,7 @@ void Raycaster::set_level(GameLevel level) { // player doesn't need a sprite if(player.entity == ent) return; fmt::println("entity {} will have sprite named {}", ent, sprite.name); - auto sprite_txt = $textures.sprite_textures.at(sprite.name); + auto sprite_txt = $textures.get(sprite.name); $sprites.try_emplace(ent, sprite_txt); }); } diff --git a/tests/ecs_gui.cpp b/tests/ecs_gui.cpp index 5bdb877..720c04b 100644 --- a/tests/ecs_gui.cpp +++ b/tests/ecs_gui.cpp @@ -2,10 +2,14 @@ #include #include "constants.hpp" #include "ecs_gui.hpp" +#include "texture.hpp" TEST_CASE("prototype one gui", "[ecs-gui]") { GUECS gui; + TexturePack textures; + textures.load_sprites(); + gui.position(0, 0, 1000, 500); gui.layout("[test1|test2|test3][test4|_|test5]"); @@ -18,7 +22,7 @@ TEST_CASE("prototype one gui", "[ecs-gui]") { world.set(button, {name}); } - gui.init(); + gui.init(textures); // at this point it's mostly ready but I'd need to render it to a window real quick sf::RenderWindow window; diff --git a/texture.cpp b/texture.cpp index 5213d0b..8c83ea0 100644 --- a/texture.cpp +++ b/texture.cpp @@ -64,3 +64,7 @@ matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) { return result; } + +SpriteTexture TexturePack::get(std::string name) { + return sprite_textures.at(name); +} diff --git a/texture.hpp b/texture.hpp index 9b01985..87ad670 100644 --- a/texture.hpp +++ b/texture.hpp @@ -23,6 +23,7 @@ struct TexturePack { void load_tiles(); void load_sprites(); + SpriteTexture get(std::string name); sf::Image load_image(std::string filename); const uint32_t* get_surface(size_t num);