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": {
|
"player": {
|
||||||
},
|
},
|
||||||
"worldgen": {
|
"worldgen": {
|
||||||
"enemy_probability": 10,
|
"enemy_probability": 30,
|
||||||
"empty_room_probability": 1,
|
"empty_room_probability": 10,
|
||||||
"device_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.",
|
"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,
|
"inventory_count": 1,
|
||||||
"components": [
|
"components": [
|
||||||
{"_type": "LightSource", "strength": 50, "radius": 1.5},
|
{"_type": "LightSource", "strength": 50, "radius": 2.5},
|
||||||
{"_type": "Tile", "display": "\u0f08",
|
{"_type": "Tile", "display": "\u0f08",
|
||||||
"foreground": [24, 120, 189],
|
"foreground": [24, 120, 189],
|
||||||
"background": [230,120, 120]
|
"background": [230,120, 120]
|
||||||
|
|
|
@ -11,7 +11,7 @@ constexpr const int SCREEN_WIDTH=1280;
|
||||||
constexpr const int SCREEN_HEIGHT=720;
|
constexpr const int SCREEN_HEIGHT=720;
|
||||||
constexpr const int RAY_VIEW_X=(SCREEN_WIDTH - RAY_VIEW_WIDTH);
|
constexpr const int RAY_VIEW_X=(SCREEN_WIDTH - RAY_VIEW_WIDTH);
|
||||||
constexpr const int RAY_VIEW_Y=0;
|
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 FRAME_LIMIT=60;
|
||||||
constexpr const int NUM_SPRITES=1;
|
constexpr const int NUM_SPRITES=1;
|
||||||
constexpr const int MAX_LOG_MESSAGES=17;
|
constexpr const int MAX_LOG_MESSAGES=17;
|
||||||
|
|
|
@ -14,6 +14,10 @@ namespace components {
|
||||||
items.push_back(new_item);
|
items.push_back(new_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Inventory::has_item(size_t at) {
|
||||||
|
return at < items.size();
|
||||||
|
}
|
||||||
|
|
||||||
InventoryItem& Inventory::get(size_t at) {
|
InventoryItem& Inventory::get(size_t at) {
|
||||||
dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
|
dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
|
||||||
return items[at];
|
return items[at];
|
||||||
|
|
|
@ -22,6 +22,8 @@ namespace components {
|
||||||
|
|
||||||
bool decrease(size_t at, int count);
|
bool decrease(size_t at, int count);
|
||||||
|
|
||||||
|
bool has_item(size_t at);
|
||||||
|
|
||||||
InventoryItem& get(size_t at);
|
InventoryItem& get(size_t at);
|
||||||
|
|
||||||
int item_index(std::string id);
|
int item_index(std::string id);
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
namespace lighting {
|
namespace lighting {
|
||||||
using components::LightSource;
|
using components::LightSource;
|
||||||
|
|
||||||
const int MIN = 30;
|
const int MIN = 20;
|
||||||
const int MAX = 105;
|
const int MAX = 125;
|
||||||
|
|
||||||
class LightRender {
|
class LightRender {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -7,18 +7,26 @@
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
using namespace guecs;
|
using namespace guecs;
|
||||||
|
using std::any, std::any_cast, std::string, std::make_any;
|
||||||
|
|
||||||
StatusUI::StatusUI(GameLevel level) :
|
StatusUI::StatusUI(GameLevel level) :
|
||||||
$level(level)
|
$level(level)
|
||||||
{
|
{
|
||||||
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
|
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
|
||||||
$gui.layout(
|
$gui.layout(
|
||||||
"[slot1 | slot2 | slot3]"
|
"[inv_slot1 | inv_slot2 | inv_slot3]"
|
||||||
"[slot4 | slot5 | slot6]"
|
"[inv_slot4 | inv_slot5 | inv_slot6]"
|
||||||
"[slot7 | slot8 | slot9]"
|
"[inv_slot7 | inv_slot8 | inv_slot9]"
|
||||||
"[*%(100,300)log_view]"
|
"[*%(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() {
|
void StatusUI::render() {
|
||||||
|
@ -31,9 +39,9 @@ namespace gui {
|
||||||
auto button = $gui.entity(name);
|
auto button = $gui.entity(name);
|
||||||
$gui.set<Rectangle>(button, {});
|
$gui.set<Rectangle>(button, {});
|
||||||
$gui.set<Textual>(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, {
|
$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();
|
$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 cn = $gui.get<CellName>(ent);
|
||||||
auto world = $level.world;
|
auto world = $level.world;
|
||||||
|
|
||||||
if(world->has<components::Inventory>($level.player)) {
|
if(world->has<components::Inventory>($level.player)) {
|
||||||
auto& inventory = world->get<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++) {
|
auto [used, name] = inventory.use($level, inv_id);
|
||||||
if($slots[i] == cn.name) {
|
|
||||||
auto [used, name] = inventory.use($level, i);
|
|
||||||
|
|
||||||
if(used) {
|
if(used) {
|
||||||
log(fmt::format("Used item: {}", name));
|
log(fmt::format("Used item: {}", name));
|
||||||
} else {
|
} else {
|
||||||
log(fmt::format("You are out of {}.", name));
|
log(fmt::format("You are out of {}.", name));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
@ -68,7 +75,7 @@ namespace gui {
|
||||||
void StatusUI::update() {
|
void StatusUI::update() {
|
||||||
if($gui.has<Textual>($log_to)) {
|
if($gui.has<Textual>($log_to)) {
|
||||||
auto& text = $gui.get<Textual>($log_to);
|
auto& text = $gui.get<Textual>($log_to);
|
||||||
std::string log;
|
string log;
|
||||||
for(auto msg : $messages) {
|
for(auto msg : $messages) {
|
||||||
log += msg + "\n";
|
log += msg + "\n";
|
||||||
}
|
}
|
||||||
|
@ -79,15 +86,13 @@ namespace gui {
|
||||||
if(world->has<components::Inventory>($level.player)) {
|
if(world->has<components::Inventory>($level.player)) {
|
||||||
auto& inventory = world->get<components::Inventory>($level.player);
|
auto& inventory = world->get<components::Inventory>($level.player);
|
||||||
|
|
||||||
if(inventory.count() > 0) {
|
for(auto& [slot_name, inv_id] : $slots) {
|
||||||
size_t limit = std::min(inventory.count(), $slots.size());
|
if(inventory.has_item(inv_id)) {
|
||||||
|
auto slot = $gui.entity(slot_name);
|
||||||
for(size_t i = 0; i < limit; i++) {
|
auto& item = inventory.get(inv_id);
|
||||||
auto slot = $gui.entity($slots[i]);
|
|
||||||
auto& item = inventory.get(i);
|
|
||||||
auto comp_sprite = components::get<components::Sprite>(item.data);
|
auto comp_sprite = components::get<components::Sprite>(item.data);
|
||||||
$gui.set_init<guecs::Sprite>(slot, {comp_sprite.name});
|
$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);
|
auto& label = $gui.get<Textual>(slot);
|
||||||
label.text->setString(count_label);
|
label.text->setString(count_label);
|
||||||
|
|
||||||
|
@ -107,7 +112,7 @@ namespace gui {
|
||||||
$gui.render(window);
|
$gui.render(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusUI::log(std::string msg) {
|
void StatusUI::log(string msg) {
|
||||||
$messages.push_front(msg);
|
$messages.push_front(msg);
|
||||||
if($messages.size() > MAX_LOG_MESSAGES) {
|
if($messages.size() > MAX_LOG_MESSAGES) {
|
||||||
$messages.pop_back();
|
$messages.pop_back();
|
||||||
|
|
|
@ -10,14 +10,10 @@ namespace gui {
|
||||||
public:
|
public:
|
||||||
guecs::UI $gui;
|
guecs::UI $gui;
|
||||||
DinkyECS::Entity $log_to;
|
DinkyECS::Entity $log_to;
|
||||||
std::array<std::string, 9> $slots = {
|
std::map<std::string, size_t> $slots;
|
||||||
"slot1", "slot2", "slot3",
|
|
||||||
"slot4", "slot5", "slot6",
|
|
||||||
"slot7", "slot8", "slot9"
|
|
||||||
};
|
|
||||||
|
|
||||||
std::deque<std::string> $messages;
|
std::deque<std::string> $messages;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
|
|
||||||
StatusUI(GameLevel level);
|
StatusUI(GameLevel level);
|
||||||
void select_slot(DinkyECS::Entity ent, std::any data);
|
void select_slot(DinkyECS::Entity ent, std::any data);
|
||||||
void update_level(GameLevel &level) { $level = level; }
|
void update_level(GameLevel &level) { $level = level; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue