#include "PersonT.h" #include #include using namespace std; PersonT::PersonT(string n): name{n}, currentAttribute(AttributeT_COUNT,0), maxAttribute(AttributeT_COUNT,0) {} PersonT::~PersonT() { for (auto x: spells) { delete x; } } string PersonT::GetName(void) const{ return name; } void PersonT::ChangeAttribute(AttributeT attrib, int amt){ currentAttribute[static_cast(attrib)] += amt ; } int PersonT::GetAttribute(AttributeT attrib) const{ return currentAttribute[static_cast(attrib)]; } void PersonT::ChangeMaxAttribute(AttributeT attrib, int amt){ maxAttribute[static_cast(attrib)] += amt; } int PersonT::GetMaxAttribute(AttributeT attrib) const{ return maxAttribute[static_cast(attrib)]; } void PersonT::Tick(){ vector::iterator pos; SpellT * spell; AttributeT a; int amt; for (pos = begin(spells); pos != end(spells); ) { spell = *pos; for(a = AttributeT::HEALTH; a < AttributeT::NONE; a = NextAttribute(a)){ amt = spell->GetEffect(a); if (amt != 0) { cout << spell->GetResult() << endl; cout << "Your " << a << " changes by " << amt << "." << endl; } ChangeAttribute(a,amt); } spell->Tick(); if (not spell->Active()) { cout << "The spell " << spell ->GetSpellName() << " expires." << endl; delete spell; pos = spells.erase(pos); } else { pos++; } } } void PersonT::AddSpell(SpellT * spell){ spells.push_back(spell); } void PersonT::Attack(PersonT * opponent){ int attack{0}; int defend{0}; int delta{0}; attack = GetAttribute(AttributeT::ATTACK); defend = opponent->GetAttribute(AttributeT::DEFEND); delta = abs(attack-defend); if (attack == defend) { delta = max(1, defend/2) ; cout << "A tie, both lose " << delta << " health." << endl; ChangeAttribute(AttributeT::HEALTH, delta); opponent->ChangeAttribute(AttributeT::HEALTH, delta); } else if (attack > defend) { cout << "The attacker wins" << endl; cout << "The defender loses " << delta << " health." << endl; opponent->ChangeAttribute(AttributeT::HEALTH, delta); } else { cout << "The defender wins" << endl; cout << "The attacker loses " << delta << " health." << endl; ChangeAttribute(AttributeT::HEALTH, delta); } ChangeAttribute(AttributeT::ATTACK, attack); opponent->ChangeAttribute(AttributeT::DEFEND, defend); }