RitualUI is now ritual::UI and uses a FSM to coordinate its activities.
This commit is contained in:
parent
d1bd6b7c45
commit
8a1f42c0f1
6 changed files with 141 additions and 231 deletions
|
@ -125,6 +125,14 @@ namespace guecs {
|
||||||
$parser.position(x, y, width, height);
|
$parser.position(x, y, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sf::Vector2f UI::get_position() {
|
||||||
|
return {float($parser.grid_x), float($parser.grid_y)};
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2f UI::get_size() {
|
||||||
|
return {float($parser.grid_w), float($parser.grid_h)};
|
||||||
|
}
|
||||||
|
|
||||||
void UI::layout(std::string grid) {
|
void UI::layout(std::string grid) {
|
||||||
$grid = grid;
|
$grid = grid;
|
||||||
bool good = $parser.parse($grid);
|
bool good = $parser.parse($grid);
|
||||||
|
|
|
@ -136,6 +136,8 @@ namespace guecs {
|
||||||
UI();
|
UI();
|
||||||
|
|
||||||
void position(int x, int y, int width, int height);
|
void position(int x, int y, int width, int height);
|
||||||
|
sf::Vector2f get_position();
|
||||||
|
sf::Vector2f get_size();
|
||||||
void layout(std::string grid);
|
void layout(std::string grid);
|
||||||
DinkyECS::Entity init_entity(std::string name);
|
DinkyECS::Entity init_entity(std::string name);
|
||||||
DinkyECS::Entity entity(std::string name);
|
DinkyECS::Entity entity(std::string name);
|
||||||
|
|
286
ritual_ui.cpp
286
ritual_ui.cpp
|
@ -7,217 +7,109 @@
|
||||||
#include "sound.hpp"
|
#include "sound.hpp"
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
using namespace guecs;
|
namespace ritual {
|
||||||
using std::any, std::any_cast, std::string, std::make_any;
|
using namespace guecs;
|
||||||
|
using std::any, std::any_cast, std::string, std::make_any;
|
||||||
|
|
||||||
RitualUI::RitualUI(GameLevel level) :
|
void UI::event(Event ev) {
|
||||||
$level(level)
|
switch($state) {
|
||||||
{
|
FSM_STATE(State, START, ev);
|
||||||
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
|
FSM_STATE(State, OPENED, ev);
|
||||||
$gui.layout(
|
FSM_STATE(State, CLOSED, ev);
|
||||||
"[_]"
|
FSM_STATE(State, OPENING, ev);
|
||||||
"[inv_slot0 | inv_slot1 | inv_slot2| inv_slot3]"
|
FSM_STATE(State, CLOSING, ev);
|
||||||
"[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 UI::START(Event) {
|
||||||
}
|
$ritual_ui = textures::get("ritual_crafting_area");
|
||||||
|
$ritual_ui.sprite->setPosition($gui.get_position());
|
||||||
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
||||||
|
state(State::CLOSED);
|
||||||
|
$ritual_anim = animation::load("ritual_blanket");
|
||||||
|
|
||||||
void RitualUI::reset_inv_positions() {
|
for(auto& [name, cell] : $gui.cells()) {
|
||||||
auto& blanket = $level.world->get_the<ritual::Blanket>();
|
auto button = $gui.entity(name);
|
||||||
blanket.reset();
|
$gui.set<Rectangle>(button, {GUECS_PADDING, {50, 50, 50, 150}});
|
||||||
|
|
||||||
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, {
|
$gui.set<Clickable>(button, {
|
||||||
[&, item_id](auto ent, auto){ inv_slot_clicked(ent, item_id); }
|
[](auto ent, auto) { fmt::println("clicked {}", ent); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto open_close_toggle = $gui.entity("ritual_ui");
|
||||||
|
$gui.set<Clickable>(open_close_toggle, {
|
||||||
|
[&](auto, auto){ event(Event::TOGGLE); }
|
||||||
|
});
|
||||||
|
|
||||||
|
$gui.init();
|
||||||
|
|
||||||
|
state(State::CLOSED);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void RitualUI::toggle() {
|
void UI::OPENED(Event ev) {
|
||||||
using enum RitualUIState;
|
if(ev == Event::TOGGLE) {
|
||||||
|
state(State::CLOSING);
|
||||||
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);
|
void UI::CLOSED(Event ev) {
|
||||||
|
if(ev == Event::TOGGLE) {
|
||||||
|
$ritual_anim.play();
|
||||||
|
state(State::OPENING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($ritual_state == OPEN) {
|
void UI::OPENING(Event ev) {
|
||||||
$gui.render(window);
|
if(ev == Event::TICK) {
|
||||||
// $gui.debug_layout(window);
|
if(!animation::apply($ritual_anim, $ritual_ui)) {
|
||||||
|
state(State::OPENED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UI::CLOSING(Event ev) {
|
||||||
|
if(ev == Event::TICK) {
|
||||||
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
||||||
|
state(State::CLOSED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UI::UI(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 ]");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UI::mouse(float x, float y, bool hover) {
|
||||||
|
return $gui.mouse(x, y, hover);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UI::is_open() {
|
||||||
|
return !in_state(State::CLOSED);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UI::render(sf::RenderWindow &window) {
|
||||||
|
event(Event::TICK);
|
||||||
|
|
||||||
|
window.draw(*$ritual_ui.sprite);
|
||||||
|
|
||||||
|
if(in_state(State::OPENED)) {
|
||||||
|
$gui.render(window);
|
||||||
|
$gui.debug_layout(window);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,40 +5,48 @@
|
||||||
#include "textures.hpp"
|
#include "textures.hpp"
|
||||||
#include "guecs.hpp"
|
#include "guecs.hpp"
|
||||||
#include "rituals.hpp"
|
#include "rituals.hpp"
|
||||||
|
#include "fsm.hpp"
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
enum class RitualUIState {
|
|
||||||
OPEN=0,
|
|
||||||
CLOSED=1,
|
|
||||||
OPENING=2,
|
|
||||||
CLOSING=3
|
|
||||||
};
|
|
||||||
|
|
||||||
class RitualUI {
|
namespace ritual {
|
||||||
public:
|
enum class State {
|
||||||
sf::IntRect $ritual_closed_rect{{0,0},{380,720}};
|
START=0,
|
||||||
sf::IntRect $ritual_open_rect{{380 * 2,0},{380,720}};
|
OPENED=1,
|
||||||
ritual::Engine $engine;
|
CLOSED=2,
|
||||||
ritual::CraftingState $craft_state;
|
OPENING=3,
|
||||||
RitualUIState $ritual_state = RitualUIState::CLOSED;
|
CLOSING=4
|
||||||
textures::SpriteTexture $ritual_ui;
|
};
|
||||||
components::Animation $ritual_anim;
|
|
||||||
guecs::UI $gui;
|
|
||||||
GameLevel $level;
|
|
||||||
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> $selected;
|
|
||||||
|
|
||||||
RitualUI(GameLevel level);
|
|
||||||
bool mouse(float x, float y, bool hover);
|
|
||||||
void toggle();
|
|
||||||
bool is_open();
|
|
||||||
void init();
|
|
||||||
void render(sf::RenderWindow &window);
|
|
||||||
void update();
|
|
||||||
|
|
||||||
void attempt_combine();
|
enum class Event {
|
||||||
void update_items();
|
STARTED=0,
|
||||||
void combine_clicked(DinkyECS::Entity ent);
|
TOGGLE=1,
|
||||||
void inv_slot_clicked(DinkyECS::Entity ent, DinkyECS::Entity item_id);
|
TICK=2
|
||||||
void reset_inv_positions();
|
};
|
||||||
};
|
|
||||||
|
class UI : public DeadSimpleFSM<State, Event>{
|
||||||
|
public:
|
||||||
|
sf::IntRect $ritual_closed_rect{{0,0},{380,720}};
|
||||||
|
sf::IntRect $ritual_open_rect{{380 * 2,0},{380,720}};
|
||||||
|
components::Animation $ritual_anim;
|
||||||
|
guecs::UI $gui;
|
||||||
|
GameLevel $level;
|
||||||
|
textures::SpriteTexture $ritual_ui;
|
||||||
|
|
||||||
|
UI(GameLevel level);
|
||||||
|
|
||||||
|
void event(Event ev);
|
||||||
|
void START(Event);
|
||||||
|
void OPENED(Event);
|
||||||
|
void CLOSED(Event);
|
||||||
|
void OPENING(Event);
|
||||||
|
void CLOSING(Event);
|
||||||
|
|
||||||
|
bool mouse(float x, float y, bool hover);
|
||||||
|
void render(sf::RenderWindow &window);
|
||||||
|
bool is_open();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ namespace gui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$ritual_ui.init();
|
$ritual_ui.event(ritual::Event::STARTED);
|
||||||
$gui.init();
|
$gui.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ namespace gui {
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusUI::select_ritual() {
|
void StatusUI::select_ritual() {
|
||||||
$ritual_ui.toggle();
|
$ritual_ui.event(ritual::Event::TOGGLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusUI::select_slot(DinkyECS::Entity ent, any slot_name) {
|
void StatusUI::select_slot(DinkyECS::Entity ent, any slot_name) {
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace gui {
|
||||||
std::map<std::string, size_t> $slots;
|
std::map<std::string, size_t> $slots;
|
||||||
std::deque<std::wstring> $messages;
|
std::deque<std::wstring> $messages;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
RitualUI $ritual_ui;
|
ritual::UI $ritual_ui;
|
||||||
|
|
||||||
StatusUI(GameLevel level);
|
StatusUI(GameLevel level);
|
||||||
void select_slot(DinkyECS::Entity ent, std::any data);
|
void select_slot(DinkyECS::Entity ent, std::any data);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue