gameengine.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "gameengine.h"
  2. #include <QDebug>
  3. #include <QTimerEvent>
  4. #include <QTime>
  5. #include <QDesktopServices>
  6. #include <QSettings>
  7. const int TIMER_SPEED = 80;
  8. GameEngine::GameEngine(QObject* parent)
  9. :QObject(parent)
  10. {
  11. m_timerId = 0;
  12. m_doEnemyMissile = 1500 / TIMER_SPEED;
  13. m_GameGml = 0;
  14. m_myShipGml = 0;
  15. clearQmlObjects();
  16. // For random
  17. QTime time = QTime::currentTime();
  18. qsrand((uint)time.msec());
  19. QSettings s("info.vivainio", "qatbowling");
  20. m_hiScore = s.value("hiscore", QVariant(0)).toInt();
  21. }
  22. GameEngine::~GameEngine()
  23. {
  24. #ifdef Q_OS_SYMBIAN
  25. delete iVibrate;
  26. #endif
  27. }
  28. QVariant GameEngine::randInt(QVariant low, QVariant high)
  29. {
  30. // Random number between low and high
  31. return qrand() % ((high.toInt() + 1) - low.toInt()) + low.toInt();
  32. }
  33. void GameEngine::findQmlObjects()
  34. {
  35. if (m_GameGml) {
  36. //qDebug() << "GameEngine::findQmlObjects()";
  37. m_enemyList.clear();
  38. //qDebug() << "GameEngine::findQmlObjects() find enemies from: level QML";
  39. findEnemies(m_GameGml);
  40. QObjectList l;
  41. findObjects(m_GameGml, "player", l);
  42. m_myShipGml = qobject_cast<QDeclarativeItem*>(l.at(0));
  43. } else {
  44. //qDebug() << "no root qml yet";
  45. }
  46. }
  47. void GameEngine::clearQmlObjects()
  48. {
  49. m_enemyList.clear();
  50. }
  51. void GameEngine::findObjects(QObject *rootObject, const QString& objName, QObjectList& res)
  52. {
  53. if (rootObject) {
  54. QObjectList list = rootObject->children();
  55. QObject* item;
  56. foreach(item,list) {
  57. if (item->children().count()>0 && item->objectName()!=objName) {
  58. //qDebug() << "Enemy childs founds from: " << item->objectName();
  59. findObjects( item, objName, res);
  60. } else {
  61. if (item->objectName()==objName) {
  62. //qDebug() << "Enemy child founds: " << item->objectName();
  63. QDeclarativeItem* enemy = static_cast<QDeclarativeItem*>(item);
  64. res.append(enemy);
  65. }
  66. }
  67. }
  68. } else {
  69. //qDebug() << "GameEngine::findEnemies() rootObject NULL";
  70. }
  71. }
  72. void GameEngine::findEnemies(QObject *rootObject)
  73. {
  74. if (rootObject) {
  75. QObjectList list = rootObject->children();
  76. QObject* item;
  77. foreach(item,list) {
  78. if (item->children().count()>0 && item->objectName()!="enemy") {
  79. //qDebug() << "Enemy childs founds from: " << item->objectName();
  80. findEnemies(item);
  81. } else {
  82. if (item->objectName()=="enemy") {
  83. //qDebug() << "Enemy child founds: " << item->objectName();
  84. QDeclarativeItem* enemy = static_cast<QDeclarativeItem*>(item);
  85. m_enemyList.append(enemy);
  86. }
  87. }
  88. }
  89. } else {
  90. //qDebug() << "GameEngine::findEnemies() rootObject NULL";
  91. }
  92. }
  93. void GameEngine::setGameQml(QObject* o)
  94. {
  95. m_GameGml = static_cast<QDeclarativeItem*>(o);
  96. }
  97. void GameEngine::doHitTest()
  98. {
  99. //QDeclarativeItem* missile = 0;
  100. QDeclarativeItem* enemy = 0;
  101. if (!m_myShipGml) {
  102. return;
  103. }
  104. //QRectF playerRect = m_myShipGml->boundingRect();
  105. QRectF playerRect (m_myShipGml->pos(),QSize(m_myShipGml->boundingRect().width(),m_myShipGml->boundingRect().height()));
  106. // Check ship collision
  107. for(int e=0; e<m_enemyList.count(); e++) {
  108. enemy = m_enemyList[e];
  109. if (enemy->opacity()==0) {
  110. continue;
  111. }
  112. QPointF enemyP = enemy->pos();
  113. QRectF enemyR(enemyP,QSize(enemy->boundingRect().width(),enemy->boundingRect().height()));
  114. if (enemyR.intersects(playerRect)) {
  115. //qDebug() << "collide " << e;
  116. emit collision(m_myShipGml, enemy);
  117. }
  118. }
  119. }
  120. QVariant GameEngine::isSymbian()
  121. {
  122. #ifdef Q_OS_SYMBIAN
  123. return QVariant(true);
  124. #else
  125. return QVariant(false);
  126. #endif
  127. }
  128. QVariant GameEngine::isMaemo()
  129. {
  130. #ifdef Q_WS_MAEMO_5
  131. return QVariant(true);
  132. #else
  133. return QVariant(false);
  134. #endif
  135. }
  136. QVariant GameEngine::isWindows()
  137. {
  138. #ifdef Q_OS_WIN
  139. return QVariant(true);
  140. #else
  141. return QVariant(false);
  142. #endif
  143. }
  144. void GameEngine::vibra()
  145. {
  146. #ifdef Q_OS_SYMBIAN
  147. if (iVibrate){
  148. TRAPD(err, iVibrate->StartVibraL(4000,KHWRMVibraMaxIntensity));
  149. }
  150. #endif
  151. }
  152. void GameEngine::fastVibra()
  153. {
  154. #ifdef Q_OS_SYMBIAN
  155. if (iVibrate){
  156. TRAPD(err, iVibrate->StartVibraL(100,KHWRMVibraMaxIntensity));
  157. }
  158. #endif
  159. }
  160. void GameEngine::doFrame()
  161. {
  162. doHitTest();
  163. }
  164. int GameEngine::hiScore() const
  165. {
  166. return m_hiScore;
  167. }
  168. void GameEngine::setHiScore(int arg)
  169. {
  170. QSettings s("info.vivainio", "qatbowling");
  171. s.setValue("hiscore", QVariant(arg));
  172. m_hiScore = arg;
  173. }