boat.cpp Example File
demos/sub-attaq/boat.cpp
#include "boat.h"
#include "boat_p.h"
#include "bomb.h"
#include "pixmapitem.h"
#include "graphicsscene.h"
#include "animationmanager.h"
#include "custompropertyanimation.h"
#include "qanimationstate.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QStateMachine>
#include <QtCore/QHistoryState>
#include <QtCore/QFinalState>
#include <QtCore/QState>
#include <QtCore/QSequentialAnimationGroup>
static QAbstractAnimation *setupDestroyAnimation(Boat *boat)
{
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(boat);
#if QT_VERSION >=0x040500
PixmapItem *step1 = new PixmapItem(QString("explosion/boat/step1"),GraphicsScene::Big, boat);
step1->setZValue(6);
PixmapItem *step2 = new PixmapItem(QString("explosion/boat/step2"),GraphicsScene::Big, boat);
step2->setZValue(6);
PixmapItem *step3 = new PixmapItem(QString("explosion/boat/step3"),GraphicsScene::Big, boat);
step3->setZValue(6);
PixmapItem *step4 = new PixmapItem(QString("explosion/boat/step4"),GraphicsScene::Big, boat);
step4->setZValue(6);
step1->setOpacity(0);
step2->setOpacity(0);
step3->setOpacity(0);
step4->setOpacity(0);
CustomPropertyAnimation *anim1 = new CustomPropertyAnimation(boat);
anim1->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim1->setDuration(100);
anim1->setEndValue(1);
CustomPropertyAnimation *anim2 = new CustomPropertyAnimation(boat);
anim2->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim2->setDuration(100);
anim2->setEndValue(1);
CustomPropertyAnimation *anim3 = new CustomPropertyAnimation(boat);
anim3->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim3->setDuration(100);
anim3->setEndValue(1);
CustomPropertyAnimation *anim4 = new CustomPropertyAnimation(boat);
anim4->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim4->setDuration(100);
anim4->setEndValue(1);
CustomPropertyAnimation *anim5 = new CustomPropertyAnimation(boat);
anim5->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim5->setDuration(100);
anim5->setEndValue(0);
CustomPropertyAnimation *anim6 = new CustomPropertyAnimation(boat);
anim6->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim6->setDuration(100);
anim6->setEndValue(0);
CustomPropertyAnimation *anim7 = new CustomPropertyAnimation(boat);
anim7->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim7->setDuration(100);
anim7->setEndValue(0);
CustomPropertyAnimation *anim8 = new CustomPropertyAnimation(boat);
anim8->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim8->setDuration(100);
anim8->setEndValue(0);
group->addAnimation(anim1);
group->addAnimation(anim2);
group->addAnimation(anim3);
group->addAnimation(anim4);
group->addAnimation(anim5);
group->addAnimation(anim6);
group->addAnimation(anim7);
group->addAnimation(anim8);
#else
QtPauseAnimation *anim = new QtPauseAnimation(group);
anim->setDuration(1);
group->addAnimation(anim);
#endif
AnimationManager::self()->registerAnimation(group);
return group;
}
Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
: QGraphicsWidget(parent,wFlags), speed(0), bombsAlreadyLaunched(0), direction(Boat::None), movementAnimation(0)
{
pixmapItem = new PixmapItem(QString("boat"),GraphicsScene::Big, this);
setZValue(4);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);
resize(pixmapItem->boundingRect().size());
movementAnimation = new QPropertyAnimation(this, "pos");
destroyAnimation = setupDestroyAnimation(this);
machine = new QStateMachine(this);
QState *moving = new QState(machine);
StopState *stopState = new StopState(this, moving);
machine->setInitialState(moving);
moving->setInitialState(stopState);
MoveStateRight *moveStateRight = new MoveStateRight(this, moving);
MoveStateLeft *moveStateLeft = new MoveStateLeft(this, moving);
LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine);
LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine);
KeyStopTransition *leftStopRight = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Left);
leftStopRight->setTargetState(stopState);
KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Left);
leftMoveRight->setTargetState(moveStateRight);
KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveRight->setTargetState(moveStateRight);
KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveStop->setTargetState(moveStateRight);
KeyStopTransition *rightStopLeft = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightStopLeft->setTargetState(stopState);
KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveLeft->setTargetState(moveStateLeft);
KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
leftMoveLeft->setTargetState(moveStateLeft);
KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
leftMoveStop->setTargetState(moveStateLeft);
moveStateRight->addTransition(leftStopRight);
moveStateRight->addTransition(leftMoveRight);
moveStateRight->addTransition(rightMoveRight);
stopState->addTransition(rightMoveStop);
moveStateLeft->addTransition(rightStopLeft);
moveStateLeft->addTransition(leftMoveLeft);
moveStateLeft->addTransition(rightMoveLeft);
stopState->addTransition(leftMoveStop);
moveStateLeft->addTransition(movementAnimation, SIGNAL(finished()), stopState);
moveStateRight->addTransition(movementAnimation, SIGNAL(finished()), stopState);
KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireLeft->setTargetState(launchStateRight);
KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireRight->setTargetState(launchStateRight);
KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireStop->setTargetState(launchStateRight);
KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireLeft->setTargetState(launchStateLeft);
KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireRight->setTargetState(launchStateLeft);
KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireMove->setTargetState(launchStateLeft);
moveStateRight->addTransition(upFireRight);
moveStateLeft->addTransition(upFireLeft);
stopState->addTransition(upFireStop);
moveStateRight->addTransition(downFireRight);
moveStateLeft->addTransition(downFireLeft);
stopState->addTransition(downFireMove);
QHistoryState *historyState = new QHistoryState(moving);
launchStateLeft->addTransition(historyState);
launchStateRight->addTransition(historyState);
QFinalState *final = new QFinalState(machine);
QAnimationState *destroyedState = new QAnimationState(machine);
destroyedState->setAnimation(destroyAnimation);
moving->addTransition(this, SIGNAL(boatDestroyed()),destroyedState);
destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);
connect(machine,SIGNAL(finished()),this, SIGNAL(boatExecutionFinished()));
}
void Boat::run()
{
AnimationManager::self()->registerAnimation(movementAnimation);
AnimationManager::self()->registerAnimation(destroyAnimation);
machine->start();
}
void Boat::stop()
{
movementAnimation->stop();
machine->stop();
}
void Boat::updateBoatMovement()
{
if (speed == 0 || direction == Boat::None) {
movementAnimation->stop();
return;
}
movementAnimation->stop();
movementAnimation->setStartValue(pos());
if (direction == Boat::Left) {
movementAnimation->setEndValue(QPointF(0,y()));
movementAnimation->setDuration(x()/speed*15);
}
else {
movementAnimation->setEndValue(QPointF(scene()->width()-size().width(),y()));
movementAnimation->setDuration((scene()->width()-size().width()-x())/speed*15);
}
movementAnimation->start();
}
void Boat::destroy()
{
movementAnimation->stop();
emit boatDestroyed();
}
int Boat::bombsLaunched() const
{
return bombsAlreadyLaunched;
}
void Boat::setBombsLaunched(int number)
{
if (number > MAX_BOMB) {
qWarning("Boat::setBombsLaunched : It impossible to launch that number of bombs");
return;
}
bombsAlreadyLaunched = number;
}
int Boat::currentSpeed() const
{
return speed;
}
void Boat::setCurrentSpeed(int speed)
{
if (speed > 3 || speed < 0) {
qWarning("Boat::setCurrentSpeed: The boat can't run on that speed");
return;
}
this->speed = speed;
}
enum Boat::Movement Boat::currentDirection() const
{
return direction;
}
void Boat::setCurrentDirection(Movement direction)
{
this->direction = direction;
}
int Boat::type() const
{
return Type;
}
| Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) |
Trademarks |
Qt 4.6.0-tp1 |