UI is now working the same as last time but using GUECS.
This commit is contained in:
parent
70c2ce7d51
commit
8f3a3c10c2
5 changed files with 41 additions and 18 deletions
|
@ -117,6 +117,8 @@ void Builder::START(BuildEvent) {
|
|||
wid = fileWatcher->addWatch(git_path, listener, true);
|
||||
fileWatcher->watch();
|
||||
|
||||
gui.update_status(game);
|
||||
|
||||
state(BuildState::WAITING);
|
||||
}
|
||||
|
||||
|
@ -167,6 +169,8 @@ void Builder::READING(BuildEvent) {
|
|||
}
|
||||
|
||||
void Builder::DONE(BuildEvent) {
|
||||
gui.update_status(game);
|
||||
|
||||
if(game.is_dead()) {
|
||||
gui.you_died();
|
||||
}
|
||||
|
|
11
guecs.cpp
11
guecs.cpp
|
@ -206,9 +206,10 @@ namespace guecs {
|
|||
});
|
||||
|
||||
$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
|
||||
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);
|
||||
});
|
||||
|
||||
|
@ -264,7 +265,11 @@ namespace guecs {
|
|||
void UI::show_sprite(string region, string sprite_name) {
|
||||
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};
|
||||
auto& cell = cell_for(ent);
|
||||
to_show.init(cell);
|
||||
|
|
|
@ -68,7 +68,7 @@ namespace guecs {
|
|||
|
||||
struct Meter {
|
||||
float percent = 1.0f;
|
||||
Rectangle bar;
|
||||
Rectangle bar{};
|
||||
|
||||
void init(lel::Cell& cell);
|
||||
};
|
||||
|
@ -225,8 +225,6 @@ namespace guecs {
|
|||
|
||||
void show_sprite(string region, string sprite_name);
|
||||
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);
|
||||
};
|
||||
|
||||
|
|
34
gui.cpp
34
gui.cpp
|
@ -2,6 +2,7 @@
|
|||
#include <stdlib.h> // for EXIT_SUCCESS
|
||||
#include <chrono> // for milliseconds
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/xchar.h>
|
||||
#include <memory> // for allocator, shared_ptr
|
||||
#include <string> // for operator+, to_string
|
||||
#include <vector>
|
||||
|
@ -19,7 +20,7 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
|
|||
|
||||
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
$gui.layout(
|
||||
"[*%(200,300)face|_|*%(100,300)stats|*%(200,500)log|_]"
|
||||
"[*%(200,300)face|_|*%(100,300)status|*%(200,500)log|_]"
|
||||
"[_|_|_|_|_]"
|
||||
"[_|_|_|_|_]"
|
||||
"[*%(300,200)clock|_|_|_|_]"
|
||||
|
@ -36,8 +37,8 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
|
|||
auto face = $gui.entity("face");
|
||||
$gui.set<Sprite>(face, {"building"});
|
||||
|
||||
auto stats = $gui.entity("stats");
|
||||
$gui.set<Textual>(stats, {L"STATS"});
|
||||
auto status = $gui.entity("status");
|
||||
$gui.set<Textual>(status, {L""});
|
||||
|
||||
auto log = $gui.entity("log");
|
||||
$gui.set<Textual>(log, {L"LOG"});
|
||||
|
@ -45,8 +46,8 @@ GUI::GUI(SFMLBackend &backend) : sfml(backend) {
|
|||
auto clock = $gui.entity("clock");
|
||||
$gui.set<Label>(clock, {L"00:00:00", 110});
|
||||
|
||||
auto hp_bar = $gui.entity("hp_bar");
|
||||
$gui.set<Meter>(hp_bar, {});
|
||||
$hp_bar = $gui.entity("hp_bar");
|
||||
$gui.set<Meter>($hp_bar, {1.0f, {10, ColorValue::LIGHT_DARK}});
|
||||
|
||||
$gui.init();
|
||||
}
|
||||
|
@ -57,7 +58,13 @@ void GUI::output(const string msg) {
|
|||
}
|
||||
|
||||
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.debug_layout(sfml.window);
|
||||
sfml.handle_events();
|
||||
// sfml.update_entities();
|
||||
|
@ -65,14 +72,14 @@ void GUI::main_loop() {
|
|||
}
|
||||
|
||||
void GUI::build_success() {
|
||||
sfml.change_face("build_success");
|
||||
$gui.show_sprite("face", "build_success");
|
||||
sound::stop("building");
|
||||
sound::play("build_success");
|
||||
output("BUILD FINISHED!");
|
||||
}
|
||||
|
||||
void GUI::build_failed(bool play_sound, const string &command) {
|
||||
sfml.change_face("build_failed");
|
||||
$gui.show_sprite("face", "build_failed");
|
||||
sound::stop("building");
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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() {
|
||||
sfml.change_face("you_died");
|
||||
$gui.show_sprite("face", "you_died");
|
||||
sound::stop("building");
|
||||
sound::play("you_died");
|
||||
output("!!!! YOU DIED! !!!! Learn to code luser.");
|
||||
|
@ -91,7 +107,7 @@ void GUI::you_died() {
|
|||
}
|
||||
|
||||
void GUI::building() {
|
||||
sfml.change_face("building");
|
||||
$gui.show_sprite("face", "building");
|
||||
output("############# START ############");
|
||||
output(">>>> Will it Build?");
|
||||
sound::play("building");
|
||||
|
|
6
gui.hpp
6
gui.hpp
|
@ -13,15 +13,15 @@ class GUI {
|
|||
std::vector<string> _lines;
|
||||
SFMLBackend &sfml;
|
||||
guecs::UI $gui;
|
||||
std::wstring $status;
|
||||
DinkyECS::Entity $hp_bar;
|
||||
|
||||
public:
|
||||
|
||||
GUI(SFMLBackend &backend);
|
||||
|
||||
// prevent copy
|
||||
GUI(GUI &g) = delete;
|
||||
|
||||
void output(const string msg);
|
||||
void update_status(GameEngine &game);
|
||||
|
||||
void main_loop();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue