You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include "BattleInterface.hpp"
  2. const int BattleInterface::maxTurn = 100;
  3. BattleInterface::BattleInterface()
  4. {
  5. this->turn = -1;
  6. this->player1 = nullptr;
  7. this->player2 = nullptr;
  8. this->winner = nullptr;
  9. this->loser = nullptr;
  10. }
  11. BattleInterface::BattleInterface(Character *p1, Character *p2)
  12. {
  13. this->turn = 1;
  14. this->player1 = p1;
  15. this->player2 = p2;
  16. this->winner = nullptr;
  17. this->loser = nullptr;
  18. }
  19. void BattleInterface::battleStartAnnounce()
  20. {
  21. std::cout << " === BATTLE START === \n"
  22. << " " << player1->getName() << "\n"
  23. << " vs \n"
  24. << " " << player2->getName() << "\n"
  25. << " === ============ === \n"
  26. << std::flush;
  27. }
  28. void BattleInterface::battleEndAnnouce()
  29. {
  30. std::cout << " === BATTLE END === \n"
  31. << "winner: " << this->winner->getName() << "\n"
  32. << std::flush;
  33. }
  34. Character *BattleInterface::getLoser()
  35. {
  36. return this->loser;
  37. }
  38. Character *BattleInterface::getWinner()
  39. {
  40. return this->winner;
  41. }
  42. void BattleInterface::start()
  43. {
  44. int choiceInt;
  45. char buff[64] = {0};
  46. this->battleStartAnnounce();
  47. while ((player1->isAlive() && player2->isAlive()) && this->turn <= BattleInterface::maxTurn)
  48. {
  49. std::cout << "Turn " << this->turn << " of " << BattleInterface::maxTurn << "\n"
  50. << "Your spell book:\n";
  51. for (size_t i = 0; i < Spell::Book.size() && i < (player1->getLevel()); i++)
  52. {
  53. std::cout << "[" << i << "] " << Spell::Book.at(i).getName() << "\n";
  54. }
  55. std::cout << std::flush;
  56. choiceInt = UserInterface::getNumber(
  57. "What are you gonna cast this turn?\n>", 0,
  58. player1->getLevel() > Spell::Book.size() ? Spell::Book.size() : player1->getLevel());
  59. if (player1->canCastSpell(&Spell::Book.at(choiceInt)))
  60. {
  61. player1->castSpell(&Spell::Book.at(choiceInt), player2);
  62. snprintf(buff, 63, "%s cast %s...\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
  63. UserInterface::print(buff);
  64. memset(buff, 0, 64);
  65. snprintf(buff, 63, "%s took %d damage...\n", player2->getName(), Spell::Book.at(choiceInt).getAmount());
  66. UserInterface::print(buff);
  67. memset(buff, 0, 64);
  68. }
  69. else
  70. {
  71. snprintf(buff, 63, "%s failed to cast %s!\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
  72. UserInterface::print(buff);
  73. }
  74. choiceInt = (random() % (player2->getLevel() < Spell::Book.size() ? player2->getLevel() : Spell::Book.size()));
  75. if (player2->canCastSpell(&Spell::Book.at(choiceInt)))
  76. {
  77. player2->castSpell(&Spell::Book.at(choiceInt), player1);
  78. snprintf(buff, 63, "%s cast %s...\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
  79. UserInterface::print(buff);
  80. memset(buff, 0, 64);
  81. snprintf(buff, 63, "%s took %d damage...\n", player1->getName(), Spell::Book.at(choiceInt).getAmount());
  82. UserInterface::print(buff);
  83. memset(buff, 0, 64);
  84. }
  85. else
  86. {
  87. snprintf(buff, 63, "%s failed to cast %s!\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
  88. UserInterface::print(buff);
  89. }
  90. this->turn++;
  91. }
  92. winner = (player1->isAlive() && (player1->getCurHP() >= player2->getCurHP())) ? player1 : player2;
  93. loser = (winner == player1) ? player2 : player1;
  94. this->battleEndAnnouce();
  95. }