gameobject.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "gameobject.h"
  2. GameObject::GameObject(QObject* parent)
  3. : QObject(parent)
  4. {
  5. int n[3] = { -1, 0, 1 };
  6. for (int i = 0; i < 3; i++) {
  7. for (int j = 0; j < 3; j++) {
  8. QPoint pt(n[i], n[j]);
  9. m_directions << pt;
  10. }
  11. }
  12. QPoint pt = m_directions.at(qrand() % m_directions.size());
  13. m_moveX = pt.x();
  14. m_moveY = pt.y();
  15. m_orientation = Ot_Down;
  16. m_pixIndex = 0;
  17. }
  18. QPixmap GameObject::image() const
  19. {
  20. return m_image;
  21. }
  22. void GameObject::setImage(const QPixmap& image)
  23. {
  24. m_image = image;
  25. m_defaultPix = image;
  26. }
  27. QRect GameObject::rect() const
  28. {
  29. return m_rect;
  30. }
  31. void GameObject::setRect(const QRect& rect)
  32. {
  33. m_rect = rect;
  34. }
  35. QString GameObject::gameObjectType() const
  36. {
  37. return m_gameObjectType;
  38. }
  39. void GameObject::setGameObjectType(const QString& gameObjectType)
  40. {
  41. m_gameObjectType = gameObjectType;
  42. }
  43. int GameObject::x() const
  44. {
  45. return m_x;
  46. }
  47. void GameObject::setX(int x)
  48. {
  49. m_x = x;
  50. }
  51. int GameObject::y() const
  52. {
  53. return m_y;
  54. }
  55. void GameObject::setY(int y)
  56. {
  57. m_y = y;
  58. }
  59. void GameObject::radomMove()
  60. {
  61. int x = m_x + m_moveX;
  62. int y = m_y + m_moveY;
  63. bool f = this->m_map->hasObjectInPostion(this, x, y);
  64. if ((f == true) || ((m_moveX == 0) && (m_moveY == 0))) {
  65. randomDirections();
  66. for (int i = 0; i < m_directions.count(); i++) {
  67. QPoint pt = m_directions.at(i);
  68. m_moveX = pt.x();
  69. m_moveY = pt.y();
  70. x = m_x + m_moveX;
  71. y = m_y + m_moveY;
  72. f = this->m_map->hasObjectInPostion(this, x, y);
  73. if (f == false) {
  74. break;
  75. }
  76. }
  77. }
  78. m_x = x;
  79. m_y = y;
  80. resetRect();
  81. }
  82. GameMap* GameObject::map() const
  83. {
  84. return m_map;
  85. }
  86. void GameObject::setMap(GameMap* map)
  87. {
  88. m_map = map;
  89. }
  90. void GameObject::resetRect()
  91. {
  92. m_rect.setRect(m_x, m_y, 48, 48);
  93. }
  94. void GameObject::randomDirections()
  95. {
  96. for (int i = 0; i < m_directions.count(); i++) {
  97. int j = qrand() % m_directions.count();
  98. m_directions.swap(i, j);
  99. }
  100. }
  101. void GameObject::moveRight()
  102. {
  103. if (m_map->isStop()) {
  104. return;
  105. }
  106. int x = m_x + 5;
  107. int y = m_y;
  108. if (m_map->hasObjectInPostion(this, x, y, 5) == true) {
  109. return;
  110. }
  111. if (m_orientation != Ot_Right) {
  112. m_orientation = Ot_Right;
  113. m_pixIndex = 0;
  114. } else {
  115. if ((m_pixIndex + 1) < rightImage.count()) {
  116. m_pixIndex++;
  117. } else {
  118. m_pixIndex = 0;
  119. }
  120. }
  121. m_image = rightImage.at(m_pixIndex);
  122. m_x = x;
  123. m_y = y;
  124. resetRect();
  125. }
  126. void GameObject::moveLeft()
  127. {
  128. if (m_map->isStop()) {
  129. return;
  130. }
  131. int x = m_x - 5;
  132. int y = m_y;
  133. if (m_map->hasObjectInPostion(this, x, y, 5) == true) {
  134. return;
  135. }
  136. if (m_orientation != Ot_Left) {
  137. m_orientation = Ot_Left;
  138. m_pixIndex = 0;
  139. } else {
  140. if ((m_pixIndex + 1) < leftImage.count()) {
  141. m_pixIndex++;
  142. } else {
  143. m_pixIndex = 0;
  144. }
  145. }
  146. m_image = leftImage.at(m_pixIndex);
  147. m_x = x;
  148. m_y = y;
  149. resetRect();
  150. }
  151. void GameObject::moveUp()
  152. {
  153. if (m_map->isStop()) {
  154. return;
  155. }
  156. int x = m_x;
  157. int y = m_y - 5;
  158. if (m_map->hasObjectInPostion(this, x, y, 5) == true) {
  159. return;
  160. }
  161. if (m_orientation != Ot_Up) {
  162. m_orientation = Ot_Up;
  163. m_pixIndex = 0;
  164. } else {
  165. if ((m_pixIndex + 1) < downImage.count()) {
  166. m_pixIndex++;
  167. } else {
  168. m_pixIndex = 0;
  169. }
  170. }
  171. m_image = upImage.at(m_pixIndex);
  172. m_x = x;
  173. m_y = y;
  174. resetRect();
  175. }
  176. void GameObject::moveDown()
  177. {
  178. if (m_map->isStop()) {
  179. return;
  180. }
  181. int x = m_x;
  182. int y = m_y + 5;
  183. if (m_map->hasObjectInPostion(this, x, y, 5) == true) {
  184. return;
  185. }
  186. if (m_orientation != Ot_Down) {
  187. m_orientation = Ot_Down;
  188. m_pixIndex = 0;
  189. } else {
  190. if ((m_pixIndex + 1) < downImage.count()) {
  191. m_pixIndex++;
  192. } else {
  193. m_pixIndex = 0;
  194. }
  195. }
  196. m_image = downImage.at(m_pixIndex);
  197. m_x = x;
  198. m_y = y;
  199. resetRect();
  200. }
  201. Orientation GameObject::orientation() const
  202. {
  203. return m_orientation;
  204. }
  205. void GameObject::setOrientation(const Orientation& orientation)
  206. {
  207. m_orientation = orientation;
  208. }
  209. bool GameObject::CaOtherGameObject(GameObject* other)
  210. {
  211. QRect rc;
  212. rc.setRect(m_x - 100, m_y - 100, 200, 200);
  213. if (rc.contains(QPoint(other->x(), other->y()))) {
  214. if (m_x > other->x()) {
  215. m_moveX = -1;
  216. } else {
  217. m_moveX = 1;
  218. }
  219. if (m_y > other->y()) {
  220. m_moveY = -1;
  221. } else {
  222. m_moveY = 1;
  223. }
  224. return true;
  225. } else {
  226. return false;
  227. }
  228. }
  229. bool GameObject::isHitOther(GameObject* other)
  230. {
  231. QRect rc1 = m_rect;
  232. QRect rc2 = other->rect();
  233. QPoint pt1 = rc1.center();
  234. QPoint pt2 = rc2.center();
  235. double l = sqrt((pt1.x() - pt2.x()) * (pt1.x() - pt2.x()) + (pt1.y() - pt2.y()) * (pt1.y() - pt2.y()));
  236. return l < 10;
  237. }