submarine.cpp Example File
demos/sub-attaq/submarine.cpp
#include "submarine.h"
#include "submarine_p.h"
#include "torpedo.h"
#include "pixmapitem.h"
#include "graphicsscene.h"
#include "animationmanager.h"
#include "qanimationstate.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QStateMachine>
#include <QtCore/QFinalState>
#include <QtCore/QSequentialAnimationGroup>
static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub)
{
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(sub);
for (int i = 1; i <= 4; ++i) {
PixmapItem *step = new PixmapItem(QString::fromLatin1("explosion/submarine/step%1").arg(i), GraphicsScene::Big, sub);
step->setZValue(6);
step->setOpacity(0);
QPropertyAnimation *anim = new QPropertyAnimation(step, "opacity", group);
anim->setDuration(100);
anim->setEndValue(1);
}
AnimationManager::self()->registerAnimation(group);
return group;
}
SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QString("submarine"), GraphicsScene::Big),
subType(type), subName(name), subPoints(points), speed(0), direction(SubMarine::None)
{
setZValue(5);
setTransformOriginPoint(boundingRect().center());
graphicsRotation = new QGraphicsRotation(this);
graphicsRotation->setAxis(Qt::YAxis);
graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0));
QList<QGraphicsTransform *> r;
r.append(graphicsRotation);
setTransformations(r);
QStateMachine *machine = new QStateMachine(this);
QState *moving = new QState(machine);
MovementState *movement = new MovementState(this, moving);
ReturnState *rotation = new ReturnState(this, moving);
moving->setInitialState(movement);
movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving);
machine->setInitialState(moving);
QFinalState *final = new QFinalState(machine);
movement->addTransition(movement, SIGNAL(animationFinished()), rotation);
rotation->addTransition(rotation, SIGNAL(animationFinished()), movement);
QAnimationState *destroyedState = new QAnimationState(machine);
destroyedState->setAnimation(setupDestroyAnimation(this));
moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState);
destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);
connect(machine,SIGNAL(finished()),this, SIGNAL(subMarineExecutionFinished()));
machine->start();
}
int SubMarine::points() const
{
return subPoints;
}
void SubMarine::setCurrentDirection(SubMarine::Movement direction)
{
if (this->direction == direction)
return;
if (direction == SubMarine::Right && this->direction == SubMarine::None) {
graphicsRotation->setAngle(180);
}
this->direction = direction;
}
enum SubMarine::Movement SubMarine::currentDirection() const
{
return direction;
}
void SubMarine::setCurrentSpeed(int speed)
{
if (speed < 0 || speed > 3) {
qWarning("SubMarine::setCurrentSpeed : The speed is invalid");
}
this->speed = speed;
emit subMarineStateChanged();
}
int SubMarine::currentSpeed() const
{
return speed;
}
void SubMarine::launchTorpedo(int speed)
{
Torpedo * torp = new Torpedo();
GraphicsScene *scene = static_cast<GraphicsScene *>(this->scene());
scene->addItem(torp);
torp->setPos(pos());
torp->setCurrentSpeed(speed);
torp->launch();
}
void SubMarine::destroy()
{
emit subMarineDestroyed();
}
int SubMarine::type() const
{
return Type;
}
[+] Documentation Feedback