#include "mobile_suit.h" mobile_suit::mobile_suit() : model{"RX-78-2"}, pilot{"Unknown"}, x{0}, y{0}, hp{120}, maxhp{120}, energy{100}, maxenergy{100}, armor{6}, beamDamage{28}, meleeDamage{18} { } mobile_suit::mobile_suit(const string& modelName, const string& pilotName, int startx, int starty) : model{modelName}, pilot{pilotName}, x{startx}, y{starty}, hp{120}, maxhp{120}, energy{100}, maxenergy{100}, armor{6}, beamDamage{28}, meleeDamage{18} { } bool mobile_suit::isOperational() const { return hp > 0; } void mobile_suit::move(int dx, int dy) { int cost{2}; if (energy < cost) { return; } energy -= cost; x += dx; y += dy; } void mobile_suit::recharge(int amount) { if (amount < 0) { amount = 0; } energy += amount; if (energy > maxenergy) { energy = maxenergy; } } void mobile_suit::takeDamage(int rawDamage) { if (rawDamage < 0) { rawDamage = 0; } int reduced{rawDamage - armor}; if (reduced < 1) { reduced = 1; } hp -= reduced; if (hp < 0) { hp = 0; } } void mobile_suit::repair(int amount) { if (amount < 0) { amount = 0; } hp += amount; if (hp > maxhp) { hp = maxhp; } } void mobile_suit::beamShot(mobile_suit& target) { int cost{15}; if (!isOperational()) { return; } if (energy < cost) { return; } energy -= cost; target.takeDamage(beamDamage); } void mobile_suit::saberStrike(mobile_suit& target) { if (!isOperational()) { return; } target.takeDamage(meleeDamage); } // operator- replaces distance (use abs) int operator-(const mobile_suit& a, const mobile_suit& b) { int dx{abs(a.x - b.x)}; int dy{abs(a.y - b.y)}; return dx + dy; } void duel(mobile_suit& a, mobile_suit& b) { int round{1}; while (a.isOperational() && b.isOperational() && round <= 12) { if ((a - b) <= 2) { a.saberStrike(b); } else { a.beamShot(b); } if (!b.isOperational()) { break; } if ((b - a) <= 2) { b.saberStrike(a); } else { b.beamShot(a); } a.recharge(5); b.recharge(5); ++round; } } // operator<< replaces print() ostream& operator<<(ostream& os, const mobile_suit& ms) { os << ms.pilot << " | " << ms.model << " | Pos(" << ms.x << "," << ms.y << ")" << " | HP " << ms.hp << "/" << ms.maxhp << " | EN " << ms.energy << "/" << ms.maxenergy; return os; }