Exploring raycasters and possibly make a little "doom like" game based on it.
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/combat_ui.cpp

58 lines
1.6 KiB

#include "combat_ui.hpp"
#include "constants.hpp"
#include "color.hpp"
#include "events.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(
"[*%(100,150)button_attack1 | *%(100,150)button_attack2 | *%(100,150)button_attack3 | *%(100,150)button_heal]"
"[ >.%(100,50)label_hp | *%.(198,50)bar_hp | _ ]");
}
void CombatUI::init() {
auto& world = $gui.world();
world.set_the<Background>({$gui.$parser});
for(auto& [name, cell] : $gui.cells()) {
if(name.starts_with("button_")) {
auto button = $gui.entity(name);
world.set<Rectangle>(button, {});
world.set<Clickable>(button,
guecs::make_action(*$level.world, Events::GUI::ATTACK));
world.set<Label>(button, {"Attack"});
} else if(name.starts_with("bar_")) {
$meter = $gui.entity(name);
world.set<Rectangle>($meter, {});
world.set<Meter>($meter, {});
} else {
// ignored, it's just space
$gui.entity(name);
}
}
$gui.init();
}
void CombatUI::render(sf::RenderWindow& window) {
auto& player_combat = $level.world->get<components::Combat>($level.player);
set_damage(float(player_combat.hp) / float(player_combat.max_hp));
$gui.render(window);
}
void CombatUI::set_damage(float percent) {
auto& meter = $gui.world().get<Meter>($meter);
meter.percent = percent;
}
void CombatUI::update_level(GameLevel &level) {
$level = level;
init();
}
}