1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtCore module of the Qt Toolkit.
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
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
44
#include "graphicsscene.h"
46
#include "submarine.h"
48
#include "animationmanager.h"
49
#include "progressitem.h"
50
#include "textinformationitem.h"
53
#include <QtGui/QMessageBox>
54
#include <QtGui/QGraphicsView>
55
#include <QtCore/QStateMachine>
56
#include <QtGui/QKeyEventTransition>
57
#include <QtCore/QFinalState>
59
PlayState::PlayState(GraphicsScene *scene, QState *parent)
68
PlayState::~PlayState()
73
void PlayState::onEntry(QEvent *)
78
//we hide the information
79
scene->textInformationItem->hide();
86
machine = new QStateMachine;
88
//This state is when player is playing
89
LevelState *levelState = new LevelState(scene, this, machine);
91
//This state is when the player is actually playing but the game is not paused
92
QState *playingState = new QState(levelState);
93
levelState->setInitialState(playingState);
95
//This state is when the game is paused
96
PauseState *pauseState = new PauseState(scene, levelState);
98
//We have one view, it receive the key press event
99
QKeyEventTransition *pressPplay = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P);
100
pressPplay->setTargetState(pauseState);
101
QKeyEventTransition *pressPpause = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P);
102
pressPpause->setTargetState(playingState);
104
//Pause "P" is triggered, the player pause the game
105
playingState->addTransition(pressPplay);
107
//To get back playing when the game has been paused
108
pauseState->addTransition(pressPpause);
110
//This state is when player have lost
111
LostState *lostState = new LostState(scene, this, machine);
113
//This state is when player have won
114
WinState *winState = new WinState(scene, this, machine);
116
//The boat has been destroyed then the game is finished
117
levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
119
//This transition check if we won or not
120
WinTransition *winTransition = new WinTransition(scene, this, winState);
122
//The boat has been destroyed then the game is finished
123
levelState->addTransition(winTransition);
125
//This state is an animation when the score changed
126
UpdateScoreState *scoreState = new UpdateScoreState(this, levelState);
128
//This transition update the score when a submarine die
129
UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, levelState);
130
scoreTransition->setTargetState(scoreState);
132
//The boat has been destroyed then the game is finished
133
playingState->addTransition(scoreTransition);
135
//We go back to play state
136
scoreState->addTransition(playingState);
138
//We start playing!!!
139
machine->setInitialState(levelState);
142
QFinalState *final = new QFinalState(machine);
144
//This transition is triggered when the player press space after completing a level
145
CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space);
146
spaceTransition->setTargetState(levelState);
147
winState->addTransition(spaceTransition);
149
//We lost we should reach the final state
150
lostState->addTransition(lostState, SIGNAL(finished()), final);
155
LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
158
void LevelState::onEntry(QEvent *)
163
void LevelState::initializeLevel()
165
//we re-init the boat
166
scene->boat->setPos(scene->width()/2, scene->sealLevel() - scene->boat->size().height());
167
scene->boat->setCurrentSpeed(0);
168
scene->boat->setCurrentDirection(Boat::None);
169
scene->boat->setBombsLaunched(0);
171
scene->setFocusItem(scene->boat, Qt::OtherFocusReason);
174
scene->progressItem->setScore(game->score);
175
scene->progressItem->setLevel(game->currentLevel + 1);
177
GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel);
179
for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) {
181
QPair<int,int> subContent = currentLevelDescription.submarines.at(i);
182
GraphicsScene::SubmarineDescription submarineDesc = scene->submarinesData.at(subContent.first);
184
for (int j = 0; j < subContent.second; ++j ) {
185
SubMarine *sub = new SubMarine(submarineDesc.type, submarineDesc.name, submarineDesc.points);
187
int random = (qrand() % 15 + 1);
188
qreal x = random == 13 || random == 5 ? 0 : scene->width() - sub->size().width();
189
qreal y = scene->height() -(qrand() % 150 + 1) - sub->size().height();
191
sub->setCurrentDirection(x == 0 ? SubMarine::Right : SubMarine::Left);
192
sub->setCurrentSpeed(qrand() % 3 + 1);
198
PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent),scene(scene)
201
void PauseState::onEntry(QEvent *)
203
AnimationManager::self()->pauseAll();
204
scene->boat->setEnabled(false);
206
void PauseState::onExit(QEvent *)
208
AnimationManager::self()->resumeAll();
209
scene->boat->setEnabled(true);
210
scene->boat->setFocus();
214
LostState::LostState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
218
void LostState::onEntry(QEvent *)
220
//The message to display
221
QString message = QString("You lose on level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score);
223
//We set the level back to 0
224
game->currentLevel = 0;
226
//We set the score back to 0
232
//We inform the player
233
scene->textInformationItem->setMessage(message);
234
scene->textInformationItem->show();
237
void LostState::onExit(QEvent *)
239
//we hide the information
240
scene->textInformationItem->hide();
244
WinState::WinState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
248
void WinState::onEntry(QEvent *)
254
if (scene->levelsData.size() - 1 != game->currentLevel) {
255
message = QString("You win the level %1. Your score is %2.\nPress Space to continue.").arg(game->currentLevel+1).arg(game->score);
256
//We increment the level number
257
game->currentLevel++;
259
message = QString("You finish the game on level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score);
260
//We set the level back to 0
261
game->currentLevel = 0;
262
//We set the score back to 0
266
//We inform the player
267
scene->textInformationItem->setMessage(message);
268
scene->textInformationItem->show();
271
void WinState::onExit(QEvent *)
273
//we hide the information
274
scene->textInformationItem->hide();
277
/** UpdateScore State */
278
UpdateScoreState::UpdateScoreState(PlayState *g, QState *parent) : QState(parent), game(g)
282
/** Win transition */
283
UpdateScoreTransition::UpdateScoreTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
284
: QSignalTransition(scene,SIGNAL(subMarineDestroyed(int))),
285
game(game), scene(scene)
287
setTargetState(target);
290
bool UpdateScoreTransition::eventTest(QEvent *event)
292
if (!QSignalTransition::eventTest(event))
294
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event);
295
game->score += se->arguments().at(0).toInt();
296
scene->progressItem->setScore(game->score);
300
/** Win transition */
301
WinTransition::WinTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
302
: QSignalTransition(scene,SIGNAL(allSubMarineDestroyed(int))),
303
game(game), scene(scene)
305
setTargetState(target);
308
bool WinTransition::eventTest(QEvent *event)
310
if (!QSignalTransition::eventTest(event))
312
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event);
313
game->score += se->arguments().at(0).toInt();
314
scene->progressItem->setScore(game->score);
318
/** Space transition */
319
CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key)
320
: QKeyEventTransition(widget, type, key),
325
bool CustomSpaceTransition::eventTest(QEvent *event)
327
if (!QKeyEventTransition::eventTest(event))
329
return (game->currentLevel != 0);