bomb.cpp Example File
demos/sub-attaq/bomb.cpp
#include "bomb.h"
#include "submarine.h"
#include "pixmapitem.h"
#include "animationmanager.h"
#include "qanimationstate.h"
#include <QtCore/QSequentialAnimationGroup>
#include <QtCore/QPropertyAnimation>
#include <QtCore/QStateMachine>
#include <QtCore/QFinalState>
Bomb::Bomb() : PixmapItem(QString("bomb"), GraphicsScene::Big)
{
setZValue(2);
}
void Bomb::launch(Bomb::Direction direction)
{
QSequentialAnimationGroup *launchAnimation = new QSequentialAnimationGroup;
AnimationManager::self()->registerAnimation(launchAnimation);
qreal delta = direction == Right ? 20 : - 20;
QPropertyAnimation *anim = new QPropertyAnimation(this, "pos");
anim->setEndValue(QPointF(x() + delta,y() - 20));
anim->setDuration(150);
launchAnimation->addAnimation(anim);
anim = new QPropertyAnimation(this, "pos");
anim->setEndValue(QPointF(x() + delta*2, y() ));
anim->setDuration(150);
launchAnimation->addAnimation(anim);
anim = new QPropertyAnimation(this, "pos");
anim->setEndValue(QPointF(x() + delta*2,scene()->height()));
anim->setDuration(y()/2*60);
launchAnimation->addAnimation(anim);
connect(anim,SIGNAL(valueChanged(QVariant)),this,SLOT(onAnimationLaunchValueChanged(QVariant)));
connect(this, SIGNAL(bombExploded()), launchAnimation, SLOT(stop()));
QStateMachine *machine = new QStateMachine(this);
QAnimationState *launched = new QAnimationState(machine);
launched->setAnimation(launchAnimation);
QFinalState *final = new QFinalState(machine);
machine->setInitialState(launched);
launched->addTransition(this, SIGNAL(bombExploded()),final);
launched->addTransition(launched, SIGNAL(animationFinished()), final);
connect(machine,SIGNAL(finished()),this, SIGNAL(bombExecutionFinished()));
machine->start();
}
void Bomb::onAnimationLaunchValueChanged(const QVariant &)
{
foreach (QGraphicsItem * item , collidingItems(Qt::IntersectsItemBoundingRect)) {
if (item->type() == SubMarine::Type) {
SubMarine *s = static_cast<SubMarine *>(item);
destroy();
s->destroy();
}
}
}
void Bomb::destroy()
{
emit bombExploded();
}
[+] Documentation Feedback