A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
turings-tarpit/gui.cpp

160 lines
4.1 KiB

#include "gui.hpp"
#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>
#include <SFML/Audio.hpp>
#include <nlohmann/json.hpp>
#include "builder.hpp"
#include <fstream>
#include <iostream>
#include "sound.hpp"
#include "shaders.hpp"
using std::string, std::vector;
GUI::GUI(int timer_seconds) :
$timer_seconds(timer_seconds),
$window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Turing's Tarpit")
{
using namespace guecs;
$timer_end = std::chrono::system_clock::now() + std::chrono::seconds(timer_seconds);
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
$gui.layout(
"[*%(200,300)face|_|*%(100,300)status|*%(200,500)log|_]"
"[_|_|_|_|_]"
"[_|_|_|_|_]"
"[*%(300,200)clock|_|_|_|_]"
"[_|_|_|_|_]"
"[hp_bar]");
$gui.world().set_the<Background>({$gui.$parser, {0,0,0,0}});
for(auto& [name, cell] : $gui.cells()) {
auto ent = $gui.entity(name);
$gui.set<Rectangle>(ent, {});
}
auto face = $gui.entity("face");
$gui.set<Sprite>(face, {"building"});
auto status = $gui.entity("status");
$gui.set<Textual>(status, {L""});
$log = $gui.entity("log");
auto& rect = $gui.get<Rectangle>($log);
rect.color = {255,255,255,255};
$gui.set<Effect>($log, {(float)$timer_seconds, "build_status"});
auto clock = $gui.entity("clock");
$gui.set<Label>(clock, {L"00:00:00", 110});
$hp_bar = $gui.entity("hp_bar");
$gui.set<Meter>($hp_bar, {1.0f, {10, ColorValue::LIGHT_DARK}});
$gui.init();
}
void GUI::output(const string msg) {
std::cout << msg << std::endl;
}
void GUI::main_loop() {
auto clock_time = $timer_end - 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($window);
$window.display();
handle_events();
}
void GUI::build_success() {
$hit_error = false;
configure_status_shader(1, $hit_error);
$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) {
$hit_error = true;
configure_status_shader(1, $hit_error);
$gui.show_sprite("face", "build_failed");
sound::stop("building");
if(play_sound) {
sound::play("build_failed");
}
output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command));
}
void GUI::configure_status_shader(int speedup, bool is_error) {
auto& effect = $gui.get<guecs::Effect>($log);
if(speedup > 0) {
effect.$shader->setUniform("speedup", speedup);
}
if(!$hit_error && is_error) {
$hit_error = is_error;
}
effect.$shader->setUniform("is_error", $hit_error);
effect.run();
}
void GUI::update_status(GameEngine &game, size_t line_length, bool is_error) {
configure_status_shader(line_length * 0, is_error);
$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() {
configure_status_shader(0, $hit_error);
$gui.show_sprite("face", "you_died");
sound::stop("building");
sound::play("you_died");
output("!!!! YOU DIED! !!!! Learn to code luser.");
output("YOU DIED!");
}
void GUI::building() {
$hit_error = false;
configure_status_shader(20, $hit_error);
$gui.show_sprite("face", "building");
output("############# START ############");
output(">>>> Will it Build?");
sound::play("building");
}
void GUI::handle_events() {
// is this a main event loop
while(const auto ev = $window.pollEvent()) {
if(ev->is<sf::Event::Closed>()) {
fmt::print("Exiting...\n");
$window.close();
}
}
}
void GUI::startup() {
$window.setPosition({0,0});
$window.setFramerateLimit(FRAME_LIMIT);
$window.setVerticalSyncEnabled(VSYNC);
}