Inventory and lighting improved, now to get ready for going down a level and that's most of the game loop working.
This commit is contained in:
parent
0878a9e978
commit
dfd59065f7
8 changed files with 43 additions and 36 deletions
|
@ -7,18 +7,26 @@
|
|||
|
||||
namespace gui {
|
||||
using namespace guecs;
|
||||
using std::any, std::any_cast, std::string, std::make_any;
|
||||
|
||||
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]"
|
||||
"[inv_slot1 | inv_slot2 | inv_slot3]"
|
||||
"[inv_slot4 | inv_slot5 | inv_slot6]"
|
||||
"[inv_slot7 | inv_slot8 | inv_slot9]"
|
||||
"[*%(100,300)log_view]"
|
||||
"[_]"
|
||||
"[_]");
|
||||
|
||||
size_t inv_id = 0;
|
||||
for(auto [name, entity] : $gui.$name_ents) {
|
||||
if(name.starts_with("inv_")) {
|
||||
$slots[name] = inv_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StatusUI::render() {
|
||||
|
@ -31,9 +39,9 @@ namespace gui {
|
|||
auto button = $gui.entity(name);
|
||||
$gui.set<Rectangle>(button, {});
|
||||
$gui.set<Textual>(button, {""});
|
||||
$gui.set<ActionData>(button, {std::make_any<std::string>(name)});
|
||||
$gui.set<ActionData>(button, {make_any<string>(name)});
|
||||
$gui.set<Clickable>(button, {
|
||||
[&](auto ent, auto data){ select_slot(ent, data); }
|
||||
[this](auto ent, auto data){ select_slot(ent, data); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -41,23 +49,22 @@ namespace gui {
|
|||
$gui.init();
|
||||
}
|
||||
|
||||
void StatusUI::select_slot(DinkyECS::Entity ent, std::any) {
|
||||
void StatusUI::select_slot(DinkyECS::Entity ent, any slot_name) {
|
||||
dbc::check(slot_name.has_value(), "passed select_slot an any without a value");
|
||||
|
||||
auto cn = $gui.get<CellName>(ent);
|
||||
auto world = $level.world;
|
||||
|
||||
if(world->has<components::Inventory>($level.player)) {
|
||||
auto& inventory = world->get<components::Inventory>($level.player);
|
||||
size_t inv_id = $slots[any_cast<string>(slot_name)];
|
||||
|
||||
for(size_t i = 0; i < inventory.count(); i++) {
|
||||
if($slots[i] == cn.name) {
|
||||
auto [used, name] = inventory.use($level, i);
|
||||
auto [used, name] = inventory.use($level, inv_id);
|
||||
|
||||
if(used) {
|
||||
log(fmt::format("Used item: {}", name));
|
||||
} else {
|
||||
log(fmt::format("You are out of {}.", name));
|
||||
}
|
||||
}
|
||||
if(used) {
|
||||
log(fmt::format("Used item: {}", name));
|
||||
} else {
|
||||
log(fmt::format("You are out of {}.", name));
|
||||
}
|
||||
|
||||
update();
|
||||
|
@ -68,7 +75,7 @@ namespace gui {
|
|||
void StatusUI::update() {
|
||||
if($gui.has<Textual>($log_to)) {
|
||||
auto& text = $gui.get<Textual>($log_to);
|
||||
std::string log;
|
||||
string log;
|
||||
for(auto msg : $messages) {
|
||||
log += msg + "\n";
|
||||
}
|
||||
|
@ -79,15 +86,13 @@ namespace gui {
|
|||
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);
|
||||
for(auto& [slot_name, inv_id] : $slots) {
|
||||
if(inventory.has_item(inv_id)) {
|
||||
auto slot = $gui.entity(slot_name);
|
||||
auto& item = inventory.get(inv_id);
|
||||
auto comp_sprite = components::get<components::Sprite>(item.data);
|
||||
$gui.set_init<guecs::Sprite>(slot, {comp_sprite.name});
|
||||
std::string count_label = fmt::format("{}", item.count);
|
||||
string count_label = fmt::format("{}", item.count);
|
||||
auto& label = $gui.get<Textual>(slot);
|
||||
label.text->setString(count_label);
|
||||
|
||||
|
@ -107,7 +112,7 @@ namespace gui {
|
|||
$gui.render(window);
|
||||
}
|
||||
|
||||
void StatusUI::log(std::string msg) {
|
||||
void StatusUI::log(string msg) {
|
||||
$messages.push_front(msg);
|
||||
if($messages.size() > MAX_LOG_MESSAGES) {
|
||||
$messages.pop_back();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue