gamemanager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Someone tells, that Qt can to build this
  3. */
  4. #include "include/hotseatgame/gamemanager.h"
  5. #include "cell.h"
  6. #include "hotseatgame/gameproperties.h"
  7. #include <iostream>
  8. #include <cmath>
  9. GameManager::GameManager(QObject *parent) : QObject(parent) {
  10. }
  11. void GameManager::buildGameTable(double start_x_coordinate, double start_y_coordinate) {
  12. start_x_coordinate_ = start_x_coordinate;
  13. start_y_coordinate_ = start_y_coordinate;
  14. generateTable();
  15. }
  16. void GameManager::generateTable() {
  17. game_table_.assign(GameProperties::FIELD_COLUMNS_NUMBER, std::vector< Cell* >(GameProperties::FIELD_ROWS_NUMBER, nullptr));
  18. for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
  19. for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
  20. game_table_[col][row] = new Cell(nullptr);
  21. }
  22. }
  23. unsigned isEven = 1;
  24. for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
  25. isEven ^= 1;
  26. for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
  27. if (row != 0 && col != 0 - isEven) {
  28. game_table_[col][row]->setleftUp(game_table_[col - 1 +isEven][row - 1]);
  29. }
  30. if (col != 0) {
  31. game_table_[col][row]->setleft(game_table_[col - 1][row]);
  32. }
  33. if (row != GameProperties::FIELD_ROWS_NUMBER - 1 && col != 0 -isEven) {
  34. game_table_[col][row]->setleftDown(game_table_[col - 1 +isEven][row + 1]);
  35. }
  36. if (row != 0 && col != GameProperties::FIELD_COLUMNS_NUMBER -isEven) {
  37. game_table_[col][row]->setrightUp(game_table_[col +isEven][row - 1]);
  38. }
  39. if (col != GameProperties::FIELD_COLUMNS_NUMBER - 1) {
  40. game_table_[col][row]->setright(game_table_[col + 1][row]);
  41. }
  42. if (row != GameProperties::FIELD_ROWS_NUMBER - 1 && col != GameProperties::FIELD_COLUMNS_NUMBER -isEven) {
  43. game_table_[col][row]->setrightDown(game_table_[col +isEven][row + 1]);
  44. }
  45. }
  46. }
  47. game_table_[0][0]->setXCoordinate(start_x_coordinate_);
  48. game_table_[0][0]->setYCoordinate(start_y_coordinate_);
  49. for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
  50. for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
  51. auto cell = game_table_[col][row];
  52. auto verticles = cell->getPoints(GameProperties::CELL_MIN_RADIUS * 2, 30.0);
  53. if (cell->getrightUp()) {
  54. cell->getrightUp()->setXCoordinate(cell->getXCoordinate() + verticles[0].first );
  55. cell->getrightUp()->setYCoordinate(cell->getYCoordinate() + verticles[0].second);
  56. }
  57. if (cell->getright()) {
  58. cell->getright()->setXCoordinate(cell->getXCoordinate() + verticles[1].first );
  59. cell->getright()->setYCoordinate(cell->getYCoordinate() + verticles[1].second);
  60. }
  61. if (cell->getrightDown()) {
  62. cell->getrightDown()->setXCoordinate(cell->getXCoordinate() + verticles[2].first );
  63. cell->getrightDown()->setYCoordinate(cell->getYCoordinate() + verticles[2].second);
  64. }
  65. if (cell->getleftDown()) {
  66. cell->getleftDown()->setXCoordinate(cell->getXCoordinate() + verticles[3].first );
  67. cell->getleftDown()->setYCoordinate(cell->getYCoordinate() + verticles[3].second);
  68. }
  69. if (cell->getleft()) {
  70. cell->getleft()->setXCoordinate(cell->getXCoordinate() + verticles[4].first );
  71. cell->getleft()->setYCoordinate(cell->getYCoordinate() + verticles[4].second);
  72. }
  73. if (cell->getleftUp()) {
  74. cell->getleftUp()->setXCoordinate(cell->getXCoordinate() + verticles[5].first );
  75. cell->getleftUp()->setYCoordinate(cell->getYCoordinate() + verticles[5].second);
  76. }
  77. }
  78. }
  79. for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
  80. for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
  81. auto cell = game_table_[col][row];
  82. std::cout << "(" << cell->getXCoordinate() << "," << cell->getYCoordinate() << ") ";
  83. }
  84. std::cout << std::endl;
  85. }
  86. }
  87. UnitsQueue* GameManager::getTurnQueue() {
  88. return &turn_queue_;
  89. }
  90. void GameManager::AddToUnitQueue(Unit* unit) {
  91. turn_queue_.add(unit);
  92. }
  93. void GameManager::RmFromUnitQueue(Unit* unit) {
  94. turn_queue_.remove(unit);
  95. }
  96. int GameManager::getCurPlayerId() {
  97. return cur_player_id_;
  98. }
  99. std::vector<std::vector<Cell *> > GameManager::getGameField()
  100. {
  101. return game_table_;
  102. }
  103. Player* GameManager::getCurrentPlayer() {
  104. return player_manager_->getPlayer(cur_player_id_);
  105. }