Game now keeps track of deaths, rounds, streaks and other fun stuff. You can also die finally.

master
Zed A. Shaw 3 months ago
parent a13704fe33
commit 9a012813ae
  1. 9
      builder.cpp
  2. 91
      game_engine.cpp
  3. 42
      game_engine.hpp
  4. 6
      gui.cpp
  5. 4
      tests/game_engine.cpp

@ -107,11 +107,20 @@ void Builder::run(const char *git_path, const char *build_cmd) {
fileWatcher->watch(); fileWatcher->watch();
if(listener->changes) { if(listener->changes) {
gui.output("############# START ############");
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
gui.output(format("CHANGES! Running build {}", build_cmd)); gui.output(format("CHANGES! Running build {}", build_cmd));
run_build(game, build_cmd); run_build(game, build_cmd);
if(game.is_dead()) {
gui.output("!!!! YOU DIED! !!!! Learn to code luser.");
game.reset();
}
listener->reset_state(); listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
} }
return 0; return 0;
}); });

@ -10,6 +10,58 @@ const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
using namespace fmt; using namespace fmt;
using namespace std; 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() { Brainfucker::Brainfucker() {
} }
@ -124,42 +176,3 @@ void Brainfucker::reset() {
string Brainfucker::to_string() { string Brainfucker::to_string() {
return out.str(); 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;
}

@ -8,24 +8,6 @@
using namespace std; using namespace std;
class Brainfucker {
public:
size_t dp = 0;
size_t ip = 0;
ostringstream out;
array<int, 100> 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 { class GameEngine {
map<string, int> damage_types{ map<string, int> damage_types{
@ -35,8 +17,11 @@ class GameEngine {
}; };
public: public:
int starting_hp = 0;
int hit_points = 0; int hit_points = 0;
int hits_taken = 0; int hits_taken = 0;
int rounds = 0;
int streak = 0;
GameEngine(int hp); GameEngine(int hp);
@ -51,4 +36,25 @@ class GameEngine {
bool is_dead(); bool is_dead();
void heal(); void heal();
void reset();
};
class Brainfucker {
public:
size_t dp = 0;
size_t ip = 0;
ostringstream out;
array<int, 100> data = {};
string code = {};
Brainfucker();
void run(int count);
void set_code(string &code);
void reset();
void jump_backward();
void jump_forward();
string to_string();
}; };

@ -33,7 +33,7 @@ int GUI::main_loop(GameEngine &game, std::function<bool()> runner) {
auto status = Renderer([&] { auto status = Renderer([&] {
return vbox({ 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(), separator(),
hbox({ hbox({
text("HP "), text("HP "),
@ -73,9 +73,9 @@ int GUI::main_loop(GameEngine &game, std::function<bool()> runner) {
auto component = Renderer(build_log, [&] { auto component = Renderer(build_log, [&] {
return vbox({ return vbox({
game_stuff->Render() | flex, game_stuff->Render() | flex | size(HEIGHT, GREATER_THAN, 20),
separator(), separator(),
build_log->Render() | vscroll_indicator | yframe | flex, build_log->Render() | vscroll_indicator | yframe | yflex_grow,
separator(), separator(),
status->Render() status->Render()
}) | border; }) | border;

@ -61,11 +61,15 @@ TEST_CASE("", "[game_engine]") {
game.end_round(); game.end_round();
REQUIRE(game.hit_points < 100); REQUIRE(game.hit_points < 100);
REQUIRE(game.rounds == 1);
REQUIRE(game.streak == 0);
REQUIRE(game.is_dead() == false); REQUIRE(game.is_dead() == false);
game.start_round(); game.start_round();
game.end_round(); game.end_round();
REQUIRE(game.hit_points == 100); REQUIRE(game.hit_points == 100);
REQUIRE(game.rounds == 2);
REQUIRE(game.streak == 1);
REQUIRE(game.is_dead() == false); REQUIRE(game.is_dead() == false);
} }

Loading…
Cancel
Save