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