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