Fix alpha setting in Designer's "Edit Palette" window
[qt-netbsd.git] / demos / sub-attaq / states.cpp
blob4a9d845053b7736e27dd0f63b33a7217131dc53b
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.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 //Own
43 #include "states.h"
44 #include "graphicsscene.h"
45 #include "boat.h"
46 #include "submarine.h"
47 #include "torpedo.h"
48 #include "animationmanager.h"
49 #include "progressitem.h"
50 #include "textinformationitem.h"
52 //Qt
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)
61 : QState(parent),
62 scene(scene),
63 machine(0),
64 currentLevel(0),
65 score(0)
69 PlayState::~PlayState()
73 void PlayState::onEntry(QEvent *)
75 //We are now playing?
76 if (machine) {
77 machine->stop();
78 scene->clearScene();
79 currentLevel = 0;
80 score = 0;
81 delete machine;
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);
139 //Final state
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);
150 machine->start();
153 LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
156 void LevelState::onEntry(QEvent *)
158 initializeLevel();
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);
168 scene->boat->show();
169 scene->setFocusItem(scene->boat,Qt::OtherFocusReason);
170 scene->boat->run();
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);
184 scene->addItem(sub);
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();
188 sub->setPos(x,y);
189 sub->setCurrentDirection(x == 0 ? SubMarine::Right : SubMarine::Left);
190 sub->setCurrentSpeed(qrand() % 3 + 1);
195 /** Pause State */
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();
211 /** Lost State */
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
225 game->score = 0;
227 //We clear the scene
228 scene->clearScene();
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();
241 /** Win State */
242 WinState::WinState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
246 void WinState::onEntry(QEvent *)
248 //We clear the scene
249 scene->clearScene();
251 QString message;
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++;
256 } else {
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
261 game->score = 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)
278 this->game = game;
280 void UpdateScoreState::onEntry(QEvent *e)
282 QState::onEntry(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))
296 return false;
297 else {
298 QSignalEvent *se = static_cast<QSignalEvent*>(event);
299 game->score += se->arguments().at(0).toInt();
300 scene->progressItem->setScore(game->score);
301 return true;
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))
316 return false;
317 else {
318 QSignalEvent *se = static_cast<QSignalEvent*>(event);
319 game->score += se->arguments().at(0).toInt();
320 scene->progressItem->setScore(game->score);
321 return true;
325 /** Space transition */
326 CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key)
327 : QKeyEventTransition(widget, type, key),
328 game(game)
332 bool CustomSpaceTransition::eventTest(QEvent *event)
334 Q_UNUSED(event);
335 if (!QKeyEventTransition::eventTest(event))
336 return false;
337 if (game->currentLevel != 0)
338 return true;
339 else
340 return false;