|
|
- #include "BattleInterface.hpp"
-
- const int BattleInterface::maxTurn = 100;
-
- BattleInterface::BattleInterface()
- {
- this->turn = -1;
- this->player1 = nullptr;
- this->player2 = nullptr;
- this->winner = nullptr;
- this->loser = nullptr;
- }
-
- BattleInterface::BattleInterface(Character *p1, Character *p2)
- {
- this->turn = 1;
- this->player1 = p1;
- this->player2 = p2;
- this->winner = nullptr;
- this->loser = nullptr;
- }
-
- void BattleInterface::battleStartAnnounce()
- {
- std::cout << " === BATTLE START === \n"
- << " " << player1->getName() << "\n"
- << " vs \n"
- << " " << player2->getName() << "\n"
- << " === ============ === \n"
- << std::flush;
- }
-
- void BattleInterface::battleEndAnnouce()
- {
- std::cout << " === BATTLE END === \n"
- << "winner: " << this->winner->getName() << "\n"
- << std::flush;
- }
-
- Character *BattleInterface::getLoser()
- {
- return this->loser;
- }
-
- Character *BattleInterface::getWinner()
- {
- return this->winner;
- }
-
- void BattleInterface::start()
- {
- int choiceInt;
- char buff[64] = {0};
-
- this->battleStartAnnounce();
- while ((player1->isAlive() && player2->isAlive()) && this->turn <= BattleInterface::maxTurn)
- {
- std::cout << "Turn " << this->turn << " of " << BattleInterface::maxTurn << "\n"
- << "Your spell book:\n";
- for (size_t i = 0; i < Spell::Book.size() && i < (player1->getLevel()); i++)
- {
- std::cout << "[" << i << "] " << Spell::Book.at(i).getName() << "\n";
- }
-
- choiceInt = UserInterface::getNumber(
- "What are you gonna cast this turn?\n>", 0,
- player1->getLevel() > Spell::Book.size() ? Spell::Book.size() : player1->getLevel());
-
- if (player1->canCastSpell(&Spell::Book.at(choiceInt)))
- {
- player1->castSpell(&Spell::Book.at(choiceInt), player2);
- snprintf(buff, 63, "%s cast %s...\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
- UserInterface::print(buff);
- memset(buff, 0, 64);
- snprintf(buff, 63, "%s took %d damage...\n", player2->getName(), Spell::Book.at(choiceInt).getAmount());
- UserInterface::print(buff);
- memset(buff, 0, 64);
- }
- else
- {
- snprintf(buff, 63, "%s failed to cast %s!\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
- UserInterface::print(buff);
- }
-
- choiceInt = (random() % player2->getLevel());
- if (player2->canCastSpell(&Spell::Book.at(choiceInt)))
- {
- player2->castSpell(&Spell::Book.at(choiceInt), player1);
- snprintf(buff, 63, "%s cast %s...\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
- UserInterface::print(buff);
- memset(buff, 0, 64);
- snprintf(buff, 63, "%s took %d damage...\n", player1->getName(), Spell::Book.at(choiceInt).getAmount());
- UserInterface::print(buff);
- memset(buff, 0, 64);
- }
- else
- {
- snprintf(buff, 63, "%s failed to cast %s!\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
- UserInterface::print(buff);
- }
- this->turn++;
- }
-
- winner = (player1->isAlive() && (player1->getCurHP() >= player2->getCurHP())) ? player1 : player2;
- loser = (winner == player1) ? player2 : player1;
- this->battleEndAnnouce();
- }
|