A retro style homage to 80s dungeon crawlers hand crafted in C++.
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.
 
 
 
 
 
 
raycaster/gui/combat_ui.cpp

109 lines
3.2 KiB

#include "gui/combat_ui.hpp"
#include "constants.hpp"
#include "rituals.hpp"
#include <fmt/xchar.h>
#include "gui/guecstra.hpp"
#include "inventory.hpp"
namespace gui {
using namespace guecs;
CombatUI::CombatUI(GameLevel level) :
$level(level)
{
$gui.position(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT);
$gui.layout(
"[button_0 | button_1 | button_2 | button_3"
"|button_4 | button_5 | button_6 | =healing_button | =hp_gauge ]"
);
}
guecs::Entity CombatUI::make_button(
std::string name,
Events::GUI event,
int action,
const std::string &icon_name,
const std::string &sound,
const std::string &effect_name)
{
auto button = $gui.entity(name);
$gui.set<Sprite>(button, {icon_name});
$gui.set<Sound>(button, {sound});
$gui.set<Effect>(button, {.duration=0.5f, .name=effect_name});
$gui.set<Clickable>(button,
guecs::make_action($level, event, {action}));
return button;
}
void CombatUI::init() {
using guecs::THEME;
$gui.set<Background>($gui.MAIN, {$gui.$parser, THEME.DARK_MID});
auto& the_belt = $level.world->get_the<ritual::Belt>();
for(int slot = 0; slot < the_belt.max_slots; slot++) {
if(the_belt.has(slot)) {
std::string name = fmt::format("button_{}", slot);
auto& ritual = the_belt.get(slot);
using enum ritual::Element;
switch(ritual.element) {
case FIRE:
make_button(name, Events::GUI::ATTACK,
slot, "broken_yoyo", "fireball_01", "flame");
break;
case LIGHTNING:
make_button(name, Events::GUI::ATTACK,
slot, "pocket_watch", "electric_shock_01", "lightning");
break;
default:
make_button(name, Events::GUI::ATTACK,
slot, "severed_finger", "punch_cartoony", "ui_shader");
}
}
}
auto healing_button = $gui.entity("healing_button");
auto& inventory = $level.world->get<inventory::Model>($level.player);
if(inventory.has("pocket_l")) {
$gui.set<Icon>(healing_button, {"healing_potion_small"});
$gui.set<Clickable>(healing_button, {[&](auto gui_id, auto) {
use_item(gui_id, "pocket_l");
}});
} else {
$gui.remove<Icon>(healing_button);
$gui.remove<Clickable>(healing_button);
}
auto hp_gauge = $gui.entity("hp_gauge");
$gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"});
$gui.set<Clickable>(hp_gauge,
guecs::make_action($level, Events::GUI::HP_STATUS, {}));
$gui.init();
}
void CombatUI::use_item(guecs::Entity gui_id, const string& slot) {
auto& inventory = $level.world->get<inventory::Model>($level.player);
dbc::check(inventory.has(slot), fmt::format(
"attempted to use an item but {} isn't in your inventory", slot));
auto healing_item = inventory.get(slot);
$level.world->send<Events::GUI>(Events::GUI::USE_ITEM, gui_id, {healing_item});
}
void CombatUI::render(sf::RenderWindow& window) {
$gui.render(window);
}
void CombatUI::update_level(GameLevel &level) {
$level = level;
init();
}
bool CombatUI::mouse(float x, float y, bool hover) {
return $gui.mouse(x, y, hover);
}
}