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) {
auto& world = $gui.world();
world.set_the<Background>({$gui.$parser});
for(auto& [name, cell] : $gui.cells()) {
if(name.starts_with("button_")) {
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 MAX_FONT_SIZE = 140;
constexpr int MIN_FONT_SIZE = 20;
constexpr int STATUS_UI_WIDTH = 29;
constexpr int STATUS_UI_HEIGHT = 25;
constexpr int STATUS_UI_X = 43;
constexpr int STATUS_UI_Y = 200;
constexpr int STATUS_UI_WIDTH = SCREEN_WIDTH - RAY_VIEW_WIDTH;
constexpr int STATUS_UI_HEIGHT = SCREEN_HEIGHT;
constexpr int STATUS_UI_X = 0;
constexpr int STATUS_UI_Y = 0;
constexpr float PERCENT = 0.01f;
constexpr int COMBAT_UI_WIDTH = 89;
constexpr int COMBAT_UI_HEIGHT = 6;

@ -22,6 +22,15 @@ namespace guecs {
}
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) {
rect.init(cell);
});
@ -49,6 +58,11 @@ namespace guecs {
}
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) {
window.draw(*rect.shape);
});

@ -42,10 +42,9 @@ namespace guecs {
shared_ptr<sf::RectangleShape> shape = nullptr;
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);
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->setOutlineColor(ColorValue::MID);
shape->setOutlineThickness(1);
@ -65,6 +64,31 @@ namespace guecs {
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 {
public:
DinkyECS::World $world;

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

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

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

Loading…
Cancel
Save