source: trunk/demos/sub-attaq/states.cpp@ 887

Last change on this file since 887 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 10.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
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"
51
52//Qt
53#include <QtGui/QMessageBox>
54#include <QtGui/QGraphicsView>
55#include <QtCore/QStateMachine>
56#include <QtGui/QKeyEventTransition>
57#include <QtCore/QFinalState>
58
59PlayState::PlayState(GraphicsScene *scene, QState *parent)
60 : QState(parent),
61 scene(scene),
62 machine(0),
63 currentLevel(0),
64 score(0)
65{
66}
67
68PlayState::~PlayState()
69{
70 delete machine;
71}
72
73void PlayState::onEntry(QEvent *)
74{
75 //We are now playing?
76 if (machine) {
77 machine->stop();
78 //we hide the information
79 scene->textInformationItem->hide();
80 scene->clearScene();
81 currentLevel = 0;
82 score = 0;
83 delete machine;
84 }
85
86 machine = new QStateMachine;
87
88 //This state is when player is playing
89 LevelState *levelState = new LevelState(scene, this, machine);
90
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);
94
95 //This state is when the game is paused
96 PauseState *pauseState = new PauseState(scene, levelState);
97
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);
103
104 //Pause "P" is triggered, the player pause the game
105 playingState->addTransition(pressPplay);
106
107 //To get back playing when the game has been paused
108 pauseState->addTransition(pressPpause);
109
110 //This state is when player have lost
111 LostState *lostState = new LostState(scene, this, machine);
112
113 //This state is when player have won
114 WinState *winState = new WinState(scene, this, machine);
115
116 //The boat has been destroyed then the game is finished
117 levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
118
119 //This transition check if we won or not
120 WinTransition *winTransition = new WinTransition(scene, this, winState);
121
122 //The boat has been destroyed then the game is finished
123 levelState->addTransition(winTransition);
124
125 //This state is an animation when the score changed
126 UpdateScoreState *scoreState = new UpdateScoreState(this, levelState);
127
128 //This transition update the score when a submarine die
129 UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, levelState);
130 scoreTransition->setTargetState(scoreState);
131
132 //The boat has been destroyed then the game is finished
133 playingState->addTransition(scoreTransition);
134
135 //We go back to play state
136 scoreState->addTransition(playingState);
137
138 //We start playing!!!
139 machine->setInitialState(levelState);
140
141 //Final state
142 QFinalState *final = new QFinalState(machine);
143
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);
148
149 //We lost we should reach the final state
150 lostState->addTransition(lostState, SIGNAL(finished()), final);
151
152 machine->start();
153}
154
155LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
156{