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/status_ui.cpp

85 lines
2.3 KiB

#include "status_ui.hpp"
#include "components.hpp"
#include "inventory.hpp"
#include "color.hpp"
#include "guecs.hpp"
#include "rand.hpp"
namespace gui {
using namespace guecs;
StatusUI::StatusUI(GameLevel level) :
$level(level)
{
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
$gui.layout(
"[slot1 | slot2 | slot3]"
"[slot4 | slot5 | slot6]"
"[slot7 | slot8 | slot9]"
"[*%(100,300)log_view]"
"[_]"
"[_]");
}
void StatusUI::render() {
for(auto& [name, cell] : $gui.cells()) {
if(name == "log_view") {
$log_to = $gui.entity("log_view");
$gui.set<Rectangle>($log_to, {});
$gui.set<Textual>($log_to, {"Welcome to the Game!", 20});
} else {
auto button = $gui.entity(name);
$gui.set<Rectangle>(button, {});
$gui.set<Textual>(button, {""});
$gui.set<Clickable>(button,
guecs::make_action(*$level.world, Events::GUI::NOOP));
}
}
$gui.init();
}
/* WARNING: This is really not the greatest way to do this. */
void StatusUI::update() {
if($gui.has<Textual>($log_to)) {
auto& text = $gui.get<Textual>($log_to);
std::string log;
for(auto msg : $messages) {
log += msg + "\n";
}
text.update(log);
}
auto world = $level.world;
if(world->has<components::Inventory>($level.player)) {
auto& inventory = world->get<components::Inventory>($level.player);
if(inventory.count() > 0) {
size_t limit = std::min(inventory.count(), $slots.size());
for(size_t i = 0; i < limit; i++) {
auto slot = $gui.entity($slots[i]);
auto& item = inventory.get(i);
auto comp_sprite = components::get<components::Sprite>(item.data);
$gui.set_init<guecs::Sprite>(slot, {comp_sprite.name});
std::string count_label = item.count > 1 ? fmt::format("{}", item.count): "";
auto& label = $gui.get<Textual>(slot);
label.text->setString(count_label);
}
}
}
}
void StatusUI::draw(sf::RenderWindow &window) {
$gui.render(window);
}
void StatusUI::log(std::string msg) {
$messages.push_front(msg);
if($messages.size() > MAX_LOG_MESSAGES) {
$messages.pop_back();
}
}
}