recruitmentscene.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include <ui/hotseat_recruitment/recruitmentscene.h>
  2. #include <hotseatgame/uniticon.h>
  3. #include <hotseatgame/raceicon.h>
  4. #include <gui/guiscenemanager.h>
  5. #include <gui/scene.h>
  6. #include <playermanager.h>
  7. #include <player.h>
  8. #include <racemanager.h>
  9. #include <race.h>
  10. #include <ui_recruitmentscene.h>
  11. #include <QHBoxLayout>
  12. #include <QString>
  13. #include <QStringList>
  14. #include <cassert>
  15. #include <vector>
  16. RecruitmentScene::RecruitmentScene(QWidget *parent)
  17. : Scene(parent)
  18. , ui(new Ui::RecruitmentScene)
  19. {
  20. ui->setupUi(this);
  21. for (int i = 0; i < 5; i++) {
  22. available_units_icons_[i] = new UnitIcon();
  23. ui->available_units_layout_->addWidget(available_units_icons_[i], 0, i);
  24. connect(available_units_icons_[i], SIGNAL(clicked(UnitIcon*)), this, SLOT(onUnitIconClicked(UnitIcon*)));
  25. connect(available_units_icons_[i], SIGNAL(hovered(UnitIcon*)), this, SLOT(onUnitIconHovered(UnitIcon*)));
  26. connect(available_units_icons_[i], SIGNAL(unhovered(UnitIcon*)), this, SLOT(onUnitIconUnHovered(UnitIcon*)));
  27. connect(available_units_icons_[i], SIGNAL(doubleclicked(UnitIcon*)), this, SLOT(onUnitIconDoubleClicked(UnitIcon*)));
  28. }
  29. for (int i = 0; i < 3; i++) {
  30. available_races_icons_[i] = new RaceIcon();
  31. ui->availableRacesLayout_->addWidget(available_races_icons_[i], i);
  32. connect(available_races_icons_[i], SIGNAL(clicked(RaceIcon*)), this, SLOT(onRaceIconClicked(RaceIcon*)));
  33. connect(available_races_icons_[i], SIGNAL(hovered(RaceIcon*)), this, SLOT(onRaceIconHovered(RaceIcon*)));
  34. connect(available_races_icons_[i], SIGNAL(unhovered(RaceIcon*)), this, SLOT(onRaceIconUnHovered(RaceIcon*)));
  35. connect(available_races_icons_[i], SIGNAL(doubleclicked(RaceIcon*)), this, SLOT(onRaceIconDoubleClicked(RaceIcon*)));
  36. }
  37. for (int i = 0; i < 10; i++) {
  38. chosen_units_icons_[i] = new UnitIcon();
  39. ui->chosen_units_layout_->addWidget(chosen_units_icons_[i], i / 5, i % 5);
  40. connect(chosen_units_icons_[i], SIGNAL(clicked(UnitIcon*)), this, SLOT(onChosenUnitIconClicked(UnitIcon*)));
  41. connect(chosen_units_icons_[i], SIGNAL(hovered(UnitIcon*)), this, SLOT(onChosenUnitIconHovered(UnitIcon*)));
  42. connect(chosen_units_icons_[i], SIGNAL(unhovered(UnitIcon*)), this, SLOT(onChosenUnitIconUnHovered(UnitIcon*)));
  43. connect(chosen_units_icons_[i], SIGNAL(doubleclicked(UnitIcon*)), this, SLOT(onChosenUnitIconDoubleClicked(UnitIcon*)));
  44. }
  45. for (int i = 0; i < 4; i++) {
  46. prev_grade_units_icons[i] = new UnitIcon();
  47. ui->prev_gen_units_layout->addWidget(prev_grade_units_icons[i]);
  48. connect(prev_grade_units_icons[i], SIGNAL(clicked(UnitIcon*)), this, SLOT(onGradeUnitIconClicked(UnitIcon*)));
  49. connect(prev_grade_units_icons[i], SIGNAL(hovered(UnitIcon*)), this, SLOT(onGradeUnitIconHovered(UnitIcon*)));
  50. connect(prev_grade_units_icons[i], SIGNAL(unhovered(UnitIcon*)), this, SLOT(onGradeUnitIconUnHovered(UnitIcon*)));
  51. connect(prev_grade_units_icons[i], SIGNAL(doubleclicked(UnitIcon*)), this, SLOT(onGradeUnitIconDoubleClicked(UnitIcon*)));
  52. next_grade_units_icons[i] = new UnitIcon();
  53. ui->next_gen_units_layout_->addWidget(next_grade_units_icons[i]);
  54. connect(next_grade_units_icons[i], SIGNAL(clicked(UnitIcon*)), this, SLOT(onGradeUnitIconClicked(UnitIcon*)));
  55. connect(next_grade_units_icons[i], SIGNAL(hovered(UnitIcon*)), this, SLOT(onGradeUnitIconHovered(UnitIcon*)));
  56. connect(next_grade_units_icons[i], SIGNAL(unhovered(UnitIcon*)), this, SLOT(onGradeUnitIconUnHovered(UnitIcon*)));
  57. connect(next_grade_units_icons[i], SIGNAL(doubleclicked(UnitIcon*)), this, SLOT(onGradeUnitIconDoubleClicked(UnitIcon*)));
  58. }
  59. ui->description_icon_->resize(45, 45);
  60. ui->description_text_->setWordWrap(true);
  61. ui->current_spec_unit_->resize(45, 45);
  62. ui->current_spec_unit_->setUnitIcon("warcraft", "chaosorc");
  63. }
  64. RecruitmentScene::~RecruitmentScene()
  65. {
  66. }
  67. void RecruitmentScene::parseArgs(QString args) {
  68. QStringList list = args.split("|");
  69. assert(list.size() >= 2);
  70. current_player_id_ = list[0].toInt();
  71. current_player_ = PlayerManager::getInstance().getPlayer(current_player_id_);
  72. available_money_ = list[1].toInt();
  73. init();
  74. }
  75. void RecruitmentScene::init() {
  76. current_money_ = available_money_;
  77. initAvailableRaces();
  78. initAvailableUnits();
  79. showChosenUnits();
  80. ui->title_->setText(
  81. "<html><head/><body><p align=\"center\"><span style=\"font-size:24pt; color:#b12d2d;\">Набор юнитов - "
  82. + current_player_->getPlayerName()
  83. + "</span></p></body></html>"
  84. );
  85. setDescriptionIcon(current_player_->getRace()->getRaceIcon());
  86. setDescriptionText(current_player_->getRace()->getRaceDescr());
  87. setDescriptionTitle(current_player_->getRace()->getRaceName());
  88. ui->chosen_race_image_->resize(100, 100);
  89. ui->chosen_race_image_->setRaceIcon(current_player_->getRace()->getRaceIcon());
  90. ui->chosen_race_image_->setState(5);
  91. ui->unit_spec_widget_->hide();
  92. setMoney(current_money_);
  93. }
  94. void RecruitmentScene::initAvailableRaces() {
  95. auto races = RaceManager::getInstance().getAvailableRacesList();
  96. int race_id = 0;
  97. for (QString race_name : races) {
  98. assert(race_id < 3);
  99. auto race = RaceManager::getInstance().getRace(race_name);
  100. available_races_icons_[race_id]->setRaceIcon(race->getRaceIcon());
  101. available_races_icons_[race_id]->setRace(race);
  102. race_id++;
  103. }
  104. }
  105. void RecruitmentScene::initAvailableUnits() {
  106. int unit_id = 0;
  107. for (int i = 0; i < 5; i++) {
  108. available_units_icons_[i]->setUnitIcon("norace", "nounit");
  109. available_units_icons_[i]->setState(5);
  110. }
  111. for (Unit* unit : current_player_->getRace()->getAllUnitsList()) {
  112. available_units_icons_[unit_id]->setState(0);
  113. available_units_icons_[unit_id]->setUnitIcon(unit->getUnitIcon());
  114. available_units_icons_[unit_id]->setUnit(unit);
  115. unit_id++;
  116. }
  117. }
  118. void RecruitmentScene::showChosenUnits() {
  119. for (int i = 0; i < 10; i++) {
  120. chosen_units_icons_[i]->unsetUnitIcon();
  121. chosen_units_icons_[i]->setUnit(nullptr);
  122. }
  123. int counter = 0;
  124. for (auto unit : current_player_->getUnits()) {
  125. chosen_units_icons_[counter]->setUnitIcon(unit->getUnitIcon());
  126. chosen_units_icons_[counter]->setUnit(unit);
  127. counter++;
  128. }
  129. }
  130. void RecruitmentScene::updateSpecsWidget(Unit *unit) {
  131. auto next_specs = unit->getUpgradeSpecs();
  132. auto prev_specs = unit->getParentSpecs();
  133. ui->prev_gen_units_list_->resize(45 * prev_specs.size(), 45);
  134. ui->next_gen_units_list_->resize(45 * next_specs.size(), 45);
  135. ui->prev_gen_units_list_->move((180 - 45 * prev_specs.size()) / 2, 40);
  136. ui->next_gen_units_list_->move((180 - 45 * next_specs.size()) / 2, 240);
  137. for (int i = 0; i < 4; i++) {
  138. prev_grade_units_icons[i]->hide();
  139. next_grade_units_icons[i]->hide();
  140. }
  141. int prev_specs_num = 0;
  142. for (unsigned i = 0; i < prev_specs.size(); i++) {
  143. if (prev_specs[i].length() <= 2)
  144. continue;
  145. delete prev_grade_units_icons[i]->getUnit();
  146. prev_grade_units_icons[i]->resize(45, 45);
  147. prev_grade_units_icons[i]->unsetUnitIcon();
  148. prev_grade_units_icons[i]->setUnit(current_player_->getRace()->createUnit(prev_specs[i]));
  149. prev_grade_units_icons[i]->show();
  150. prev_specs_num++;
  151. }
  152. if (prev_specs_num == 0) {
  153. ui->spec_title_->move(0, 110);
  154. ui->prev_gen_units_list_->hide();
  155. ui->arrow_down_1_->hide();
  156. } else {
  157. ui->spec_title_->move(0, 10);
  158. ui->prev_gen_units_list_->show();
  159. ui->arrow_down_1_->show();
  160. }
  161. int next_specs_num = 0;
  162. for (unsigned i = 0; i < next_specs.size(); i++) {
  163. if (next_specs[i].length() <= 2)
  164. continue;
  165. delete next_grade_units_icons[i]->getUnit();
  166. next_grade_units_icons[i]->resize(45, 45);
  167. next_grade_units_icons[i]->unsetUnitIcon();
  168. next_grade_units_icons[i]->setUnit(current_player_->getRace()->createUnit(next_specs[i]));
  169. next_grade_units_icons[i]->show();
  170. next_specs_num++;
  171. }
  172. if (next_specs_num == 0) {
  173. ui->next_gen_units_list_->hide();
  174. ui->arrow_down_2_->hide();
  175. } else {
  176. ui->next_gen_units_list_->show();
  177. ui->arrow_down_2_->show();
  178. }
  179. ui->current_spec_unit_->setUnit(unit);
  180. ui->current_spec_unit_->setState(5);
  181. }
  182. void RecruitmentScene::changeRace(Race *race) {
  183. current_player_->clearUnits();
  184. current_player_->setRace(race);
  185. init();
  186. }
  187. void RecruitmentScene::setMoney(int money_) {
  188. ui->available_money_text_->setText("<html><head/><body><p>"
  189. "<span style=\"font-size:10pt; color:#ff0000;\">Ресурсы: "
  190. + QString::number(money_) +
  191. "</span></p></body></html>");
  192. }
  193. void RecruitmentScene::on_back_button_clicked()
  194. {
  195. GuiSceneManager::getInstance().changeScene("main_menu");
  196. }
  197. void RecruitmentScene::setDescriptionTitle(QString title) {
  198. ui->desctiption_title_->setText("<html><head/><body><p align=\"center\">"
  199. "<span style=\" font-weight:600; font-size:16px; color:#000080;\">"
  200. + title +
  201. "</span></p></body></html>");
  202. }
  203. void RecruitmentScene::setDescriptionText(QString text) {
  204. ui->description_text_->setText("<html><head/><body><p><span style=\"color:#000080;\">"
  205. + text
  206. + "</span></p></body></html>");
  207. }
  208. void RecruitmentScene::setDescriptionIcon(QImage icon) {
  209. ui->description_icon_->setUnitIcon(icon);
  210. ui->description_icon_->setState(0);
  211. }
  212. void RecruitmentScene::onUnitIconHovered(UnitIcon*) {
  213. }
  214. void RecruitmentScene::onUnitIconUnHovered(UnitIcon*) {
  215. }
  216. void RecruitmentScene::onUnitIconClicked(UnitIcon* icon) {
  217. setDescriptionIcon(icon->getUnit()->getUnitIcon());
  218. setDescriptionText(icon->getUnit()->getUnitDescr());
  219. setDescriptionTitle(icon->getUnit()->getUnitName());
  220. for (int i = 0; i < 5; i++) {
  221. if (available_units_icons_[i] != icon)
  222. available_units_icons_[i]->deactivate();
  223. }
  224. for (int i = 0; i < 10; i++) {
  225. chosen_units_icons_[i]->deactivate();
  226. }
  227. for (int i = 0; i < 3; i++) {
  228. available_races_icons_[i]->deactivate();
  229. }
  230. ui->unit_spec_widget_->hide();
  231. }
  232. void RecruitmentScene::onUnitIconDoubleClicked(UnitIcon* icon) {
  233. if (current_player_->getUnits().size() >= 10)
  234. return;
  235. current_player_->addUnit(current_player_->getRace()->createUnit(icon->getUnit()->getUnitId()));
  236. showChosenUnits();
  237. }
  238. void RecruitmentScene::onChosenUnitIconHovered(UnitIcon*) {}
  239. void RecruitmentScene::onChosenUnitIconUnHovered(UnitIcon*) {}
  240. void RecruitmentScene::onChosenUnitIconClicked(UnitIcon* icon) {
  241. setDescriptionIcon(icon->getUnit()->getUnitIcon());
  242. setDescriptionText(icon->getUnit()->getUnitDescr());
  243. setDescriptionTitle(icon->getUnit()->getUnitName());
  244. for (int i = 0; i < 5; i++) {
  245. available_units_icons_[i]->deactivate();
  246. }
  247. for (int i = 0; i < 10; i++) {
  248. if (chosen_units_icons_[i] != icon)
  249. chosen_units_icons_[i]->deactivate();
  250. }
  251. for (int i = 0; i < 3; i++) {
  252. available_races_icons_[i]->deactivate();
  253. }
  254. updateSpecsWidget(icon->getUnit());
  255. ui->unit_spec_widget_->show();
  256. }
  257. void RecruitmentScene::onChosenUnitIconDoubleClicked(UnitIcon* icon) {
  258. current_player_->deleteUnit(icon->getUnit());
  259. showChosenUnits();
  260. ui->unit_spec_widget_->hide();
  261. }
  262. void RecruitmentScene::onRaceIconHovered(RaceIcon*) {
  263. }
  264. void RecruitmentScene::onRaceIconUnHovered(RaceIcon*) {
  265. }
  266. void RecruitmentScene::onRaceIconClicked(RaceIcon* icon) {
  267. setDescriptionIcon(icon->getRace()->getRaceIcon());
  268. setDescriptionText(icon->getRace()->getRaceDescr());
  269. setDescriptionTitle(icon->getRace()->getRaceName());
  270. for (int i = 0; i < 5; i++) {
  271. available_units_icons_[i]->deactivate();
  272. }
  273. for (int i = 0; i < 10; i++) {
  274. chosen_units_icons_[i]->deactivate();
  275. }
  276. for (int i = 0; i < 3; i++) {
  277. if (available_races_icons_[i] != icon)
  278. available_races_icons_[i]->deactivate();
  279. }
  280. ui->unit_spec_widget_->hide();
  281. }
  282. void RecruitmentScene::onRaceIconDoubleClicked(RaceIcon* icon) {
  283. if (icon->getRace() != current_player_->getRace())
  284. changeRace(icon->getRace());
  285. }
  286. void RecruitmentScene::onGradeUnitIconHovered(UnitIcon*) {}
  287. void RecruitmentScene::onGradeUnitIconUnHovered(UnitIcon*) {}
  288. void RecruitmentScene::onGradeUnitIconClicked(UnitIcon* icon) {
  289. setDescriptionIcon(icon->getUnit()->getUnitIcon());
  290. setDescriptionText(icon->getUnit()->getUnitDescr());
  291. setDescriptionTitle(icon->getUnit()->getUnitName());
  292. for (int i = 0; i < 4; i++) {
  293. if (prev_grade_units_icons[i] != icon)
  294. prev_grade_units_icons[i]->deactivate();
  295. if (next_grade_units_icons[i] != icon)
  296. next_grade_units_icons[i]->deactivate();
  297. }
  298. }
  299. void RecruitmentScene::onGradeUnitIconDoubleClicked(UnitIcon* icon) {
  300. current_player_->deleteUnit(ui->current_spec_unit_->getUnit());
  301. auto new_unit = current_player_->getRace()->createUnit(icon->getUnit()->getUnitId());
  302. current_player_->addUnit(new_unit);
  303. updateSpecsWidget(new_unit);
  304. showChosenUnits();
  305. for (int i = 0; i < 10; i++) {
  306. if (chosen_units_icons_[i]->getUnit() == new_unit)
  307. chosen_units_icons_[i]->activate();
  308. }
  309. current_money_ -= 100;
  310. setMoney(current_money_);
  311. }
  312. void RecruitmentScene::on_apply_clicked()
  313. {
  314. qDebug() << "Exiting recruit army scene with player_id =" << current_player_id_;
  315. if (current_player_id_ == 0) {
  316. qDebug() << "Changing to 1";
  317. GuiSceneManager::getInstance().changeScene("recruit_army", "1|" + QString::number(available_money_));
  318. } else {
  319. GuiSceneManager::getInstance().changeScene("prebattle");
  320. }
  321. }