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
|
@ -38,8 +38,8 @@
|
|||
"player": {
|
||||
},
|
||||
"worldgen": {
|
||||
"enemy_probability": 10,
|
||||
"empty_room_probability": 1,
|
||||
"enemy_probability": 30,
|
||||
"empty_room_probability": 10,
|
||||
"device_probability": 10
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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,24 +49,23 @@ 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
|
@ -10,14 +10,10 @@ namespace gui {
|
|||
public:
|
||||
guecs::UI $gui;
|
||||
DinkyECS::Entity $log_to;
|
||||
std::array<std::string, 9> $slots = {
|
||||
"slot1", "slot2", "slot3",
|
||||
"slot4", "slot5", "slot6",
|
||||
"slot7", "slot8", "slot9"
|
||||
};
|
||||
|
||||
std::map<std::string, size_t> $slots;
|
||||
std::deque<std::string> $messages;
|
||||
GameLevel $level;
|
||||
|
||||
StatusUI(GameLevel level);
|
||||
void select_slot(DinkyECS::Entity ent, std::any data);
|
||||
void update_level(GameLevel &level) { $level = level; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue