1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51//Own
52#include "boat.h"
53#include "boat_p.h"
54#include "bomb.h"
55#include "graphicsscene.h"
56#include "animationmanager.h"
57#include "qanimationstate.h"
58
59//Qt
60#include <QFinalState>
61#include <QHistoryState>
62#include <QPropertyAnimation>
63#include <QSequentialAnimationGroup>
64#include <QState>
65#include <QStateMachine>
66
67static QAbstractAnimation *setupDestroyAnimation(Boat *boat)
68{
69 QSequentialAnimationGroup *group = new QSequentialAnimationGroup(boat);
70 for (int i = 1; i <= 4; i++) {
71 PixmapItem *step = new PixmapItem(QString("explosion/boat/step%1").arg(a: i),GraphicsScene::Big, boat);
72 step->setZValue(6);
73 step->setOpacity(0);
74
75 //fade-in
76 QPropertyAnimation *anim = new QPropertyAnimation(step, "opacity");
77 anim->setEndValue(1);
78 anim->setDuration(100);
79 group->insertAnimation(index: i-1, animation: anim);
80
81 //and then fade-out
82 QPropertyAnimation *anim2 = new QPropertyAnimation(step, "opacity");
83 anim2->setEndValue(0);
84 anim2->setDuration(100);
85 group->addAnimation(animation: anim2);
86 }
87
88 AnimationManager::self()->registerAnimation(anim: group);
89 return group;
90}
91
92
93
94Boat::Boat()
95 : PixmapItem(QString("boat"), GraphicsScene::Big),
96 speed(0), bombsAlreadyLaunched(0), direction(Boat::None)
97{
98 setZValue(4);
99 setFlags(QGraphicsItem::ItemIsFocusable);
100
101 //The movement animation used to animate the boat
102 movementAnimation = new QPropertyAnimation(this, "pos");
103
104 //The destroy animation used to explode the boat
105 destroyAnimation = setupDestroyAnimation(this);
106
107 //We setup the state machine of the boat
108 machine = new QStateMachine(this);
109 QState *moving = new QState(machine);
110 StopState *stopState = new StopState(this, moving);
111 machine->setInitialState(moving);
112 moving->setInitialState(stopState);
113 MoveStateRight *moveStateRight = new MoveStateRight(this, moving);
114 MoveStateLeft *moveStateLeft = new MoveStateLeft(this, moving);
115 LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine);
116 LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine);
117
118 //then setup the transitions for the rightMove state
119 KeyStopTransition *leftStopRight = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Left);
120 leftStopRight->setTargetState(stopState);
121 KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Left);
122 leftMoveRight->setTargetState(moveStateRight);
123 KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
124 rightMoveRight->setTargetState(moveStateRight);
125 KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
126 rightMoveStop->setTargetState(moveStateRight);
127
128 //then setup the transitions for the leftMove state
129 KeyStopTransition *rightStopLeft = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Right);
130 rightStopLeft->setTargetState(stopState);
131 KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
132 rightMoveLeft->setTargetState(moveStateLeft);
133 KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
134 leftMoveLeft->setTargetState(moveStateLeft);
135 KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
136 leftMoveStop->setTargetState(moveStateLeft);
137
138 //We set up the right move state
139 moveStateRight->addTransition(transition: leftStopRight);
140 moveStateRight->addTransition(transition: leftMoveRight);
141 moveStateRight->addTransition(transition: rightMoveRight);
142 stopState->addTransition(transition: rightMoveStop);
143
144 //We set up the left move state
145 moveStateLeft->addTransition(transition: rightStopLeft);
146 moveStateLeft->addTransition(transition: leftMoveLeft);
147 moveStateLeft->addTransition(transition: rightMoveLeft);
148 stopState->addTransition(transition: leftMoveStop);
149
150 //The animation is finished, it means we reached the border of the screen, the boat is stopped so we move to the stop state
151 moveStateLeft->addTransition(obj: movementAnimation, signal: &QAbstractAnimation::finished, target: stopState);
152 moveStateRight->addTransition(obj: movementAnimation, signal: &QAbstractAnimation::finished, target: stopState);
153
154 //We set up the keys for dropping bombs
155 KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
156 upFireLeft->setTargetState(launchStateRight);
157 KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
158 upFireRight->setTargetState(launchStateRight);
159 KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
160 upFireStop->setTargetState(launchStateRight);
161 KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
162 downFireLeft->setTargetState(launchStateLeft);
163 KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
164 downFireRight->setTargetState(launchStateLeft);
165 KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
166 downFireMove->setTargetState(launchStateLeft);
167
168 //We set up transitions for fire up
169 moveStateRight->addTransition(transition: upFireRight);
170 moveStateLeft->addTransition(transition: upFireLeft);
171 stopState->addTransition(transition: upFireStop);
172
173 //We set up transitions for fire down
174 moveStateRight->addTransition(transition: downFireRight);
175 moveStateLeft->addTransition(transition: downFireLeft);
176 stopState->addTransition(transition: downFireMove);
177
178 //Finally the launch state should come back to its original state
179 QHistoryState *historyState = new QHistoryState(moving);
180 launchStateLeft->addTransition(target: historyState);
181 launchStateRight->addTransition(target: historyState);
182
183 QFinalState *finalState = new QFinalState(machine);
184
185 //This state play the destroyed animation
186 QAnimationState *destroyedState = new QAnimationState(machine);
187 destroyedState->setAnimation(destroyAnimation);
188
189 //Play a nice animation when the boat is destroyed
190 moving->addTransition(obj: this, signal: &Boat::boatDestroyed, target: destroyedState);
191
192 //Transition to final state when the destroyed animation is finished
193 destroyedState->addTransition(obj: destroyedState, signal: &QAnimationState::animationFinished, target: finalState);
194
195 //The machine has finished to be executed, then the boat is dead
196 connect(sender: machine, signal: &QState::finished, receiver: this, slot: &Boat::boatExecutionFinished);
197
198}
199
200void Boat::run()
201{
202 //We register animations
203 AnimationManager::self()->registerAnimation(anim: movementAnimation);
204 AnimationManager::self()->registerAnimation(anim: destroyAnimation);
205 machine->start();
206}
207
208void Boat::stop()
209{
210 movementAnimation->stop();
211 machine->stop();
212}
213
214void Boat::updateBoatMovement()
215{
216 if (speed == 0 || direction == Boat::None) {
217 movementAnimation->stop();
218 return;
219 }
220
221 movementAnimation->stop();
222
223 if (direction == Boat::Left) {
224 movementAnimation->setEndValue(QPointF(0,y()));
225 movementAnimation->setDuration(x()/speed*15);
226 }
227 else /*if (direction == Boat::Right)*/ {
228 movementAnimation->setEndValue(QPointF(scene()->width()-size().width(),y()));
229 movementAnimation->setDuration((scene()->width()-size().width()-x())/speed*15);
230 }
231 movementAnimation->start();
232}
233
234void Boat::destroy()
235{
236 movementAnimation->stop();
237 emit boatDestroyed();
238}
239
240int Boat::bombsLaunched() const
241{
242 return bombsAlreadyLaunched;
243}
244
245void Boat::setBombsLaunched(int number)
246{
247 if (number > MAX_BOMB) {
248 qWarning(msg: "Boat::setBombsLaunched : It impossible to launch that number of bombs");
249 return;
250 }
251 bombsAlreadyLaunched = number;
252}
253
254int Boat::currentSpeed() const
255{
256 return speed;
257}
258
259void Boat::setCurrentSpeed(int speed)
260{
261 if (speed > 3 || speed < 0) {
262 qWarning(msg: "Boat::setCurrentSpeed: The boat can't run on that speed");
263 return;
264 }
265 this->speed = speed;
266}
267
268enum Boat::Movement Boat::currentDirection() const
269{
270 return direction;
271}
272
273void Boat::setCurrentDirection(Movement direction)
274{
275 this->direction = direction;
276}
277
278int Boat::type() const
279{
280 return Type;
281}
282

source code of qtbase/examples/widgets/animation/sub-attaq/boat.cpp