From dfd59065f7f36cc89d3b4c6486835812f44221c6 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 24 Feb 2025 01:56:16 -0500 Subject: [PATCH] Inventory and lighting improved, now to get ready for going down a level and that's most of the game loop working. --- assets/config.json | 4 ++-- assets/items.json | 2 +- constants.hpp | 2 +- inventory.cpp | 4 ++++ inventory.hpp | 2 ++ lights.hpp | 4 ++-- status_ui.cpp | 53 +++++++++++++++++++++++++--------------------- status_ui.hpp | 8 ++----- 8 files changed, 43 insertions(+), 36 deletions(-) diff --git a/assets/config.json b/assets/config.json index 2ea598d..a4af630 100644 --- a/assets/config.json +++ b/assets/config.json @@ -38,8 +38,8 @@ "player": { }, "worldgen": { - "enemy_probability": 10, - "empty_room_probability": 1, + "enemy_probability": 30, + "empty_room_probability": 10, "device_probability": 10 } } diff --git a/assets/items.json b/assets/items.json index a828f11..364e135 100644 --- a/assets/items.json +++ b/assets/items.json @@ -5,7 +5,7 @@ "description": "A torch that barely lights the way. You wonder if it'd be better to not see the person who murders you.", "inventory_count": 1, "components": [ - {"_type": "LightSource", "strength": 50, "radius": 1.5}, + {"_type": "LightSource", "strength": 50, "radius": 2.5}, {"_type": "Tile", "display": "\u0f08", "foreground": [24, 120, 189], "background": [230,120, 120] diff --git a/constants.hpp b/constants.hpp index 3068134..52cb9b7 100644 --- a/constants.hpp +++ b/constants.hpp @@ -11,7 +11,7 @@ constexpr const int SCREEN_WIDTH=1280; constexpr const int SCREEN_HEIGHT=720; constexpr const int RAY_VIEW_X=(SCREEN_WIDTH - RAY_VIEW_WIDTH); constexpr const int RAY_VIEW_Y=0; -constexpr const bool VSYNC=true; +constexpr const bool VSYNC=false; constexpr const int FRAME_LIMIT=60; constexpr const int NUM_SPRITES=1; constexpr const int MAX_LOG_MESSAGES=17; diff --git a/inventory.cpp b/inventory.cpp index 8df47ed..a541893 100644 --- a/inventory.cpp +++ b/inventory.cpp @@ -14,6 +14,10 @@ namespace components { items.push_back(new_item); } + bool Inventory::has_item(size_t at) { + return at < items.size(); + } + InventoryItem& Inventory::get(size_t at) { dbc::check(at < items.size(), fmt::format("inventory index {} too big", at)); return items[at]; diff --git a/inventory.hpp b/inventory.hpp index 50795b6..9083cab 100644 --- a/inventory.hpp +++ b/inventory.hpp @@ -22,6 +22,8 @@ namespace components { bool decrease(size_t at, int count); + bool has_item(size_t at); + InventoryItem& get(size_t at); int item_index(std::string id); diff --git a/lights.hpp b/lights.hpp index dec8745..8da0bdd 100644 --- a/lights.hpp +++ b/lights.hpp @@ -10,8 +10,8 @@ namespace lighting { using components::LightSource; - const int MIN = 30; - const int MAX = 105; + const int MIN = 20; + const int MAX = 125; class LightRender { public: diff --git a/status_ui.cpp b/status_ui.cpp index 5c591a4..f79343f 100644 --- a/status_ui.cpp +++ b/status_ui.cpp @@ -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(button, {}); $gui.set(button, {""}); - $gui.set(button, {std::make_any(name)}); + $gui.set(button, {make_any(name)}); $gui.set(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(ent); auto world = $level.world; if(world->has($level.player)) { auto& inventory = world->get($level.player); + size_t inv_id = $slots[any_cast(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($log_to)) { auto& text = $gui.get($log_to); - std::string log; + string log; for(auto msg : $messages) { log += msg + "\n"; } @@ -79,15 +86,13 @@ namespace gui { if(world->has($level.player)) { auto& inventory = world->get($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(item.data); $gui.set_init(slot, {comp_sprite.name}); - std::string count_label = fmt::format("{}", item.count); + string count_label = fmt::format("{}", item.count); auto& label = $gui.get(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(); diff --git a/status_ui.hpp b/status_ui.hpp index 6f0e698..9cf8bd8 100644 --- a/status_ui.hpp +++ b/status_ui.hpp @@ -10,14 +10,10 @@ namespace gui { public: guecs::UI $gui; DinkyECS::Entity $log_to; - std::array $slots = { - "slot1", "slot2", "slot3", - "slot4", "slot5", "slot6", - "slot7", "slot8", "slot9" - }; - + std::map $slots; std::deque $messages; GameLevel $level; + StatusUI(GameLevel level); void select_slot(DinkyECS::Entity ent, std::any data); void update_level(GameLevel &level) { $level = level; }