Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

107 строки
3.4 KiB

5 лет назад
  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. choiceInt = UserInterface::getNumber(
  56. "What are you gonna cast this turn?\n>", 0,
  57. player1->getLevel() > Spell::Book.size() ? Spell::Book.size() : player1->getLevel());
  58. if (player1->canCastSpell(&Spell::Book.at(choiceInt)))
  59. {
  60. player1->castSpell(&Spell::Book.at(choiceInt), player2);
  61. snprintf(buff, 63, "%s cast %s...\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
  62. UserInterface::print(buff);
  63. memset(buff, 0, 64);
  64. snprintf(buff, 63, "%s took %d damage...\n", player2->getName(), Spell::Book.at(choiceInt).getAmount());
  65. UserInterface::print(buff);
  66. memset(buff, 0, 64);
  67. }
  68. else
  69. {
  70. snprintf(buff, 63, "%s failed to cast %s!\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
  71. UserInterface::print(buff);
  72. }
  73. choiceInt = (random() % player2->getLevel());
  74. if (player2->canCastSpell(&Spell::Book.at(choiceInt)))
  75. {
  76. player2->castSpell(&Spell::Book.at(choiceInt), player1);
  77. snprintf(buff, 63, "%s cast %s...\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
  78. UserInterface::print(buff);
  79. memset(buff, 0, 64);
  80. snprintf(buff, 63, "%s took %d damage...\n", player1->getName(), Spell::Book.at(choiceInt).getAmount());
  81. UserInterface::print(buff);
  82. memset(buff, 0, 64);
  83. }
  84. else
  85. {
  86. snprintf(buff, 63, "%s failed to cast %s!\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
  87. UserInterface::print(buff);
  88. }
  89. this->turn++;
  90. }
  91. winner = (player1->isAlive() && (player1->getCurHP() >= player2->getCurHP())) ? player1 : player2;
  92. loser = (winner == player1) ? player2 : player1;
  93. this->battleEndAnnouce();
  94. }