diff --git a/animation.cpp b/animation.cpp index 2e5856d..bb63322 100644 --- a/animation.cpp +++ b/animation.cpp @@ -84,6 +84,17 @@ namespace animation { return anim.playing; } + void rotate(sf::Sprite& target, float degrees) { + target.rotate(sf::degrees(degrees)); + } + + void center(sf::Sprite& target, sf::Vector2f pos) { + auto bounds = target.getLocalBounds(); + target.setPosition({pos.x + bounds.size.x / 2, + pos.y + bounds.size.y / 2}); + target.setOrigin({bounds.size.x / 2, bounds.size.y / 2}); + } + void init() { if(!initialized) { Config config("assets/animations.json"); @@ -98,6 +109,7 @@ namespace animation { } Animation load(std::string name) { + dbc::check(initialized, "You forgot to initialize animation."); return MGR.animations.at(name); } } diff --git a/animation.hpp b/animation.hpp index 44bac0e..18264bb 100644 --- a/animation.hpp +++ b/animation.hpp @@ -9,6 +9,9 @@ namespace animation { }; bool apply(components::Animation& anim, textures::SpriteTexture& target); + void rotate(sf::Sprite& target, float degrees); + void center(sf::Sprite& target, sf::Vector2f pos); + void init(); components::Animation load(std::string name); } diff --git a/ritual_ui.cpp b/ritual_ui.cpp index e8d0e20..cba10dd 100644 --- a/ritual_ui.cpp +++ b/ritual_ui.cpp @@ -3,6 +3,7 @@ #include "guecs.hpp" #include "rand.hpp" #include "animation.hpp" +#include "rand.hpp" namespace gui { using namespace guecs; @@ -32,20 +33,26 @@ namespace gui { auto button = $gui.entity(name); if(name == "circle_area") { - // $gui.set(button, {}); $gui.set(button, {"the_ritual_circle"}); $gui.set(button, { - [this](auto, auto){ dbc::log("circle clicked"); } + [&](auto ent, auto){ ritual_circle_clicked(ent); } }); } else if(name.starts_with("inv_slot")) { - $gui.set(button, {"the_ritual_circle"}); - // $gui.set(button, {}); + + if(button % 5 == 0) { + $gui.set(button, {"cinqueda"}); + } else if(button % 9 == 0) { + $gui.set(button, {"healing_potion_small"}); + } else if(button % 3 == 0) { + $gui.set(button, {"torch_horizontal_floor"}); + } + $gui.set(button, { - [this, name](auto, auto){ dbc::log(fmt::format("inv_slot {}", name)); } + [&](auto ent, auto){ inv_slot_clicked(ent); } }); } else if(name == "ritual_ui") { $gui.set(button, { - [this](auto, auto){ toggle(); } + [&](auto, auto){ toggle(); } }); } } @@ -57,12 +64,51 @@ namespace gui { $ritual_anim = animation::load("ritual_blanket"); $gui.init(); + } bool RitualUI::is_open() { return $ritual_state != RitualUIState::CLOSED; } + void RitualUI::inv_slot_clicked(DinkyECS::Entity ent) { + if($gui.has(ent)) { + auto& bs = $gui.get(ent); + auto ritual_circle = $gui.entity("circle_area"); + auto& ritual_cell = $gui.cell_for(ritual_circle); + dbc::log(fmt::format("inv_slot clicked {}", bs.name)); + + int inner_x = ritual_cell.x + ritual_cell.x / 2; + int inner_y = ritual_cell.y + ritual_cell.y / 2; + + float x = Random::uniform(inner_x, inner_x + ritual_cell.w / 2); + float y = Random::uniform(inner_y, inner_y + ritual_cell.h / 2); + bs.sprite->setPosition({float(x), float(y)}); + } + } + + void RitualUI::reset_inv_positions() { + auto ritual_circle = $gui.entity("circle_area"); + + $gui.world().query( + [&](const auto ent, auto &cell, auto &bs) { + if(ent == ritual_circle) { + bs.sprite->setColor({255,255,255,255}); + bs.sprite->setRotation(sf::degrees(0.0)); + } else { + bs.sprite->setPosition({(float)cell.x, (float)cell.y}); + } + }); + } + + void RitualUI::ritual_circle_clicked(DinkyECS::Entity ent) { + auto cell = $gui.cell_for(ent); + auto& bs = $gui.get(ent); + bs.sprite->setColor({200, 0, 0}); + animation::center(*bs.sprite, {(float)cell.x, (float)cell.y}); + animation::rotate(*bs.sprite, 20.0); + } + bool RitualUI::mouse(float x, float y) { return $gui.mouse(x, y); } @@ -91,6 +137,7 @@ namespace gui { $ritual_state = OPEN; } } else if($ritual_state == CLOSING) { + reset_inv_positions(); $ritual_ui.sprite->setTextureRect($ritual_closed_rect); $ritual_state = CLOSED; } diff --git a/ritual_ui.hpp b/ritual_ui.hpp index 317674a..06fc71c 100644 --- a/ritual_ui.hpp +++ b/ritual_ui.hpp @@ -30,5 +30,9 @@ namespace gui { void init(); void render(sf::RenderWindow &window); void update(); + + void ritual_circle_clicked(DinkyECS::Entity ent); + void inv_slot_clicked(DinkyECS::Entity ent); + void reset_inv_positions(); }; }