Starting a bit of refactoring to sort out how to handle the various UIs.

main
Zed A. Shaw 1 week ago
parent d8400d0a76
commit f0829bb9ea
  1. 113
      gui.cpp
  2. 36
      gui.hpp
  3. 3
      panel.hpp

@ -29,49 +29,7 @@ using namespace std::chrono_literals;
using namespace ftxui;
using namespace components;
GUI::GUI(DinkyECS::World &world, Map& game_map) :
$game_map(game_map),
$log({{"Welcome to the game!"}}),
$status_ui(0, 0, STATUS_UI_WIDTH, STATUS_UI_HEIGHT),
$map_view(GAME_MAP_PIXEL_POS, 0, 0, 0, true),
$inventory_ui(INVENTORY_PIXEL_X, INVENTORY_PIXEL_Y, INVENTORY_WIDTH, INVENTORY_HEIGHT),
$lights(game_map.width(), game_map.height()),
$world(world),
$sounds("./assets"),
$renderer()
{
// this needs a config file soon
// $sounds.load("ambient", "ambient_sound.mp3");
$sounds.load("loot_gold", "loot_gold.mp3");
$sounds.load("combat_player_hit", "combat_player_hit.mp3");
$sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3");
$sounds.load("combat_miss", "combat_miss.mp3");
resize_map(MAX_FONT_SIZE);
init_shaders();
}
void GUI::resize_map(int new_size) {
$renderer.resize_grid(new_size, $map_view);
// set canvas to best size
$canvas = Canvas($map_view.width * 2, $map_view.height * 4);
}
void GUI::save_world() {
$log.log("Game saved!");
save::to_file("./savefile.world", $world, $game_map);
}
void GUI::create_renderer() {
$renderer.init_terminal();
auto player = $world.get_the<Player>();
$map_view.set_renderer(Renderer([&] {
System::draw_map($world, $game_map, $lights.lighting(), $canvas, $map_view.width, $map_view.height);
return canvas($canvas);
}));
void InventoryUI::create_render() {
auto cell = [](const char* t) { return text(t) | border; };
auto inventory_test = Renderer([cell] {
return hflow({
@ -85,6 +43,12 @@ void GUI::create_renderer() {
}) | border | flex;
});
set_renderer(inventory_test);
}
void StatusUI::create_render() {
auto player = $world.get_the<Player>();
auto status_rend = Renderer([&, player]{
const auto& player_combat = $world.get<Combat>(player.entity);
const auto& inventory = $world.get<Inventory>(player.entity);
@ -112,8 +76,53 @@ void GUI::create_renderer() {
});
});
$status_ui.set_renderer(status_rend);
$inventory_ui.set_renderer(inventory_test);
set_renderer(status_rend);
}
GUI::GUI(DinkyECS::World &world, Map& game_map) :
$game_map(game_map),
$status_ui(world),
$map_view(),
$inventory_ui(),
$lights(game_map.width(), game_map.height()),
$world(world),
$sounds("./assets"),
$renderer()
{
// this needs a config file soon
// $sounds.load("ambient", "ambient_sound.mp3");
$sounds.load("loot_gold", "loot_gold.mp3");
$sounds.load("combat_player_hit", "combat_player_hit.mp3");
$sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3");
$sounds.load("combat_miss", "combat_miss.mp3");
resize_map(MAX_FONT_SIZE);
init_shaders();
}
void GUI::resize_map(int new_size) {
$renderer.resize_grid(new_size, $map_view);
// set canvas to best size
$canvas = Canvas($map_view.width * 2, $map_view.height * 4);
dbc::log("WHY IS RESIZING LIGHTS STILL WORKING?!!!!!!!!!!!!!!!");
}
void GUI::save_world() {
$status_ui.log("Game saved!");
save::to_file("./savefile.world", $world, $game_map);
}
void GUI::create_renderer() {
$renderer.init_terminal();
$map_view.set_renderer(Renderer([&] {
System::draw_map($world, $game_map, $lights.lighting(), $canvas, $map_view.width, $map_view.height);
return canvas($canvas);
}));
$status_ui.create_render();
$inventory_ui.create_render();
}
void GUI::handle_world_events() {
@ -129,32 +138,32 @@ void GUI::handle_world_events() {
auto enemy_combat = $world.get<Combat>(entity);
if(damage.enemy_did > 0) {
$log.log(format("Enemy HIT YOU for {} damage!", damage.enemy_did));
$log.log(format("-- Enemy has {} HP left.", enemy_combat.hp));
$status_ui.log(format("Enemy HIT YOU for {} damage!", damage.enemy_did));
$status_ui.log(format("-- Enemy has {} HP left.", enemy_combat.hp));
$sounds.play("combat_enemy_hit");
shake();
} else {
$log.log("Enemy MISSED YOU.");
$status_ui.log("Enemy MISSED YOU.");
}
if(damage.player_did > 0) {
$log.log(format("You HIT enemy for {} damage!", damage.player_did));
$status_ui.log(format("You HIT enemy for {} damage!", damage.player_did));
$sounds.play("combat_player_hit");
} else {
$sounds.play("combat_miss");
$log.log("You MISSED the enemy.");
$status_ui.log("You MISSED the enemy.");
}
} break;
case eGUI::LOOT: {
auto &loot = std::any_cast<Loot&>(data);
auto inventory = $world.get<Inventory>(player.entity);
$sounds.play("loot_gold");
$log.log(format("You found {} gold. You have {} now.",
$status_ui.log(format("You found {} gold. You have {} now.",
loot.amount, inventory.gold));
}
break;
default:
$log.log(format("INVALID EVENT! {},{}", evt, entity));
$status_ui.log(format("INVALID EVENT! {},{}", evt, entity));
}
}
}

@ -48,14 +48,40 @@ struct UnDumbTSS {
}
};
class GUI {
class InventoryUI : public Panel {
public:
InventoryUI() : Panel(INVENTORY_PIXEL_X, INVENTORY_PIXEL_Y, INVENTORY_WIDTH, INVENTORY_HEIGHT) {}
void create_render();
};
class StatusUI : public Panel {
public:
ActionLog $log;
string $status_text = "NOT DEAD";
DinkyECS::World& $world;
StatusUI(DinkyECS::World& world) :
Panel(0, 0, STATUS_UI_WIDTH, STATUS_UI_HEIGHT),
$log({{"Welcome to the game!"}}),
$world(world) {}
void create_render();
void log(string msg) {
$log.log(msg);
}
};
class MapViewUI : public Panel {
public:
MapViewUI() : Panel(GAME_MAP_PIXEL_POS, 0, 0, 0, true) {}
};
class GUI {
Canvas $canvas;
Map& $game_map;
ActionLog $log;
Panel $status_ui;
Panel $map_view;
Panel $inventory_ui;
StatusUI $status_ui;
MapViewUI $map_view;
InventoryUI $inventory_ui;
LightRender $lights;
bool $show_modal = false;
Component $test_button;

@ -17,7 +17,8 @@ const int UI_PANEL_BORDER_PX=5;
using ftxui::Renderer, ftxui::Component, ftxui::Element, ftxui::Screen;
struct Panel {
class Panel {
public:
int x;
int y;
int width;

Loading…
Cancel
Save