|
|
|
#include "gui.hpp"
|
|
|
|
#include <stdlib.h> // for EXIT_SUCCESS
|
|
|
|
#include <chrono> // for milliseconds
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <memory> // for allocator, shared_ptr
|
|
|
|
#include <string> // for operator+, to_string
|
|
|
|
#include <vector>
|
|
|
|
#include <SFML/Audio.hpp>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include "sfmlgui.hpp"
|
|
|
|
|
|
|
|
using std::string, std::vector;
|
|
|
|
|
|
|
|
using namespace fmt;
|
|
|
|
using namespace nlohmann;
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
void SoundQuip::load(json &data, const char *file_key) {
|
|
|
|
auto audio = data["audio"];
|
|
|
|
json::string_t file_name = audio[file_key].template get<string>();
|
|
|
|
|
|
|
|
if(!buffer.loadFromFile(file_name)) {
|
|
|
|
println("Failed to load sound: {} with file {}", file_key, file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
sound.setBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundQuip::play() {
|
|
|
|
sound.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundQuip::stop() {
|
|
|
|
sound.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI::GUI() {
|
|
|
|
std::ifstream infile(".tarpit.json");
|
|
|
|
json data = json::parse(infile);
|
|
|
|
|
|
|
|
// json load the config file
|
|
|
|
you_died_sound.load(data, "you_died");
|
|
|
|
build_works_sound.load(data, "build_works");
|
|
|
|
build_failed_sound.load(data, "build_failed");
|
|
|
|
building_sound.load(data, "building");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::output(const string &msg) {
|
|
|
|
lines.push_back(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GUI::main_loop(GameEngine &game, std::function<bool()> runner) {
|
|
|
|
auto gui = SFMLGui(game);
|
|
|
|
|
|
|
|
gui.startup();
|
|
|
|
|
|
|
|
while(gui.is_open()) {
|
|
|
|
bool result = runner();
|
|
|
|
gui.handle_events();
|
|
|
|
gui.update_entities();
|
|
|
|
gui.update_log(lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
gui.shutdown();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::build_works() {
|
|
|
|
building_sound.stop();
|
|
|
|
build_works_sound.play();
|
|
|
|
output("BUILD FINISHED!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::build_failed(bool play_sound, const string &command) {
|
|
|
|
building_sound.stop();
|
|
|
|
|
|
|
|
if(play_sound) {
|
|
|
|
build_failed_sound.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
output(format("!!! BUILD FAILED. Your command correct? '{}'", command));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::you_died() {
|
|
|
|
building_sound.stop();
|
|
|
|
you_died_sound.play();
|
|
|
|
output("!!!! YOU DIED! !!!! Learn to code luser.");
|
|
|
|
output("YOU DIED!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::building() {
|
|
|
|
output("############# START ############");
|
|
|
|
output(">>>> Will it Build?");
|
|
|
|
building_sound.play();
|
|
|
|
}
|