Added a Background guecs component.

master
Zed A. Shaw 3 weeks ago
parent 69a810b5a1
commit 3a6ba8445a
  1. 2
      combat_ui.cpp
  2. 8
      constants.hpp
  3. 14
      guecs.cpp
  4. 28
      guecs.hpp
  5. 15
      gui.cpp
  6. 64
      status_ui.cpp
  7. 11
      status_ui.hpp

@ -18,6 +18,8 @@ namespace gui {
void CombatUI::render(TexturePack& textures) { void CombatUI::render(TexturePack& textures) {
auto& world = $gui.world(); auto& world = $gui.world();
world.set_the<Background>({$gui.$parser});
for(auto& [name, cell] : $gui.cells()) { for(auto& [name, cell] : $gui.cells()) {
if(name.starts_with("button_")) { if(name.starts_with("button_")) {
auto button = $gui.entity(name); auto button = $gui.entity(name);

@ -38,10 +38,10 @@ constexpr int BASE_MAP_FONT_SIZE=90;
constexpr int GAME_MAP_PIXEL_POS = 600; constexpr int GAME_MAP_PIXEL_POS = 600;
constexpr int MAX_FONT_SIZE = 140; constexpr int MAX_FONT_SIZE = 140;
constexpr int MIN_FONT_SIZE = 20; constexpr int MIN_FONT_SIZE = 20;
constexpr int STATUS_UI_WIDTH = 29; constexpr int STATUS_UI_WIDTH = SCREEN_WIDTH - RAY_VIEW_WIDTH;
constexpr int STATUS_UI_HEIGHT = 25; constexpr int STATUS_UI_HEIGHT = SCREEN_HEIGHT;
constexpr int STATUS_UI_X = 43; constexpr int STATUS_UI_X = 0;
constexpr int STATUS_UI_Y = 200; constexpr int STATUS_UI_Y = 0;
constexpr float PERCENT = 0.01f; constexpr float PERCENT = 0.01f;
constexpr int COMBAT_UI_WIDTH = 89; constexpr int COMBAT_UI_WIDTH = 89;
constexpr int COMBAT_UI_HEIGHT = 6; constexpr int COMBAT_UI_HEIGHT = 6;

@ -22,6 +22,15 @@ namespace guecs {
} }
void UI::init(TexturePack& textures) { void UI::init(TexturePack& textures) {
if($world.has_the<Background>()) {
auto& bg = $world.get_the<Background>();
bg.init();
}
$world.query<Background>([](auto, auto& bg) {
bg.init();
});
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) { $world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
rect.init(cell); rect.init(cell);
}); });
@ -49,6 +58,11 @@ namespace guecs {
} }
void UI::render(sf::RenderWindow& window) { void UI::render(sf::RenderWindow& window) {
if($world.has_the<Background>()) {
auto& bg = $world.get_the<Background>();
window.draw(*bg.shape);
}
$world.query<Rectangle>([&](auto, auto& rect) { $world.query<Rectangle>([&](auto, auto& rect) {
window.draw(*rect.shape); window.draw(*rect.shape);
}); });

@ -42,10 +42,9 @@ namespace guecs {
shared_ptr<sf::RectangleShape> shape = nullptr; shared_ptr<sf::RectangleShape> shape = nullptr;
void init(lel::Cell& cell) { void init(lel::Cell& cell) {
sf::Vector2f size{(float)cell.w, (float)cell.h}; sf::Vector2f size{float(cell.w) - 6, float(cell.h) - 6};
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
shape->setPosition({float(cell.x + 3), float(cell.y + 3)}); shape->setPosition({float(cell.x + 3), float(cell.y + 3)});
shape->setSize({float(cell.w - 6), float(cell.h - 6)});
shape->setFillColor(ColorValue::DARK_MID); shape->setFillColor(ColorValue::DARK_MID);
shape->setOutlineColor(ColorValue::MID); shape->setOutlineColor(ColorValue::MID);
shape->setOutlineThickness(1); shape->setOutlineThickness(1);
@ -65,6 +64,31 @@ namespace guecs {
std::string name; std::string name;
}; };
struct Background {
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
shared_ptr<sf::RectangleShape> shape = nullptr;
Background(lel::Parser& parser) :
x(parser.grid_x),
y(parser.grid_y),
w(parser.grid_w),
h(parser.grid_h)
{}
Background() {}
void init() {
sf::Vector2f size{float(w), float(h)};
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
shape->setPosition({float(x), float(y)});
shape->setFillColor(ColorValue::MID);
}
};
class UI { class UI {
public: public:
DinkyECS::World $world; DinkyECS::World $world;

@ -53,7 +53,7 @@ namespace gui {
$combat_view.render($textures); $combat_view.render($textures);
$status_view.create_render(); $status_view.render($textures);
$status_view.log("Welcome to the game!"); $status_view.log("Welcome to the game!");
$renderer.init_terminal(); $renderer.init_terminal();
@ -323,18 +323,7 @@ namespace gui {
} }
void FSM::draw_gui() { void FSM::draw_gui() {
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT}); $status_view.draw($window);
rect.setPosition({0,0});
rect.setFillColor({30, 30, 30});
$window.draw(rect);
auto left_gui = $textures.sprite_textures.at("left_gui").sprite;
left_gui->setPosition({0,0});
$window.draw(*left_gui);
$status_view.render();
$renderer.draw($status_view);
$combat_view.draw($window); $combat_view.draw($window);
auto debug = $level.world->get_the<Debug>(); auto debug = $level.world->get_the<Debug>();

@ -1,46 +1,41 @@
#include "status_ui.hpp" #include "status_ui.hpp"
#include <ftxui/dom/node.hpp> // for Render
#include <ftxui/screen/box.hpp> // for ftxui
#include <ftxui/component/loop.hpp>
#include <ftxui/screen/color.hpp>
#include <ftxui/dom/table.hpp>
#include "components.hpp" #include "components.hpp"
#include "color.hpp" #include "color.hpp"
#include "guecs.hpp"
namespace gui { namespace gui {
using namespace components; using namespace guecs;
using namespace ftxui;
StatusUI::StatusUI(GameLevel level) : StatusUI::StatusUI(GameLevel level) :
Panel(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT, false),
$level(level) $level(level)
{ {
default_bg = ColorValue::DARK_MID; $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
$gui.layout("[log_view]");
} }
void StatusUI::create_render() { void StatusUI::render(TexturePack &textures) {
auto player = $level.world->get_the<Player>(); auto& world = $gui.world();
auto& cell = $gui.cells().at("log_view");
auto status_rend = Renderer([&, player]{ $log_to = $gui.entity("log_view");
const auto& player_combat = $level.world->get<Combat>(player.entity); world.set<lel::Cell>($log_to, cell);
const auto& combat = $level.world->get<Combat>(player.entity); world.set<Rectangle>($log_to, {});
world.set<Textual>($log_to, {"TEST"});
auto log_box = vbox($log_list) | yflex_grow | border;
$gui.init(textures);
return hbox({ }
hflow(
vbox( void StatusUI::draw(sf::RenderWindow &window) {
text(fmt::format("HP: {: >3} DMG: {: >3}", auto &world = $gui.world();
player_combat.hp, auto &text = world.get<Textual>($log_to);
combat.damage)), std::string log;
separator(),
log_box for(auto msg : $messages) {
) | flex_grow log += msg + "\n";
) }
});
}); text.label = log;
set_renderer(status_rend); $gui.render(window);
} }
void StatusUI::log(std::string msg) { void StatusUI::log(std::string msg) {
@ -48,10 +43,5 @@ namespace gui {
if($messages.size() > MAX_LOG_MESSAGES) { if($messages.size() > MAX_LOG_MESSAGES) {
$messages.pop_back(); $messages.pop_back();
} }
$log_list.clear();
for(auto msg : $messages) {
$log_list.push_back(text(msg));
}
} }
} }

@ -1,18 +1,21 @@
#pragma once #pragma once
#include "panel.hpp"
#include "levelmanager.hpp" #include "levelmanager.hpp"
#include "constants.hpp" #include "constants.hpp"
#include <deque> #include <deque>
#include "texture.hpp"
#include "guecs.hpp"
namespace gui { namespace gui {
class StatusUI : public Panel { class StatusUI {
public: public:
std::vector<Element> $log_list; guecs::UI $gui;
DinkyECS::Entity $log_to;
std::deque<std::string> $messages; std::deque<std::string> $messages;
GameLevel $level; GameLevel $level;
StatusUI(GameLevel level); StatusUI(GameLevel level);
void create_render();
void update_level(GameLevel &level) { $level = level; } void update_level(GameLevel &level) { $level = level; }
void log(std::string msg); void log(std::string msg);
void render(TexturePack &textures);
void draw(sf::RenderWindow &window);
}; };
} }

Loading…
Cancel
Save