hotseatgame.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "ui/hotseat_game/hotseatgame.h"
  2. #include "ui_hotseatgame.h"
  3. #include "gui/guiscenemanager.h"
  4. #include "cell.h"
  5. #include "hotseatgame/gamemanager.h"
  6. #include <cmath>
  7. #include <QDebug>
  8. #include <QGraphicsScene>
  9. #include <QEvent>
  10. #include <QResizeEvent>
  11. HotSeatGame::HotSeatGame(QWidget *parent) :
  12. Scene(parent),
  13. ui(new Ui::HotSeatGame)
  14. {
  15. qDebug() << "Initialising game";
  16. ui->setupUi(this);
  17. game_scene_ = new QGraphicsScene(ui->game_content);
  18. ui->game_content->setScene(game_scene_);
  19. setMouseTracking(true);
  20. ui->game_content->setMouseTracking(true);
  21. game_scene_watcher_ = new DrawSceneEventListener(this);
  22. ui->game_content->installEventFilter(game_scene_watcher_);
  23. }
  24. void HotSeatGame::init() {
  25. GameProperties::FIELD_ROWS_NUMBER = 6;
  26. GameProperties::FIELD_COLUMNS_NUMBER = 14;
  27. int left_right_margin = 0;
  28. int up_down_margin = 0;
  29. int widget_width = ui->game_content->width();
  30. int widget_height = ui->game_content->height();
  31. qDebug() << widget_width << " " << widget_height;
  32. int predictable_min_radius = (widget_width - left_right_margin * 2) / ((GameProperties::FIELD_COLUMNS_NUMBER + 1) * 2);
  33. int predictable_max_radius = (widget_height - up_down_margin * 2) / ((GameProperties::FIELD_ROWS_NUMBER + 1) * 2);
  34. qDebug() << predictable_min_radius << " " << predictable_max_radius;
  35. if (abs(predictable_max_radius * sin(60.0 * M_PI / 180.0)) > abs(predictable_min_radius)) {
  36. predictable_max_radius = abs(predictable_min_radius / sin(60.0 * M_PI / 180.0));
  37. } else {
  38. predictable_min_radius = abs(predictable_max_radius * sin(60.0 * M_PI / 180.0));
  39. }
  40. qDebug() << predictable_min_radius << " " << predictable_max_radius;
  41. GameProperties::CELL_MIN_RADIUS = predictable_min_radius;
  42. GameProperties::CELL_MAX_RADIUS = predictable_max_radius;
  43. left_right_margin = (widget_width - predictable_min_radius * GameProperties::FIELD_COLUMNS_NUMBER * 2) / 2;
  44. up_down_margin = (widget_height - predictable_max_radius * GameProperties::FIELD_ROWS_NUMBER * 2) / 2;
  45. qDebug() << left_right_margin << " " << up_down_margin;
  46. qDebug() << predictable_min_radius * GameProperties::FIELD_COLUMNS_NUMBER * 2 << " " << predictable_max_radius * GameProperties::FIELD_ROWS_NUMBER * 2;
  47. GameManager::getInstance().buildGameTable(left_right_margin + GameProperties::CELL_MIN_RADIUS, -up_down_margin - GameProperties::CELL_MAX_RADIUS);
  48. drawField();
  49. }
  50. void HotSeatGame::deinit()
  51. {
  52. auto field = GameManager::getInstance().getGameField();
  53. for (int i = 0; i < field.size(); i++) {
  54. for (int j = 0; j < field[i].size(); j++){
  55. field[i][j]->setVisible(false);
  56. game_scene_->removeItem(field[i][j]);
  57. }
  58. }
  59. }
  60. HotSeatGame::~HotSeatGame()
  61. {
  62. delete ui;
  63. }
  64. void HotSeatGame::on_surrender_button__clicked()
  65. {
  66. //GuiSceneManager::getInstance().showMainWindow();
  67. //GuiSceneManager::getInstance().hideDetachedScene("hotseatgame");
  68. //GuiSceneManager::getInstance().changeScene("hotseatgame_results");
  69. GuiSceneManager::getInstance().changeScene("main_menu");
  70. }
  71. void HotSeatGame::drawField()
  72. {
  73. auto field = GameManager::getInstance().getGameField();
  74. game_scene_->addRect(0, 0, 0.1, 0.1);
  75. for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
  76. for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
  77. auto cell = field[col][row];
  78. double center_x = cell->getXCoordinate();
  79. double center_y = cell->getYCoordinate();
  80. qDebug() << center_x << " " << center_y;
  81. game_scene_->addItem(cell);
  82. cell->setPos(cell->getXCoordinate(), cell->getYCoordinate());
  83. cell->setVisible(true);
  84. }
  85. }
  86. }
  87. DrawSceneEventListener::DrawSceneEventListener(HotSeatGame *scene, QObject *parent) : QObject(parent) {
  88. scene_ = scene;
  89. }
  90. bool DrawSceneEventListener::eventFilter(QObject *watched, QEvent *event)
  91. {
  92. if (event->type() == QEvent::Resize) {
  93. scene_->deinit();
  94. scene_->init();
  95. }
  96. if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonPress ) {
  97. qDebug() << "ha-ha!!";
  98. QMouseEvent* evt = dynamic_cast<QMouseEvent*>(event);
  99. auto cells = GameManager::getInstance().getGameField();
  100. qDebug() << evt->globalPos();
  101. for (int i = 0; i < cells.size(); i++) {
  102. for (int j = 0; j < cells[i].size(); j++) {
  103. if (cells[i][j]->contains(evt->localPos())) {
  104. qDebug() << "He-he!!!";
  105. cells[i][j]->setHovered(true);
  106. } else {
  107. cells[i][j]->setHovered(false);
  108. }
  109. cells[i][j]->show();
  110. }
  111. }
  112. }
  113. return false;
  114. }