You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
6.4 KiB
223 lines
6.4 KiB
#include "ritual_ui.hpp"
|
|
#include "components.hpp"
|
|
#include "guecs.hpp"
|
|
#include "rand.hpp"
|
|
#include "animation.hpp"
|
|
#include "rand.hpp"
|
|
#include "sound.hpp"
|
|
|
|
namespace gui {
|
|
using namespace guecs;
|
|
using std::any, std::any_cast, std::string, std::make_any;
|
|
|
|
RitualUI::RitualUI(GameLevel level) :
|
|
$level(level)
|
|
{
|
|
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
|
|
$gui.layout(
|
|
"[_]"
|
|
"[inv_slot0 | inv_slot1 | inv_slot2| inv_slot3]"
|
|
"[inv_slot4 | inv_slot5 | inv_slot6| inv_slot7]"
|
|
"[inv_slot8 | inv_slot9 | inv_slot10| inv_slot11]"
|
|
"[inv_slot12 | inv_slot13 | inv_slot14| inv_slot15]"
|
|
"[reset |*%(200,400)result_text|_]"
|
|
"[*%(100,200)result_image|_ |_]"
|
|
"[_|_|_]"
|
|
"[combine|_|_]"
|
|
"[_|craft0|craft1|craft2|craft3|_]"
|
|
"[_|craft4|craft5|craft6|craft7|_]"
|
|
"[ ritual_ui ]");
|
|
}
|
|
|
|
void RitualUI::init() {
|
|
update_items();
|
|
|
|
auto combine = $gui.entity("combine");
|
|
$gui.set<Effect>(combine, {0.4f});
|
|
$gui.set<Sprite>(combine, {"the_ritual_circle"});
|
|
$gui.set<Clickable>(combine, {
|
|
[&](auto ent, auto){ combine_clicked(ent); }
|
|
});
|
|
|
|
auto result_image = $gui.entity("result_image");
|
|
$gui.set<Sprite>(result_image, {"severed_finger-128"});
|
|
$gui.set<Rectangle>(result_image, {10, {60, 60, 60, 30}});
|
|
|
|
auto result_text = $gui.entity("result_text");
|
|
$gui.set<Rectangle>(result_text, {15, {60, 60, 60, 30}});
|
|
$gui.set<Textual>(result_text, {
|
|
L"Celiac migas\nunicorn hexagon.\nBrooklyn williamsburg\ntruffaut pickled\nchillwave raclette\nchurch-key sus.", 16, ColorValue::LIGHT_LIGHT, 10});
|
|
|
|
auto reset = $gui.entity("reset");
|
|
$gui.set<Rectangle>(reset, {5, {60, 60, 60, 30}});
|
|
$gui.set<Label>(reset, L"reset");
|
|
$gui.set<Clickable>(reset, {
|
|
[&](auto, auto){ reset_inv_positions(); }
|
|
});
|
|
|
|
auto open_close_toggle = $gui.entity("ritual_ui");
|
|
$gui.set<Clickable>(open_close_toggle, {[&](auto, auto){ toggle(); }});
|
|
$gui.set<Sound>(open_close_toggle, {"pickup"});
|
|
|
|
$ritual_ui = textures::get("ritual_crafting_area");
|
|
$ritual_ui.sprite->setPosition({0,0});
|
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
|
$ritual_state = RitualUIState::CLOSED;
|
|
$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, DinkyECS::Entity item_id) {
|
|
$selected.insert_or_assign(ent, item_id);
|
|
|
|
auto& blanket = $level.world->get_the<ritual::Blanket>();
|
|
blanket.select(item_id);
|
|
|
|
attempt_combine();
|
|
|
|
// display the possible outcome here
|
|
if($craft_state.is_combined()) {
|
|
auto ritual = $engine.finalize($craft_state);
|
|
using enum ritual::Element;
|
|
switch(ritual.element) {
|
|
case FIRE:
|
|
$gui.show_sprite("result_image", "broken_yoyo-64");
|
|
break;
|
|
case LIGHTNING:
|
|
$gui.show_sprite("result_image", "pocket_watch-64");
|
|
break;
|
|
default:
|
|
$gui.show_sprite("result_image", "severed_finger-64");
|
|
}
|
|
} else {
|
|
$gui.close<Sprite>("result_image");
|
|
}
|
|
|
|
update_items();
|
|
}
|
|
|
|
void RitualUI::reset_inv_positions() {
|
|
auto& blanket = $level.world->get_the<ritual::Blanket>();
|
|
blanket.reset();
|
|
|
|
std::array<std::string, 8> temp_names{
|
|
"craft0","craft1","craft2","craft3",
|
|
"craft4","craft5","craft6","craft7"
|
|
};
|
|
|
|
for(const auto& name : temp_names) {
|
|
$gui.close<Sprite>(name);
|
|
}
|
|
|
|
update_items();
|
|
}
|
|
|
|
void RitualUI::attempt_combine() {
|
|
auto& blanket = $level.world->get_the<ritual::Blanket>();
|
|
$craft_state = $engine.start();
|
|
|
|
for(auto [ent, yes] : blanket.selected) {
|
|
$engine.load_junk($craft_state, blanket.get(ent));
|
|
}
|
|
|
|
// finalize here ritual here
|
|
$engine.plan($craft_state);
|
|
}
|
|
|
|
void RitualUI::combine_clicked(DinkyECS::Entity ent) {
|
|
// auto cell = $gui.cell_for(ent);
|
|
auto& bs = $gui.get<Sprite>(ent);
|
|
bs.sprite->setColor({200, 0, 0});
|
|
|
|
attempt_combine();
|
|
|
|
if($craft_state.is_combined()) {
|
|
// add it to the belt
|
|
auto ritual = $engine.finalize($craft_state);
|
|
|
|
// remove the items from the blanket now
|
|
auto& the_belt = $level.world->get_the<ritual::Belt>();
|
|
|
|
the_belt.equip(the_belt.next(), ritual);
|
|
|
|
$level.world->send<Events::GUI>(Events::GUI::NEW_RITUAL, $level.player, {});
|
|
reset_inv_positions();
|
|
} else {
|
|
fmt::println("Failed to combine!");
|
|
reset_inv_positions();
|
|
sound::play("ui_click");
|
|
}
|
|
}
|
|
|
|
bool RitualUI::mouse(float x, float y, bool hover) {
|
|
return $gui.mouse(x, y, hover);
|
|
}
|
|
|
|
void RitualUI::update_items() {
|
|
auto& blanket = $level.world->get_the<ritual::Blanket>();
|
|
int i = 0;
|
|
int j = 0;
|
|
|
|
for(auto& [item_id, item] : blanket.contents) {
|
|
auto button = $gui.entity("inv_slot", i++);
|
|
std::string sprite_name = fmt::format("{}-64", item);
|
|
|
|
if(blanket.is_selected(item_id)) {
|
|
auto selector = $gui.entity("craft", j++);
|
|
$gui.set_init<Sprite>(selector, {sprite_name});
|
|
} else if($gui.has<Clickable>(button)) {
|
|
$gui.set_init<Sprite>(button, {sprite_name});
|
|
} else {
|
|
$gui.set_init<Sprite>(button, {sprite_name});
|
|
$gui.set_init<Effect>(button, {0.4f});
|
|
$gui.set<Sound>(button, {"ui_click"});
|
|
$gui.set<Clickable>(button, {
|
|
[&, item_id](auto ent, auto){ inv_slot_clicked(ent, item_id); }
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void RitualUI::toggle() {
|
|
using enum RitualUIState;
|
|
|
|
if($ritual_state == OPEN) {
|
|
$ritual_state = CLOSING;
|
|
} else if($ritual_state == CLOSED) {
|
|
update_items();
|
|
$craft_state = $engine.start();
|
|
$ritual_state = OPENING;
|
|
$ritual_anim.play();
|
|
}
|
|
}
|
|
|
|
void RitualUI::update() {
|
|
dbc::log("RITUAL UPDATE NOT IMPLEMENTED");
|
|
}
|
|
|
|
void RitualUI::render(sf::RenderWindow &window) {
|
|
using enum RitualUIState;
|
|
|
|
if($ritual_state == OPENING) {
|
|
if(!animation::apply($ritual_anim, $ritual_ui)) {
|
|
$ritual_state = OPEN;
|
|
}
|
|
} else if($ritual_state == CLOSING) {
|
|
reset_inv_positions();
|
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
|
$ritual_state = CLOSED;
|
|
}
|
|
|
|
window.draw(*$ritual_ui.sprite);
|
|
|
|
if($ritual_state == OPEN) {
|
|
$gui.render(window);
|
|
// $gui.debug_layout(window);
|
|
}
|
|
}
|
|
}
|
|
|