UI is now working the same as last time but using GUECS.

master
Zed A. Shaw 2 days ago
parent 70c2ce7d51
commit 8f3a3c10c2
  1. 4
      builder.cpp
  2. 11
      guecs.cpp
  3. 4
      guecs.hpp
  4. 34
      gui.cpp
  5. 6
      gui.hpp

@ -117,6 +117,8 @@ void Builder::START(BuildEvent) {
wid = fileWatcher->addWatch(git_path, listener, true); wid = fileWatcher->addWatch(git_path, listener, true);
fileWatcher->watch(); fileWatcher->watch();
gui.update_status(game);
state(BuildState::WAITING); state(BuildState::WAITING);
} }
@ -167,6 +169,8 @@ void Builder::READING(BuildEvent) {
} }
void Builder::DONE(BuildEvent) { void Builder::DONE(BuildEvent) {
gui.update_status(game);
if(game.is_dead()) { if(game.is_dead()) {
gui.you_died(); gui.you_died();
} }

@ -206,9 +206,10 @@ namespace guecs {
}); });
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) { $world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) {
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w); int pad = meter.bar.padding * 2;
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w - pad);
// ZED: this 6 is a border width, make it a thing // ZED: this 6 is a border width, make it a thing
meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - pad)});
render_helper(window, ent, true, meter.bar.shape); render_helper(window, ent, true, meter.bar.shape);
}); });
@ -264,7 +265,11 @@ namespace guecs {
void UI::show_sprite(string region, string sprite_name) { void UI::show_sprite(string region, string sprite_name) {
auto ent = entity(region); auto ent = entity(region);
if(!has<Sprite>(ent)) { if(has<Sprite>(ent)) {
auto& to_show = get<Sprite>(ent);
auto sprite_texture = textures::get(sprite_name);
to_show.sprite->setTexture(*sprite_texture.texture);
} else {
Sprite to_show{sprite_name}; Sprite to_show{sprite_name};
auto& cell = cell_for(ent); auto& cell = cell_for(ent);
to_show.init(cell); to_show.init(cell);

@ -68,7 +68,7 @@ namespace guecs {
struct Meter { struct Meter {
float percent = 1.0f; float percent = 1.0f;
Rectangle bar; Rectangle bar{};
void init(lel::Cell& cell); void init(lel::Cell& cell);
}; };
@ -225,8 +225,6 @@ namespace guecs {
void show_sprite(string region, string sprite_name); void show_sprite(string region, string sprite_name);
void show_text(string region, wstring content); void show_text(string region, wstring content);
void update_text(string region, wstring content);
void update_label(string region, wstring content);
void show_label(string region, wstring content); void show_label(string region, wstring content);
}; };

@ -2,6 +2,7 @@
#include <stdlib.h> // for EXIT_SUCCESS #include <stdlib.h> // for EXIT_SUCCESS
#include <chrono> // for milliseconds #include <chrono> // for milliseconds
#include <fmt/core.h> #include <fmt/core.h>
#include <fmt/xchar.h>
#include <memory> // for allocator, shared_ptr #include <memory> // for allocator, shared_ptr
#include <string> // for operator+, to_string #include <string> // for operator+, to_string
#include <vector> #include <vector>
@ -19,7 +20,7 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); $gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
$gui.layout( $gui.layout(
"[*%(200,300)face|_|*%(100,300)stats|*%(200,500)log|_]" "[*%(200,300)face|_|*%(100,300)status|*%(200,500)log|_]"
"[_|_|_|_|_]" "[_|_|_|_|_]"
"[_|_|_|_|_]" "[_|_|_|_|_]"
"[*%(300,200)clock|_|_|_|_]" "[*%(300,200)clock|_|_|_|_]"
@ -36,8 +37,8 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
auto face = $gui.entity("face"); auto face = $gui.entity("face");
$gui.set<Sprite>(face, {"building"}); $gui.set<Sprite>(face, {"building"});
auto stats = $gui.entity("stats"); auto status = $gui.entity("status");
$gui.set<Textual>(stats, {L"STATS"}); $gui.set<Textual>(status, {L""});
auto log = $gui.entity("log"); auto log = $gui.entity("log");
$gui.set<Textual>(log, {L"LOG"}); $gui.set<Textual>(log, {L"LOG"});
@ -45,8 +46,8 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
auto clock = $gui.entity("clock"); auto clock = $gui.entity("clock");
$gui.set<Label>(clock, {L"00:00:00", 110}); $gui.set<Label>(clock, {L"00:00:00", 110});
auto hp_bar = $gui.entity("hp_bar"); $hp_bar = $gui.entity("hp_bar");
$gui.set<Meter>(hp_bar, {}); $gui.set<Meter>($hp_bar, {1.0f, {10, ColorValue::LIGHT_DARK}});
$gui.init(); $gui.init();
} }
@ -57,7 +58,13 @@ void GUI::output(const string msg) {
} }
void GUI::main_loop() { void GUI::main_loop() {
auto clock_time = std::chrono::system_clock::now();
std::wstring time = std::format(L"{:%H:%M:%OS}", clock_time);
$gui.show_label("clock", time);
$gui.show_text("status", $status);
$gui.render(sfml.window); $gui.render(sfml.window);
// $gui.debug_layout(sfml.window); // $gui.debug_layout(sfml.window);
sfml.handle_events(); sfml.handle_events();
// sfml.update_entities(); // sfml.update_entities();
@ -65,14 +72,14 @@ void GUI::main_loop() {
} }
void GUI::build_success() { void GUI::build_success() {
sfml.change_face("build_success"); $gui.show_sprite("face", "build_success");
sound::stop("building"); sound::stop("building");
sound::play("build_success"); sound::play("build_success");
output("BUILD FINISHED!"); output("BUILD FINISHED!");
} }
void GUI::build_failed(bool play_sound, const string &command) { void GUI::build_failed(bool play_sound, const string &command) {
sfml.change_face("build_failed"); $gui.show_sprite("face", "build_failed");
sound::stop("building"); sound::stop("building");
if(play_sound) { if(play_sound) {
@ -82,8 +89,17 @@ void GUI::build_failed(bool play_sound, const string &command) {
output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command)); output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command));
} }
void GUI::update_status(GameEngine &game) {
$status = fmt::format(L"HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds,
game.streak, game.deaths);
auto& meter = $gui.get<guecs::Meter>($hp_bar);
meter.percent = float(game.hit_points) / float(game.max_hp());
}
void GUI::you_died() { void GUI::you_died() {
sfml.change_face("you_died"); $gui.show_sprite("face", "you_died");
sound::stop("building"); sound::stop("building");
sound::play("you_died"); sound::play("you_died");
output("!!!! YOU DIED! !!!! Learn to code luser."); output("!!!! YOU DIED! !!!! Learn to code luser.");
@ -91,7 +107,7 @@ void GUI::you_died() {
} }
void GUI::building() { void GUI::building() {
sfml.change_face("building"); $gui.show_sprite("face", "building");
output("############# START ############"); output("############# START ############");
output(">>>> Will it Build?"); output(">>>> Will it Build?");
sound::play("building"); sound::play("building");

@ -13,15 +13,15 @@ class GUI {
std::vector<string> _lines; std::vector<string> _lines;
SFMLBackend &sfml; SFMLBackend &sfml;
guecs::UI $gui; guecs::UI $gui;
std::wstring $status;
DinkyECS::Entity $hp_bar;
public: public:
GUI(SFMLBackend &backend); GUI(SFMLBackend &backend);
// prevent copy
GUI(GUI &g) = delete;
void output(const string msg); void output(const string msg);
void update_status(GameEngine &game);
void main_loop(); void main_loop();

Loading…
Cancel
Save