Your question is Refactor and Explain Code. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Rockstar Games keeps a gameplay-side damage system that every weapon and enemy type routes through. The Entity class below is the version currently checked in, and it has picked up rough edges as more weapon types were bolted on over time.
class Entity {
public:
Entity(int hp) { health = hp; }
void ApplyDamage(int amount, int type) {
if (type == 0) {
health = health - amount;
}
if (type == 1) {
health = health - (amount * 2);
}
if (type == 2) {
health = health - amount + armor;
}
lastDamage = amount;
}
int GetHealth() { return health; }
int health;
int armor = 10;
int lastDamage;
};
Refactor this class so the damage logic is clear and correct, and explain in your own words what each existing branch of ApplyDamage was actually doing before your changes, including anything that looks like a bug.