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 examples 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 ****************************************************************************/
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
58 #include "graphicsscene.h"
61 #include <QtGui/QKeyEventTransition>
63 static const int MAX_BOMB
= 5;
66 //These transtion test if we have to stop the boat (i.e current speed is 1)
67 class KeyStopTransition
: public QKeyEventTransition
70 KeyStopTransition(Boat
*boat
, QEvent::Type type
, int key
)
71 : QKeyEventTransition(boat
, type
, key
)
77 virtual bool eventTest(QEvent
*event
)
80 if (!QKeyEventTransition::eventTest(event
))
82 if (boat
->currentSpeed() == 1)
92 //These transtion test if we have to move the boat (i.e current speed was 0 or another value)
93 class KeyMoveTransition
: public QKeyEventTransition
96 KeyMoveTransition(Boat
*boat
, QEvent::Type type
, int key
)
97 : QKeyEventTransition(boat
, type
, key
)
103 virtual bool eventTest(QEvent
*event
)
106 if (!QKeyEventTransition::eventTest(event
))
108 if (boat
->currentSpeed() >= 0)
114 void onTransition(QEvent
*)
116 //We decrease the speed if needed
117 if (key
== Qt::Key_Left
&& boat
->currentDirection() == Boat::Right
)
118 boat
->setCurrentSpeed(boat
->currentSpeed() - 1);
119 else if (key
== Qt::Key_Right
&& boat
->currentDirection() == Boat::Left
)
120 boat
->setCurrentSpeed(boat
->currentSpeed() - 1);
121 else if (boat
->currentSpeed() < 3)
122 boat
->setCurrentSpeed(boat
->currentSpeed() + 1);
123 boat
->updateBoatMovement();
130 //This transition trigger the bombs launch
131 class KeyLaunchTransition
: public QKeyEventTransition
134 KeyLaunchTransition(Boat
*boat
, QEvent::Type type
, int key
)
135 : QKeyEventTransition(boat
, type
, key
)
141 virtual bool eventTest(QEvent
*event
)
144 if (!QKeyEventTransition::eventTest(event
))
146 //We have enough bomb?
147 if (boat
->bombsLaunched() < MAX_BOMB
)
157 //This state is describing when the boat is moving right
158 class MoveStateRight
: public QState
161 MoveStateRight(Boat
*boat
,QState
*parent
= 0) : QState(parent
)
166 void onEntry(QEvent
*)
168 boat
->setCurrentDirection(Boat::Right
);
169 boat
->updateBoatMovement();
175 //This state is describing when the boat is moving left
176 class MoveStateLeft
: public QState
179 MoveStateLeft(Boat
*boat
,QState
*parent
= 0) : QState(parent
)
184 void onEntry(QEvent
*)
186 boat
->setCurrentDirection(Boat::Left
);
187 boat
->updateBoatMovement();
193 //This state is describing when the boat is in a stand by position
194 class StopState
: public QState
197 StopState(Boat
*boat
,QState
*parent
= 0) : QState(parent
)
202 void onEntry(QEvent
*)
204 boat
->setCurrentSpeed(0);
205 boat
->setCurrentDirection(Boat::None
);
206 boat
->updateBoatMovement();
212 //This state is describing the launch of the torpedo on the right
213 class LaunchStateRight
: public QState
216 LaunchStateRight(Boat
*boat
,QState
*parent
= 0) : QState(parent
)
221 void onEntry(QEvent
*)
223 Bomb
*b
= new Bomb();
224 b
->setPos(boat
->x()+boat
->size().width(),boat
->y());
225 GraphicsScene
*scene
= static_cast<GraphicsScene
*>(boat
->scene());
227 b
->launch(Bomb::Right
);
228 boat
->setBombsLaunched(boat
->bombsLaunched() + 1);
234 //This state is describing the launch of the torpedo on the left
235 class LaunchStateLeft
: public QState
238 LaunchStateLeft(Boat
*boat
,QState
*parent
= 0) : QState(parent
)
243 void onEntry(QEvent
*)
245 Bomb
*b
= new Bomb();
246 b
->setPos(boat
->x() - b
->size().width(), boat
->y());
247 GraphicsScene
*scene
= static_cast<GraphicsScene
*>(boat
->scene());
249 b
->launch(Bomb::Left
);
250 boat
->setBombsLaunched(boat
->bombsLaunched() + 1);