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

65 lines
1.8 KiB

#include "combat_ui.hpp"
#include "constants.hpp"
#include "color.hpp"
namespace gui {
CombatUI::CombatUI(GameLevel level) :
$layout(RAY_VIEW_X, RAY_VIEW_HEIGHT,
RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT),
$level(level),
$font{FONT_FILE_NAME}
{
bool good = $layout.parse($grid);
dbc::check(good, "failed to parse combat layout");
render();
}
void CombatUI::render() {
$background.setPosition({float($layout.grid_x), float($layout.grid_y)});
$background.setSize({float($layout.grid_w), float($layout.grid_h)});
$background.setFillColor({0, 0, 0});
for(auto& [name, cell] : $layout.cells) {
if(name == "bar_hp") {
$meters.try_emplace(name, cell);
} else if(name == "label_hp") {
gui::Label label(cell, "hp:", $font);
$labels.emplace_back(cell, "hp:", $font);
} else if(name.starts_with("button_")) {
$buttons.try_emplace(name, cell, name, $font);
}
}
}
void CombatUI::draw(sf::RenderWindow& window) {
window.draw($background);
auto &player = $level.world->get_the<components::Player>();
auto &combat = $level.world->get<components::Combat>(player.entity);
float hp_now = float(combat.hp) / float(combat.max_hp);
auto& bar_hp = $meters.at("bar_hp");
bar_hp.set_percent(hp_now);
for(auto& [name, button] : $buttons) {
button.draw(window);
}
for(auto& [name, meter] : $meters) {
meter.draw(window);
}
for(auto& label : $labels) {
label.draw(window);
}
}
void CombatUI::click(int x, int y) {
if(auto name = $layout.hit(x, y)) {
if((*name).starts_with("button_")) {
auto& button = $buttons.at(*name);
button.shape.setFillColor({100, 0, 0});
}
}
}
}