Moved the game engine into its own unit and then made a failing test for it.
This commit is contained in:
parent
fb5bf9d733
commit
440be444ea
7 changed files with 82 additions and 49 deletions
39
game_engine.cpp
Normal file
39
game_engine.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <map>
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/color.h>
|
||||
#include "game_engine.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
using namespace std;
|
||||
|
||||
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool GameEngine::hit(string &type) {
|
||||
int damage = determine_damage(type);
|
||||
hit_points -= damage;
|
||||
|
||||
if(is_dead()) {
|
||||
print(ERROR, "YOU DIED!\n");
|
||||
} else {
|
||||
println("DAMAGE {}, HP: {}", damage, hit_points);
|
||||
}
|
||||
|
||||
// super dumb but I'll clean it up later
|
||||
return is_dead();
|
||||
}
|
||||
|
||||
bool GameEngine::is_dead() {
|
||||
return hit_points <= 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue