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/QSignalEvent>
58 #include <QtCore/QFinalState>
60 PlayState::PlayState(GraphicsScene
*scene
, QState
*parent
)
69 PlayState::~PlayState()
73 void PlayState::onEntry(QEvent
*)
84 machine
= new QStateMachine(this);
86 //This state is when player is playing
87 LevelState
*levelState
= new LevelState(scene
, this, machine
);
89 //This state is when the player is actually playing but the game is not paused
90 QState
*playingState
= new QState(levelState
);
91 levelState
->setInitialState(playingState
);
93 //This state is when the game is paused
94 PauseState
*pauseState
= new PauseState(scene
, levelState
);
96 //We have one view, it receive the key press event
97 QKeyEventTransition
*pressPplay
= new QKeyEventTransition(scene
->views().at(0), QEvent::KeyPress
, Qt::Key_P
);
98 pressPplay
->setTargetState(pauseState
);
99 QKeyEventTransition
*pressPpause
= new QKeyEventTransition(scene
->views().at(0), QEvent::KeyPress
, Qt::Key_P
);
100 pressPpause
->setTargetState(playingState
);
102 //Pause "P" is triggered, the player pause the game
103 playingState
->addTransition(pressPplay
);
105 //To get back playing when the game has been paused
106 pauseState
->addTransition(pressPpause
);
108 //This state is when player have lost
109 LostState
*lostState
= new LostState(scene
, this, machine
);
111 //This state is when player have won
112 WinState
*winState
= new WinState(scene
, this, machine
);
114 //The boat has been destroyed then the game is finished
115 levelState
->addTransition(scene
->boat
, SIGNAL(boatExecutionFinished()),lostState
);
117 //This transition check if we won or not
118 WinTransition
*winTransition
= new WinTransition(scene
, this, winState
);
120 //The boat has been destroyed then the game is finished
121 levelState
->addTransition(winTransition
);
123 //This state is an animation when the score changed
124 UpdateScoreState
*scoreState
= new UpdateScoreState(this, levelState
);
126 //This transition update the score when a submarine die
127 UpdateScoreTransition
*scoreTransition
= new UpdateScoreTransition(scene
, this, levelState
);
128 scoreTransition
->setTargetState(scoreState
);
130 //The boat has been destroyed then the game is finished
131 playingState
->addTransition(scoreTransition
);
133 //We go back to play state
134 scoreState
->addTransition(playingState
);
136 //We start playing!!!
137 machine
->setInitialState(levelState
);
140 QFinalState
*final
= new QFinalState(machine
);
142 //This transition is triggered when the player press space after completing a level
143 CustomSpaceTransition
*spaceTransition
= new CustomSpaceTransition(scene
->views().at(0), this, QEvent::KeyPress
, Qt::Key_Space
);
144 spaceTransition
->setTargetState(levelState
);
145 winState
->addTransition(spaceTransition
);
147 //We lost we should reach the final state
148 lostState
->addTransition(lostState
, SIGNAL(finished()), final
);
153 LevelState::LevelState(GraphicsScene
*scene
, PlayState
*game
, QState
*parent
) : QState(parent
), scene(scene
), game(game
)
156 void LevelState::onEntry(QEvent
*)
161 void LevelState::initializeLevel()
163 //we re-init the boat
164 scene
->boat
->setPos(scene
->width()/2, scene
->sealLevel() - scene
->boat
->size().height());
165 scene
->boat
->setCurrentSpeed(0);
166 scene
->boat
->setCurrentDirection(Boat::None
);
167 scene
->boat
->setBombsLaunched(0);
169 scene
->setFocusItem(scene
->boat
,Qt::OtherFocusReason
);
172 scene
->progressItem
->setScore(game
->score
);
173 scene
->progressItem
->setLevel(game
->currentLevel
+ 1);
175 GraphicsScene::LevelDescription currentLevelDescription
= scene
->levelsData
.value(game
->currentLevel
);
177 for (int i
= 0; i
< currentLevelDescription
.submarines
.size(); ++i
) {
179 QPair
<int,int> subContent
= currentLevelDescription
.submarines
.at(i
);
180 GraphicsScene::SubmarineDescription submarineDesc
= scene
->submarinesData
.at(subContent
.first
);
182 for (int j
= 0; j
< subContent
.second
; ++j
) {
183 SubMarine
*sub
= new SubMarine(submarineDesc
.type
, submarineDesc
.name
, submarineDesc
.points
);
185 int random
= (qrand() % 15 + 1);
186 qreal x
= random
== 13 || random
== 5 ? 0 : scene
->width() - sub
->size().width();
187 qreal y
= scene
->height() -(qrand() % 150 + 1) - sub
->size().height();
189 sub
->setCurrentDirection(x
== 0 ? SubMarine::Right
: SubMarine::Left
);
190 sub
->setCurrentSpeed(qrand() % 3 + 1);
196 PauseState::PauseState(GraphicsScene
*scene
, QState
*parent
) : QState(parent
),scene(scene
)
199 void PauseState::onEntry(QEvent
*)
201 AnimationManager::self()->pauseAll();
202 scene
->boat
->setEnabled(false);
204 void PauseState::onExit(QEvent
*)
206 AnimationManager::self()->resumeAll();
207 scene
->boat
->setEnabled(true);
208 scene
->boat
->setFocus();
212 LostState::LostState(GraphicsScene
*scene
, PlayState
*game
, QState
*parent
) : QState(parent
), scene(scene
), game(game
)
216 void LostState::onEntry(QEvent
*)
218 //The message to display
219 QString message
= QString("You lose on level %1. Your score is %2.").arg(game
->currentLevel
+1).arg(game
->score
);
221 //We set the level back to 0
222 game
->currentLevel
= 0;
224 //We set the score back to 0
230 //We inform the player
231 scene
->textInformationItem
->setMessage(message
);
232 scene
->textInformationItem
->show();
235 void LostState::onExit(QEvent
*)
237 //we hide the information
238 scene
->textInformationItem
->hide();
242 WinState::WinState(GraphicsScene
*scene
, PlayState
*game
, QState
*parent
) : QState(parent
), scene(scene
), game(game
)
246 void WinState::onEntry(QEvent
*)
252 if (scene
->levelsData
.size() - 1 != game
->currentLevel
) {
253 message
= QString("You win the level %1. Your score is %2.\nPress Space to continue.").arg(game
->currentLevel
+1).arg(game
->score
);
254 //We increment the level number
255 game
->currentLevel
++;
257 message
= QString("You finish the game on level %1. Your score is %2.").arg(game
->currentLevel
+1).arg(game
->score
);
258 //We set the level back to 0
259 game
->currentLevel
= 0;
260 //We set the score back to 0
264 //We inform the player
265 scene
->textInformationItem
->setMessage(message
);
266 scene
->textInformationItem
->show();
269 void WinState::onExit(QEvent
*)
271 //we hide the information
272 scene
->textInformationItem
->hide();
275 /** UpdateScore State */
276 UpdateScoreState::UpdateScoreState(PlayState
*game
, QState
*parent
) : QState(parent
)
280 void UpdateScoreState::onEntry(QEvent
*e
)
285 /** Win transition */
286 UpdateScoreTransition::UpdateScoreTransition(GraphicsScene
*scene
, PlayState
*game
, QAbstractState
*target
)
287 : QSignalTransition(scene
,SIGNAL(subMarineDestroyed(int))),
288 game(game
), scene(scene
)
290 setTargetState(target
);
293 bool UpdateScoreTransition::eventTest(QEvent
*event
)
295 if (!QSignalTransition::eventTest(event
))
298 QSignalEvent
*se
= static_cast<QSignalEvent
*>(event
);
299 game
->score
+= se
->arguments().at(0).toInt();
300 scene
->progressItem
->setScore(game
->score
);
305 /** Win transition */
306 WinTransition::WinTransition(GraphicsScene
*scene
, PlayState
*game
, QAbstractState
*target
)
307 : QSignalTransition(scene
,SIGNAL(allSubMarineDestroyed(int))),
308 game(game
), scene(scene
)
310 setTargetState(target
);
313 bool WinTransition::eventTest(QEvent
*event
)
315 if (!QSignalTransition::eventTest(event
))
318 QSignalEvent
*se
= static_cast<QSignalEvent
*>(event
);
319 game
->score
+= se
->arguments().at(0).toInt();
320 scene
->progressItem
->setScore(game
->score
);
325 /** Space transition */
326 CustomSpaceTransition::CustomSpaceTransition(QWidget
*widget
, PlayState
*game
, QEvent::Type type
, int key
)
327 : QKeyEventTransition(widget
, type
, key
),
332 bool CustomSpaceTransition::eventTest(QEvent
*event
)
335 if (!QKeyEventTransition::eventTest(event
))
337 if (game
->currentLevel
!= 0)