gamemap.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "gamemap.h"
  2. GameMap::GameMap(QObject* parent)
  3. : QObject(parent)
  4. {
  5. m_step = 48;
  6. m_cols = 20;
  7. m_rows = 20;
  8. m_hero = nullptr;
  9. m_isStop = true;
  10. m_scence = nullptr;
  11. m_exitDoor = nullptr;
  12. }
  13. int GameMap::cols() const
  14. {
  15. return m_cols;
  16. }
  17. void GameMap::setCols(int cols)
  18. {
  19. m_cols = cols;
  20. }
  21. int GameMap::rows() const
  22. {
  23. return m_rows;
  24. }
  25. void GameMap::setRows(int rows)
  26. {
  27. m_rows = rows;
  28. }
  29. int GameMap::step() const
  30. {
  31. return m_step;
  32. }
  33. void GameMap::setStep(int step)
  34. {
  35. m_step = step;
  36. }
  37. QImage GameMap::drawScence()
  38. {
  39. return QImage();
  40. }
  41. bool GameMap::play()
  42. {
  43. return true;
  44. }
  45. void GameMap::addGameObject(GameObject* obj)
  46. {
  47. gameObjects << obj;
  48. obj->setMap(this);
  49. }
  50. bool GameMap::hasObjectInPostion(GameObject* selObj, int x, int y, int n)
  51. {
  52. bool ret = false;
  53. for (int i = 0; i < gameObjects.count(); i++) {
  54. GameObject* obj = gameObjects.at(i);
  55. if (obj == selObj) {
  56. continue;
  57. }
  58. if (selObj == hero()) {
  59. if (obj->gameObjectType() != "墙壁") {
  60. continue;
  61. }
  62. } else {
  63. if ((obj->gameObjectType() != "墙壁") && (obj->gameObjectType() != "门")) {
  64. continue;
  65. }
  66. }
  67. QRect rc(x, y, 48, 48);
  68. if (isOverLap(obj->rect(), rc, n)) {
  69. qDebug() << obj->rect();
  70. qDebug() << rc;
  71. qDebug() << obj->gameObjectType();
  72. qDebug() << "----";
  73. ret = true;
  74. return ret;
  75. }
  76. }
  77. return ret;
  78. }
  79. bool GameMap::isOverLap(const QRect& rc1, const QRect& rc2, int n)
  80. {
  81. QList<int> lst1;
  82. lst1 << rc1.left();
  83. lst1 << rc1.right();
  84. lst1 << rc2.left();
  85. lst1 << rc2.right();
  86. qSort(lst1);
  87. QList<int> lst2;
  88. lst2 << rc1.top();
  89. lst2 << rc1.bottom();
  90. lst2 << rc2.top();
  91. lst2 << rc2.bottom();
  92. qSort(lst2);
  93. int w = lst1.at(3) - lst1.at(0) + 1 + n;
  94. int h = lst2.at(3) - lst2.at(0) + 1 + n;
  95. if ((w < (rc1.width() + rc2.width())) && (h < (rc1.height() + rc2.height()))) {
  96. qDebug() << w;
  97. qDebug() << h;
  98. qDebug() << rc1.width() + rc2.width();
  99. qDebug() << rc1.height() + rc2.height();
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }
  105. GameObject* GameMap::hero() const
  106. {
  107. return m_hero;
  108. }
  109. void GameMap::setHero(GameObject* hero)
  110. {
  111. m_hero = hero;
  112. }
  113. bool GameMap::isStop() const
  114. {
  115. return m_isStop;
  116. }
  117. void GameMap::setIsStop(bool isStop)
  118. {
  119. m_isStop = isStop;
  120. }
  121. GameScence* GameMap::scence() const
  122. {
  123. return m_scence;
  124. }
  125. void GameMap::setScence(GameScence* scence)
  126. {
  127. m_scence = scence;
  128. }
  129. GameObject* GameMap::exitDoor() const
  130. {
  131. return m_exitDoor;
  132. }
  133. void GameMap::setExitDoor(GameObject* exitDoor)
  134. {
  135. m_exitDoor = exitDoor;
  136. }
  137. void GameMap::initGame()
  138. {
  139. qDeleteAll(gameObjects);
  140. gameObjects.clear();
  141. m_isStop = false;
  142. }
  143. void GameMap::startGame()
  144. {
  145. setIsStop(false);
  146. scence()->startTimer();
  147. }