/demos/sub-attaq/graphicsscene.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 282 lines · 191 code · 35 blank · 56 comment · 21 complexity · c0414beb6b172a2e2569d012b86ee26b MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. //Own
  42. #include "graphicsscene.h"
  43. #include "states.h"
  44. #include "boat.h"
  45. #include "submarine.h"
  46. #include "torpedo.h"
  47. #include "bomb.h"
  48. #include "pixmapitem.h"
  49. #include "animationmanager.h"
  50. #include "qanimationstate.h"
  51. #include "progressitem.h"
  52. #include "textinformationitem.h"
  53. //Qt
  54. #include <QtCore/QPropertyAnimation>
  55. #include <QtCore/QSequentialAnimationGroup>
  56. #include <QtCore/QParallelAnimationGroup>
  57. #include <QtCore/QStateMachine>
  58. #include <QtCore/QFinalState>
  59. #include <QtCore/QPauseAnimation>
  60. #include <QtGui/QAction>
  61. #include <QtCore/QDir>
  62. #include <QtGui/QApplication>
  63. #include <QtGui/QMessageBox>
  64. #include <QtGui/QGraphicsView>
  65. #include <QtGui/QGraphicsSceneMouseEvent>
  66. #include <QtCore/QXmlStreamReader>
  67. GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode)
  68. : QGraphicsScene(x , y, width, height), mode(mode), boat(new Boat)
  69. {
  70. PixmapItem *backgroundItem = new PixmapItem(QString("background"),mode);
  71. backgroundItem->setZValue(1);
  72. backgroundItem->setPos(0,0);
  73. addItem(backgroundItem);
  74. PixmapItem *surfaceItem = new PixmapItem(QString("surface"),mode);
  75. surfaceItem->setZValue(3);
  76. surfaceItem->setPos(0,sealLevel() - surfaceItem->boundingRect().height()/2);
  77. addItem(surfaceItem);
  78. //The item that display score and level
  79. progressItem = new ProgressItem(backgroundItem);
  80. textInformationItem = new TextInformationItem(backgroundItem);
  81. textInformationItem->hide();
  82. //We create the boat
  83. addItem(boat);
  84. boat->setPos(this->width()/2, sealLevel() - boat->size().height());
  85. boat->hide();
  86. //parse the xml that contain all data of the game
  87. QXmlStreamReader reader;
  88. QFile file(":data.xml");
  89. file.open(QIODevice::ReadOnly);
  90. reader.setDevice(&file);
  91. LevelDescription currentLevel;
  92. while (!reader.atEnd()) {
  93. reader.readNext();
  94. if (reader.tokenType() == QXmlStreamReader::StartElement) {
  95. if (reader.name() == "submarine") {
  96. SubmarineDescription desc;
  97. desc.name = reader.attributes().value("name").toString();
  98. desc.points = reader.attributes().value("points").toString().toInt();
  99. desc.type = reader.attributes().value("type").toString().toInt();
  100. submarinesData.append(desc);
  101. } else if (reader.name() == "level") {
  102. currentLevel.id = reader.attributes().value("id").toString().toInt();
  103. currentLevel.name = reader.attributes().value("name").toString();
  104. } else if (reader.name() == "subinstance") {
  105. currentLevel.submarines.append(qMakePair(reader.attributes().value("type").toString().toInt(), reader.attributes().value("nb").toString().toInt()));
  106. }
  107. } else if (reader.tokenType() == QXmlStreamReader::EndElement) {
  108. if (reader.name() == "level") {
  109. levelsData.insert(currentLevel.id, currentLevel);
  110. currentLevel.submarines.clear();
  111. }
  112. }
  113. }
  114. }
  115. qreal GraphicsScene::sealLevel() const
  116. {
  117. return (mode == Big) ? 220 : 160;
  118. }
  119. void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
  120. {
  121. static const int nLetters = 10;
  122. static struct {
  123. char const *pix;
  124. qreal initX, initY;
  125. qreal destX, destY;
  126. } logoData[nLetters] = {
  127. {"s", -1000, -1000, 300, 150 },
  128. {"u", -800, -1000, 350, 150 },
  129. {"b", -600, -1000, 400, 120 },
  130. {"dash", -400, -1000, 460, 150 },
  131. {"a", 1000, 2000, 350, 250 },
  132. {"t", 800, 2000, 400, 250 },
  133. {"t2", 600, 2000, 430, 250 },
  134. {"a2", 400, 2000, 465, 250 },
  135. {"q", 200, 2000, 510, 250 },
  136. {"excl", 0, 2000, 570, 220 } };
  137. QSequentialAnimationGroup * lettersGroupMoving = new QSequentialAnimationGroup(this);
  138. QParallelAnimationGroup * lettersGroupFading = new QParallelAnimationGroup(this);
  139. for (int i = 0; i < nLetters; ++i) {
  140. PixmapItem *logo = new PixmapItem(QLatin1String(":/logo-") + logoData[i].pix, this);
  141. logo->setPos(logoData[i].initX, logoData[i].initY);
  142. logo->setZValue(i + 3);
  143. //creation of the animations for moving letters
  144. QPropertyAnimation *moveAnim = new QPropertyAnimation(logo, "pos", lettersGroupMoving);
  145. moveAnim->setEndValue(QPointF(logoData[i].destX, logoData[i].destY));
  146. moveAnim->setDuration(200);
  147. moveAnim->setEasingCurve(QEasingCurve::OutElastic);
  148. lettersGroupMoving->addPause(50);
  149. //creation of the animations for fading out the letters
  150. QPropertyAnimation *fadeAnim = new QPropertyAnimation(logo, "opacity", lettersGroupFading);
  151. fadeAnim->setDuration(800);
  152. fadeAnim->setEndValue(0);
  153. fadeAnim->setEasingCurve(QEasingCurve::OutQuad);
  154. }
  155. QStateMachine *machine = new QStateMachine(this);
  156. //This state is when the player is playing
  157. PlayState *gameState = new PlayState(this, machine);
  158. //Final state
  159. QFinalState *final = new QFinalState(machine);
  160. //Animation when the player enter in the game
  161. QAnimationState *lettersMovingState = new QAnimationState(machine);
  162. lettersMovingState->setAnimation(lettersGroupMoving);
  163. //Animation when the welcome screen disappear
  164. QAnimationState *lettersFadingState = new QAnimationState(machine);
  165. lettersFadingState->setAnimation(lettersGroupFading);
  166. //if new game then we fade out the welcome screen and start playing
  167. lettersMovingState->addTransition(newAction, SIGNAL(triggered()), lettersFadingState);
  168. lettersFadingState->addTransition(lettersFadingState, SIGNAL(animationFinished()), gameState);
  169. //New Game is triggered then player start playing
  170. gameState->addTransition(newAction, SIGNAL(triggered()), gameState);
  171. //Wanna quit, then connect to CTRL+Q
  172. gameState->addTransition(quitAction, SIGNAL(triggered()), final);
  173. lettersMovingState->addTransition(quitAction, SIGNAL(triggered()), final);
  174. //Welcome screen is the initial state
  175. machine->setInitialState(lettersMovingState);
  176. machine->start();
  177. //We reach the final state, then we quit
  178. connect(machine, SIGNAL(finished()), qApp, SLOT(quit()));
  179. }
  180. void GraphicsScene::addItem(Bomb *bomb)
  181. {
  182. bombs.insert(bomb);
  183. connect(bomb,SIGNAL(bombExecutionFinished()),this, SLOT(onBombExecutionFinished()));
  184. QGraphicsScene::addItem(bomb);
  185. }
  186. void GraphicsScene::addItem(Torpedo *torpedo)
  187. {
  188. torpedos.insert(torpedo);
  189. connect(torpedo,SIGNAL(torpedoExecutionFinished()),this, SLOT(onTorpedoExecutionFinished()));
  190. QGraphicsScene::addItem(torpedo);
  191. }
  192. void GraphicsScene::addItem(SubMarine *submarine)
  193. {
  194. submarines.insert(submarine);
  195. connect(submarine,SIGNAL(subMarineExecutionFinished()),this, SLOT(onSubMarineExecutionFinished()));
  196. QGraphicsScene::addItem(submarine);
  197. }
  198. void GraphicsScene::addItem(QGraphicsItem *item)
  199. {
  200. QGraphicsScene::addItem(item);
  201. }
  202. void GraphicsScene::onBombExecutionFinished()
  203. {
  204. Bomb *bomb = qobject_cast<Bomb *>(sender());
  205. bombs.remove(bomb);
  206. bomb->deleteLater();
  207. if (boat)
  208. boat->setBombsLaunched(boat->bombsLaunched() - 1);
  209. }
  210. void GraphicsScene::onTorpedoExecutionFinished()
  211. {
  212. Torpedo *torpedo = qobject_cast<Torpedo *>(sender());
  213. torpedos.remove(torpedo);
  214. torpedo->deleteLater();
  215. }
  216. void GraphicsScene::onSubMarineExecutionFinished()
  217. {
  218. SubMarine *submarine = qobject_cast<SubMarine *>(sender());
  219. submarines.remove(submarine);
  220. if (submarines.count() == 0)
  221. emit allSubMarineDestroyed(submarine->points());
  222. else
  223. emit subMarineDestroyed(submarine->points());
  224. submarine->deleteLater();
  225. }
  226. void GraphicsScene::clearScene()
  227. {
  228. foreach (SubMarine *sub, submarines) {
  229. sub->destroy();
  230. sub->deleteLater();
  231. }
  232. foreach (Torpedo *torpedo, torpedos) {
  233. torpedo->destroy();
  234. torpedo->deleteLater();
  235. }
  236. foreach (Bomb *bomb, bombs) {
  237. bomb->destroy();
  238. bomb->deleteLater();
  239. }
  240. submarines.clear();
  241. bombs.clear();
  242. torpedos.clear();
  243. AnimationManager::self()->unregisterAllAnimations();
  244. boat->stop();
  245. boat->hide();
  246. boat->setEnabled(true);
  247. }