Basic Ritual crafting UI is prototyped, so next step is to create some items and refine the UI with a possible FSM to keep it organized.
This commit is contained in:
parent
1aa6674e42
commit
8b3573b01d
4 changed files with 72 additions and 6 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<Rectangle>(button, {});
|
||||
$gui.set<Sprite>(button, {"the_ritual_circle"});
|
||||
$gui.set<Clickable>(button, {
|
||||
[this](auto, auto){ dbc::log("circle clicked"); }
|
||||
[&](auto ent, auto){ ritual_circle_clicked(ent); }
|
||||
});
|
||||
} else if(name.starts_with("inv_slot")) {
|
||||
$gui.set<Sprite>(button, {"the_ritual_circle"});
|
||||
// $gui.set<Rectangle>(button, {});
|
||||
|
||||
if(button % 5 == 0) {
|
||||
$gui.set<Sprite>(button, {"cinqueda"});
|
||||
} else if(button % 9 == 0) {
|
||||
$gui.set<Sprite>(button, {"healing_potion_small"});
|
||||
} else if(button % 3 == 0) {
|
||||
$gui.set<Sprite>(button, {"torch_horizontal_floor"});
|
||||
}
|
||||
|
||||
$gui.set<Clickable>(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<Clickable>(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<Sprite>(ent)) {
|
||||
auto& bs = $gui.get<Sprite>(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<lel::Cell, Sprite>(
|
||||
[&](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<Sprite>(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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue