source: trunk/demos/sub-attaq/submarine.cpp@ 561

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

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 6.1 KB
RevLine 
[556]1/****************************************************************************
2**
3** Copyright (C) 2009 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 "submarine.h"
44#include "submarine_p.h"
45#include "torpedo.h"
46#include "pixmapitem.h"
47#include "graphicsscene.h"
48#include "animationmanager.h"
49#include "qanimationstate.h"
50
51#include <QtCore/QPropertyAnimation>
52#include <QtCore/QStateMachine>
53#include <QtCore/QFinalState>
54#include <QtCore/QSequentialAnimationGroup>
55
56static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub)
57{
58 QSequentialAnimationGroup *group = new QSequentialAnimationGroup(sub);
59 for (int i = 1; i <= 4; ++i) {
60 PixmapItem *step = new PixmapItem(QString::fromLatin1("explosion/submarine/step%1").arg(i), GraphicsScene::Big, sub);
61 step->setZValue(6);
62 step->setOpacity(0);
63 QPropertyAnimation *anim = new QPropertyAnimation(step, "opacity", group);
64 anim->setDuration(100);
65 anim->setEndValue(1);
66 }
67 AnimationManager::self()->registerAnimation(group);
68 return group;
69}
70
71
72SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QString("submarine"), GraphicsScene::Big),
73 subType(type), subName(name), subPoints(points), speed(0), direction(SubMarine::None)
74{
75 setZValue(5);
76 setTransformOriginPoint(boundingRect().center());
77
78 graphicsRotation = new QGraphicsRotation(this);
79 graphicsRotation->setAxis(Qt::YAxis);
80 graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0));
81 QList<QGraphicsTransform *> r;
82 r.append(graphicsRotation);
83 setTransformations(r);
84
85 //We setup the state machine of the submarine
86 QStateMachine *machine = new QStateMachine(this);
87
88 //This state is when the boat is moving/rotating
89 QState *moving = new QState(machine);
90
91 //This state is when the boat is moving from left to right
92 MovementState *movement = new MovementState(this, moving);
93
94 //This state is when the boat is moving from left to right
95 ReturnState *rotation = new ReturnState(this, moving);
96
97 //This is the initial state of the moving root state
98 moving->setInitialState(movement);
99
100 movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving);
101
102 //This is the initial state of the machine
103 machine->setInitialState(moving);
104
105 //End
106 QFinalState *final = new QFinalState(machine);
107
108 //If the moving animation is finished we move to the return state
109 movement->addTransition(movement, SIGNAL(animationFinished()), rotation);
110
111 //If the return animation is finished we move to the moving state
112 rotation->addTransition(rotation, SIGNAL(animationFinished()), movement);
113
114 //This state play the destroyed animation
115 QAnimationState *destroyedState = new QAnimationState(machine);
116 destroyedState->setAnimation(setupDestroyAnimation(this));
117
118 //Play a nice animation when the submarine is destroyed
119 moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState);
120
121 //Transition to final state when the destroyed animation is finished
122 destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);
123
124 //The machine has finished to be executed, then the submarine is dead
125 connect(machine,SIGNAL(finished()),this, SIGNAL(subMarineExecutionFinished()));
126
127 machine->start();
128}
129
130int SubMarine::points() const
131{
132 return subPoints;
133}
134
135void SubMarine::setCurrentDirection(SubMarine::Movement direction)
136{
137 if (this->direction == direction)
138 return;
139 if (direction == SubMarine::Right && this->direction == SubMarine::None) {
140 graphicsRotation->setAngle(180);
141 }
142 this->direction = direction;
143}
144
145enum SubMarine::Movement SubMarine::currentDirection() const
146{
147 return direction;
148}
149
150void SubMarine::setCurrentSpeed(int speed)
151{
152 if (speed < 0 || speed > 3) {
153 qWarning("SubMarine::setCurrentSpeed : The speed is invalid");
154 }
155 this->speed = speed;
156 emit subMarineStateChanged();
157}
158
159int SubMarine::currentSpeed() const
160{
161 return speed;
162}
163
164void SubMarine::launchTorpedo(int speed)
165{
166 Torpedo * torp = new Torpedo();
167 GraphicsScene *scene = static_cast<GraphicsScene *>(this->scene());
168 scene->addItem(torp);
169 torp->setPos(pos());
170 torp->setCurrentSpeed(speed);
171 torp->launch();
172}
173
174void SubMarine::destroy()
175{
176 emit subMarineDestroyed();
177}
178
179int SubMarine::type() const
180{
181 return Type;
182}
Note: See TracBrowser for help on using the repository browser.