From 9a012813aed786ebc5c1b822692b37f1227a1971 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 22 Aug 2024 18:34:20 -0400 Subject: [PATCH] Game now keeps track of deaths, rounds, streaks and other fun stuff. You can also die finally. --- builder.cpp | 9 +++++ game_engine.cpp | 91 ++++++++++++++++++++++++------------------- game_engine.hpp | 42 +++++++++++--------- gui.cpp | 6 +-- tests/game_engine.cpp | 4 ++ 5 files changed, 92 insertions(+), 60 deletions(-) diff --git a/builder.cpp b/builder.cpp index 5905cbd..a4b0153 100644 --- a/builder.cpp +++ b/builder.cpp @@ -107,11 +107,20 @@ void Builder::run(const char *git_path, const char *build_cmd) { fileWatcher->watch(); if(listener->changes) { + gui.output("############# START ############"); std::this_thread::sleep_for(std::chrono::milliseconds(100)); gui.output(format("CHANGES! Running build {}", build_cmd)); run_build(game, build_cmd); + + if(game.is_dead()) { + gui.output("!!!! YOU DIED! !!!! Learn to code luser."); + game.reset(); + } + listener->reset_state(); + gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^"); } + return 0; }); diff --git a/game_engine.cpp b/game_engine.cpp index 892a80a..07ffc7b 100644 --- a/game_engine.cpp +++ b/game_engine.cpp @@ -10,6 +10,58 @@ const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); using namespace fmt; using namespace std; +GameEngine::GameEngine(int hp) : starting_hp(hp) { + hit_points = starting_hp; +}; + +int GameEngine::determine_damage(string &type) { + try { + return damage_types.at(type); + } catch(std::out_of_range &err) { + print(ERROR, "BAD DAMAGE TYPE {}\n", type); + return 1000; + } +} + +void GameEngine::start_round() { + hits_taken = 0; +} + +void GameEngine::end_round() { + ++rounds; + if(hits_taken == 0) { + ++streak; + heal(); + } +} + +void GameEngine::reset() { + rounds = 0; + streak = 0; + hit_points = starting_hp; + hits_taken = 0; +} + +bool GameEngine::hit(string &type) { + int damage = determine_damage(type); + hit_points -= damage; + ++hits_taken; + streak = 0; + + return is_dead(); +} + +void GameEngine::heal() { + hit_points = hit_points * 1.10; + if(hit_points > 100) hit_points = 100; +} + +bool GameEngine::is_dead() { + return hit_points <= 0; +} + + + Brainfucker::Brainfucker() { } @@ -124,42 +176,3 @@ void Brainfucker::reset() { string Brainfucker::to_string() { return out.str(); } - -GameEngine::GameEngine(int hp) : hit_points(hp) {}; - -int GameEngine::determine_damage(string &type) { - try { - return damage_types.at(type); - } catch(std::out_of_range &err) { - print(ERROR, "BAD DAMAGE TYPE {}\n", type); - return 1000; - } -} - -void GameEngine::start_round() { - hits_taken = 0; -} - -void GameEngine::end_round() { - if(hits_taken == 0) { - heal(); - } -} - -bool GameEngine::hit(string &type) { - int damage = determine_damage(type); - hit_points -= damage; - ++hits_taken; - - // super dumb but I'll clean it up later - return is_dead(); -} - -void GameEngine::heal() { - hit_points = hit_points * 1.10; - if(hit_points > 100) hit_points = 100; -} - -bool GameEngine::is_dead() { - return hit_points <= 0; -} diff --git a/game_engine.hpp b/game_engine.hpp index 604e3c6..3acc373 100644 --- a/game_engine.hpp +++ b/game_engine.hpp @@ -8,24 +8,6 @@ using namespace std; -class Brainfucker { - - public: - size_t dp = 0; - size_t ip = 0; - ostringstream out; - array data = {}; - string code = {}; - - Brainfucker(); - - void run(int count); - void set_code(string &code); - void reset(); - void jump_backward(); - void jump_forward(); - string to_string(); -}; class GameEngine { map damage_types{ @@ -35,8 +17,11 @@ class GameEngine { }; public: + int starting_hp = 0; int hit_points = 0; int hits_taken = 0; + int rounds = 0; + int streak = 0; GameEngine(int hp); @@ -51,4 +36,25 @@ class GameEngine { bool is_dead(); void heal(); + + void reset(); +}; + +class Brainfucker { + + public: + size_t dp = 0; + size_t ip = 0; + ostringstream out; + array data = {}; + string code = {}; + + Brainfucker(); + + void run(int count); + void set_code(string &code); + void reset(); + void jump_backward(); + void jump_forward(); + string to_string(); }; diff --git a/gui.cpp b/gui.cpp index 7d233d0..22a8d3c 100644 --- a/gui.cpp +++ b/gui.cpp @@ -33,7 +33,7 @@ int GUI::main_loop(GameEngine &game, std::function runner) { auto status = Renderer([&] { return vbox({ - text(fmt::format("HP {}", game.hit_points)), + text(fmt::format("HP {} | Hits Taken {} | Round {} | Streak {}", game.hit_points, game.hits_taken, game.rounds, game.streak)), separator(), hbox({ text("HP "), @@ -73,9 +73,9 @@ int GUI::main_loop(GameEngine &game, std::function runner) { auto component = Renderer(build_log, [&] { return vbox({ - game_stuff->Render() | flex, + game_stuff->Render() | flex | size(HEIGHT, GREATER_THAN, 20), separator(), - build_log->Render() | vscroll_indicator | yframe | flex, + build_log->Render() | vscroll_indicator | yframe | yflex_grow, separator(), status->Render() }) | border; diff --git a/tests/game_engine.cpp b/tests/game_engine.cpp index a17da6b..12d7ed7 100644 --- a/tests/game_engine.cpp +++ b/tests/game_engine.cpp @@ -61,11 +61,15 @@ TEST_CASE("", "[game_engine]") { game.end_round(); REQUIRE(game.hit_points < 100); + REQUIRE(game.rounds == 1); + REQUIRE(game.streak == 0); REQUIRE(game.is_dead() == false); game.start_round(); game.end_round(); REQUIRE(game.hit_points == 100); + REQUIRE(game.rounds == 2); + REQUIRE(game.streak == 1); REQUIRE(game.is_dead() == false); }