level.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #ifndef LEVEL_H
  5. #define LEVEL_H
  6. #include <QObject>
  7. #include <QVector>
  8. #include <QDeclarativeItem>
  9. #include <QFile>
  10. #include <QAudio>
  11. #include <QAudioFormat>
  12. class QAudioOutput;
  13. class QThread;
  14. class SoundPlayer;
  15. /***************************************************************************************************
  16. *Class representing the games level. *
  17. ***************************************************************************************************/
  18. class Level : public QObject
  19. {
  20. Q_OBJECT
  21. public:
  22. Level();
  23. ~Level();
  24. /***********************************************************************************************
  25. *load method accessible from QML. Loads the level from xml. *
  26. ***********************************************************************************************/
  27. Q_INVOKABLE bool load(QString fileName = "");
  28. /***********************************************************************************************
  29. *Objects rowCount property accessible in QML. *
  30. ***********************************************************************************************/
  31. Q_PROPERTY( int rowCount READ rowCount )
  32. int rowCount() const { return m_rowCount;}
  33. /***********************************************************************************************
  34. *Objects columnCount property accessible in QML. *
  35. ***********************************************************************************************/
  36. Q_PROPERTY( int columnCount READ columnCount )
  37. int columnCount() const { return m_columnCount;}
  38. /***********************************************************************************************
  39. *item method accessible from QML. Returns a string representing the item at row column. *
  40. ***********************************************************************************************/
  41. Q_INVOKABLE QString item(int row, int column) const;
  42. /***********************************************************************************************
  43. *intersects method accessible from QML. Verifies whether the two items intersect. *
  44. ***********************************************************************************************/
  45. Q_INVOKABLE bool intersects(QDeclarativeItem *,QDeclarativeItem *);
  46. /***********************************************************************************************
  47. *collidingItems method accessible from QML. *
  48. *Goes through the items colliding with the given item, checking their type and calculating the *
  49. *items intersection if necessary. *
  50. *Returns a QVariant containing the list of pointers to QObjects colliding with the item. *
  51. ***********************************************************************************************/
  52. Q_INVOKABLE QVariant collidingItems(QDeclarativeItem *) const;
  53. /***********************************************************************************************
  54. *intersects method accessible from QML. Returns the two items intersection rectangle. *
  55. ***********************************************************************************************/
  56. Q_INVOKABLE QRect intersected(QDeclarativeItem *,QDeclarativeItem *) const;
  57. /***********************************************************************************************
  58. *startBGSound method accessible from QML. Starts the background music. *
  59. ***********************************************************************************************/
  60. Q_INVOKABLE void startBGSound();
  61. /***********************************************************************************************
  62. *playBoing method accessible from QML. Plays the boing sound. *
  63. ***********************************************************************************************/
  64. Q_INVOKABLE void playBoing();
  65. /***********************************************************************************************
  66. *playGroan method accessible from QML. Plays the groan sound. *
  67. ***********************************************************************************************/
  68. Q_INVOKABLE void playGroan();
  69. signals:
  70. /***********************************************************************************************
  71. *A signal emitted to request background music playback. *
  72. ***********************************************************************************************/
  73. void bgSoundRequest();
  74. /***********************************************************************************************
  75. *A signal emitted to request boing sound playback. *
  76. ***********************************************************************************************/
  77. void boingSoundRequest();
  78. /***********************************************************************************************
  79. *A signal emitted to request groan sound playback. *
  80. ***********************************************************************************************/
  81. void groanSoundRequest();
  82. /***********************************************************************************************
  83. *A signal emitted to request the player to stop all playback. *
  84. ***********************************************************************************************/
  85. void stopSounds();
  86. /***********************************************************************************************
  87. *A signal emitted to signalise that it is now safe to quit the application. *
  88. ***********************************************************************************************/
  89. void readyToQuit();
  90. private:
  91. /***********************************************************************************************
  92. *A vector containing strings representing the game boards rows. *
  93. ***********************************************************************************************/
  94. QVector<QString> m_map;
  95. /***********************************************************************************************
  96. *The game boards row and column count. *
  97. ***********************************************************************************************/
  98. int m_rowCount;
  99. int m_columnCount;
  100. /***********************************************************************************************
  101. *The sound player object. *
  102. ***********************************************************************************************/
  103. SoundPlayer *m_player;
  104. };
  105. /***************************************************************************************************
  106. *Class representing the sound player. *
  107. ***************************************************************************************************/
  108. class SoundPlayer : public QObject
  109. {
  110. Q_OBJECT
  111. public:
  112. SoundPlayer();
  113. ~SoundPlayer();
  114. public slots:
  115. /***********************************************************************************************
  116. *Starts the background music. *
  117. ***********************************************************************************************/
  118. void startBGSound();
  119. /***********************************************************************************************
  120. *Plays the boing sound. *
  121. ***********************************************************************************************/
  122. void playBoing();
  123. /***********************************************************************************************
  124. *Plays the groan sound. *
  125. ***********************************************************************************************/
  126. void playGroan();
  127. /***********************************************************************************************
  128. *Stops all playback. *
  129. ***********************************************************************************************/
  130. void stopSounds();
  131. private slots:
  132. /***********************************************************************************************
  133. *A slot called every time a QAudioOutput changes its state. *
  134. *Resumes background music, cleans the state of the boing and groan sounds. *
  135. ***********************************************************************************************/
  136. void outputStateChanged(QAudio::State state);
  137. signals:
  138. /***********************************************************************************************
  139. *A signal emitted to signalise that all sound playback has now been stopped. *
  140. ***********************************************************************************************/
  141. void soundsStopped();
  142. private:
  143. /***********************************************************************************************
  144. *The sound format used by the player. *
  145. ***********************************************************************************************/
  146. QAudioFormat m_format;
  147. /***********************************************************************************************
  148. *Sound outputs. *
  149. ***********************************************************************************************/
  150. QAudioOutput * m_musicBGOutput;
  151. QAudioOutput * m_boingOutput;
  152. QAudioOutput * m_groanOutput;
  153. /***********************************************************************************************
  154. *Sound files. *
  155. ***********************************************************************************************/
  156. QFile m_musicBGFile;
  157. QFile m_boingtFile;
  158. QFile m_groanFile;
  159. /***********************************************************************************************
  160. *Flags indicating sound playback. *
  161. ***********************************************************************************************/
  162. bool m_musicBGPlaying;
  163. bool m_boingPlaying;
  164. bool m_groanPlaying;
  165. /***********************************************************************************************
  166. *A flag indicating if the sound back-end supports the players format. *
  167. ***********************************************************************************************/
  168. bool m_soundSupport;
  169. /***********************************************************************************************
  170. *The sound players dedicated thread. *
  171. ***********************************************************************************************/
  172. QThread *m_soundThread;
  173. };
  174. /***************************************************************************************************
  175. *Class representing an ellipse item used for the games character. *
  176. ***************************************************************************************************/
  177. class EllipseItem : public QDeclarativeItem
  178. {
  179. Q_OBJECT
  180. public:
  181. EllipseItem(QDeclarativeItem *parent = 0);
  182. /***********************************************************************************************
  183. *The ellipse items shape. Returns a painter path containing an ellipse. *
  184. ***********************************************************************************************/
  185. QPainterPath shape() const;
  186. /***********************************************************************************************
  187. *Draws the item. *
  188. ***********************************************************************************************/
  189. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  190. QWidget *widget);
  191. /***********************************************************************************************
  192. *Objects margin property accessible in QML. *
  193. ***********************************************************************************************/
  194. Q_PROPERTY(int margin READ margin WRITE setMargin)
  195. int margin() const { return m_margin;}
  196. void setMargin(int margin) { m_margin = margin;}
  197. /***********************************************************************************************
  198. *Objects burn property accessible in QML. *
  199. ***********************************************************************************************/
  200. Q_PROPERTY(bool burn READ burn WRITE setBurn)
  201. bool burn() const { return m_burn;}
  202. void setBurn(bool burn) { m_burn = burn;}
  203. /***********************************************************************************************
  204. *A vector containing the rectangles representing intersections with other items. *
  205. ***********************************************************************************************/
  206. QVector<QRect> m_rect;
  207. private:
  208. /***********************************************************************************************
  209. *Objects margin value. *
  210. ***********************************************************************************************/
  211. int m_margin;
  212. /***********************************************************************************************
  213. *Objects burn flag. *
  214. ***********************************************************************************************/
  215. bool m_burn;
  216. };
  217. /***************************************************************************************************
  218. *Class representing a multitouch area, used for game control items(arrows etc.). *
  219. ***************************************************************************************************/
  220. class MultiTouchItem : public QDeclarativeItem
  221. {
  222. Q_OBJECT
  223. public:
  224. MultiTouchItem(QDeclarativeItem *parent = 0);
  225. /***********************************************************************************************
  226. *Draws the item. *
  227. ***********************************************************************************************/
  228. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  229. QWidget *widget);
  230. /***********************************************************************************************
  231. *Objects source property accessible in QML. Also determines the items pixmap. *
  232. ***********************************************************************************************/
  233. Q_PROPERTY(QString source READ source WRITE setSource)
  234. void setSource(const QString &s);
  235. QString source() const { return m_source; }
  236. protected:
  237. /***********************************************************************************************
  238. *A reimplemented sceneEvent method, used to handle touch and mouse press events. *
  239. ***********************************************************************************************/
  240. bool sceneEvent(QEvent *event);
  241. signals:
  242. /***********************************************************************************************
  243. *A signal emitted to signalise that the item has been pressed. *
  244. ***********************************************************************************************/
  245. void pressed();
  246. /***********************************************************************************************
  247. *A signal emitted to signalise that the item has been released. *
  248. ***********************************************************************************************/
  249. void released();
  250. private:
  251. /***********************************************************************************************
  252. *The items source string. *
  253. ***********************************************************************************************/
  254. QString m_source;
  255. /***********************************************************************************************
  256. *The items pixmap. *
  257. ***********************************************************************************************/
  258. QPixmap m_pixmap;
  259. };
  260. #endif // LEVEL_H