summaryrefslogtreecommitdiffstats
path: root/examples/animation
diff options
context:
space:
mode:
authorAlexis Menard <alexis.menard@nokia.com>2009-04-24 15:04:19 (GMT)
committerAlexis Menard <alexis.menard@nokia.com>2009-04-24 15:04:19 (GMT)
commita8cce89940e42fb05efef049122286652bae61e3 (patch)
tree071e9e59bf3871d4261487bd34e3b205654a4fdf /examples/animation
parentdfe6974c1e7103ad46fa1162d7c9fd6a426894b7 (diff)
downloadQt-a8cce89940e42fb05efef049122286652bae61e3.zip
Qt-a8cce89940e42fb05efef049122286652bae61e3.tar.gz
Qt-a8cce89940e42fb05efef049122286652bae61e3.tar.bz2
Add an OSD to display game progress in the scene
Diffstat (limited to 'examples/animation')
-rw-r--r--examples/animation/sub-attaq/boat.cpp79
-rw-r--r--examples/animation/sub-attaq/boat.h6
-rw-r--r--examples/animation/sub-attaq/boat_p.h6
-rw-r--r--examples/animation/sub-attaq/graphicsscene.cpp22
-rw-r--r--examples/animation/sub-attaq/graphicsscene.h5
-rw-r--r--examples/animation/sub-attaq/progressitem.cpp67
-rw-r--r--examples/animation/sub-attaq/progressitem.h61
-rw-r--r--examples/animation/sub-attaq/states.cpp89
-rw-r--r--examples/animation/sub-attaq/states.h31
-rw-r--r--examples/animation/sub-attaq/sub-attaq.pro11
-rw-r--r--examples/animation/sub-attaq/submarine.cpp8
11 files changed, 320 insertions, 65 deletions
diff --git a/examples/animation/sub-attaq/boat.cpp b/examples/animation/sub-attaq/boat.cpp
index 5721485..5b270c0 100644
--- a/examples/animation/sub-attaq/boat.cpp
+++ b/examples/animation/sub-attaq/boat.cpp
@@ -99,10 +99,30 @@ static QAbstractAnimation *setupDestroyAnimation(Boat *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
// work around for a bug where we don't transition if the duration is zero.
QtPauseAnimation *anim = new QtPauseAnimation(group);
@@ -126,37 +146,39 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
//The movement animation used to animate the boat
movementAnimation = new QPropertyAnimation(this, "pos");
- AnimationManager::self()->registerAnimation(movementAnimation);
+
+ //The movement animation used to animate the boat
+ destroyAnimation = setupDestroyAnimation(this);
//We setup the state machien of the boat
- QStateMachine *machine = new QStateMachine(this);
+ machine = new QStateMachine(this);
QState *moving = new QState(machine->rootState());
- StopState *stopState = new StopState(this,moving);
+ 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->rootState());
- LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this,machine->rootState());
+ MoveStateRight *moveStateRight = new MoveStateRight(this, moving);
+ MoveStateLeft *moveStateLeft = new MoveStateLeft(this, moving);
+ LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine->rootState());
+ LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine->rootState());
//then setup the transitions for the rightMove state
- KeyStopTransition *leftStopRight = new KeyStopTransition(this,QEvent::KeyPress,Qt::Key_Left);
+ KeyStopTransition *leftStopRight = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Left);
leftStopRight->setTargetState(stopState);
- KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left);
+ KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Left);
leftMoveRight->setTargetState(moveStateRight);
- KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right);
+ KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveRight->setTargetState(moveStateRight);
- KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right);
+ KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveStop->setTargetState(moveStateRight);
//then setup the transitions for the leftMove state
- KeyStopTransition *rightStopLeft = new KeyStopTransition(this,QEvent::KeyPress,Qt::Key_Right);
+ KeyStopTransition *rightStopLeft = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightStopLeft->setTargetState(stopState);
- KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right);
+ KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right);
rightMoveLeft->setTargetState(moveStateLeft);
- KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left);
+ KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
leftMoveLeft->setTargetState(moveStateLeft);
- KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left);
+ KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left);
leftMoveStop->setTargetState(moveStateLeft);
//We set up the right move state
@@ -176,17 +198,17 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
moveStateRight->addTransition(movementAnimation, SIGNAL(finished()), stopState);
//We set up the keys for dropping bombs
- KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up);
+ KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireLeft->setTargetState(launchStateRight);
- KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up);
+ KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireRight->setTargetState(launchStateRight);
- KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up);
+ KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up);
upFireStop->setTargetState(launchStateRight);
- KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down);
+ KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireLeft->setTargetState(launchStateLeft);
- KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down);
+ KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireRight->setTargetState(launchStateLeft);
- KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down);
+ KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down);
downFireMove->setTargetState(launchStateLeft);
//We set up transitions for fire up
@@ -208,7 +230,7 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
//This state play the destroyed animation
QAnimationState *destroyedState = new QAnimationState(machine->rootState());
- destroyedState->setAnimation(setupDestroyAnimation(this));
+ destroyedState->setAnimation(destroyAnimation);
//Play a nice animation when the boat is destroyed
moving->addTransition(this, SIGNAL(boatDestroyed()),destroyedState);
@@ -219,9 +241,22 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
//The machine has finished to be executed, then the boat is dead
connect(machine,SIGNAL(finished()),this, SIGNAL(boatExecutionFinished()));
+}
+
+void Boat::run()
+{
+ //We register animations
+ 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) {
diff --git a/examples/animation/sub-attaq/boat.h b/examples/animation/sub-attaq/boat.h
index b8d5772..44de367 100644
--- a/examples/animation/sub-attaq/boat.h
+++ b/examples/animation/sub-attaq/boat.h
@@ -57,6 +57,8 @@
class PixmapItem;
class Bomb;
class QVariantAnimation;
+class QAbstractAnimation;
+class QStateMachine;
class Boat : public QGraphicsWidget
{
@@ -71,6 +73,8 @@ public:
enum { Type = UserType + 2 };
Boat(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
void destroy();
+ void run();
+ void stop();
int bombsLaunched() const;
void setBombsLaunched(int number);
@@ -94,6 +98,8 @@ private:
int bombsAlreadyLaunched;
Movement direction;
QVariantAnimation *movementAnimation;
+ QAbstractAnimation *destroyAnimation;
+ QStateMachine *machine;
PixmapItem *pixmapItem;
};
diff --git a/examples/animation/sub-attaq/boat_p.h b/examples/animation/sub-attaq/boat_p.h
index 8dacaba..3c466b3 100644
--- a/examples/animation/sub-attaq/boat_p.h
+++ b/examples/animation/sub-attaq/boat_p.h
@@ -61,7 +61,7 @@ class KeyStopTransition : public QKeyEventTransition
{
public:
KeyStopTransition(Boat *boat, QEvent::Type type, int key)
- : QKeyEventTransition(boat,type, key)
+ : QKeyEventTransition(boat, type, key)
{
this->boat = boat;
this->key = key;
@@ -90,7 +90,7 @@ private:
{
public:
KeyMoveTransition(Boat *boat, QEvent::Type type, int key)
- : QKeyEventTransition(boat,type, key)
+ : QKeyEventTransition(boat, type, key)
{
this->boat = boat;
this->key = key;
@@ -131,7 +131,7 @@ private:
{
public:
KeyLaunchTransition(Boat *boat, QEvent::Type type, int key)
- : QKeyEventTransition(boat,type, key)
+ : QKeyEventTransition(boat, type, key)
{
this->boat = boat;
this->key = key;
diff --git a/examples/animation/sub-attaq/graphicsscene.cpp b/examples/animation/sub-attaq/graphicsscene.cpp
index 5dcc034..a66925c 100644
--- a/examples/animation/sub-attaq/graphicsscene.cpp
+++ b/examples/animation/sub-attaq/graphicsscene.cpp
@@ -50,6 +50,7 @@
#include "custompropertyanimation.h"
#include "animationmanager.h"
#include "qanimationstate.h"
+#include "progressitem.h"
//Qt
#if defined(QT_EXPERIMENTAL_SOLUTION)
@@ -117,6 +118,15 @@ GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode)
surfaceItem->setPos(0,sealLevel() - surfaceItem->boundingRect().height()/2);
addItem(surfaceItem);
+ //The item that display score and level
+ progressItem = new ProgressItem(backgroundItem);
+
+ //We create the boat
+ boat = new Boat();
+ addItem(boat);
+ boat->setPos(this->width()/2, sealLevel() - boat->size().height());
+ boat->hide();
+
//parse the xml that contain all data of the game
QXmlStreamReader reader;
QFile file(QDir::currentPath() + "/data.xml");
@@ -335,17 +345,17 @@ void GraphicsScene::clearScene()
{
foreach (SubMarine *sub,submarines) {
sub->destroy();
- delete sub;
+ sub->deleteLater();
}
foreach (Torpedo *torpedo,torpedos) {
torpedo->destroy();
- delete torpedo;
+ torpedo->deleteLater();
}
foreach (Bomb *bomb,bombs) {
bomb->destroy();
- delete bomb;
+ bomb->deleteLater();
}
submarines.clear();
@@ -354,10 +364,8 @@ void GraphicsScene::clearScene()
AnimationManager::self()->unregisterAllAnimations();
- if (boat) {
- delete boat;
- boat = 0;
- }
+ boat->stop();
+ boat->hide();
}
QGraphicsPixmapItem *GraphicsScene::addWelcomeItem(const QPixmap &pm)
diff --git a/examples/animation/sub-attaq/graphicsscene.h b/examples/animation/sub-attaq/graphicsscene.h
index 875f59f..bc9734c 100644
--- a/examples/animation/sub-attaq/graphicsscene.h
+++ b/examples/animation/sub-attaq/graphicsscene.h
@@ -58,6 +58,7 @@ class SubMarine;
class Torpedo;
class Bomb;
class PixmapItem;
+class ProgressItem;
class QAction;
class GraphicsScene : public QGraphicsScene
@@ -109,6 +110,7 @@ private slots:
private:
Mode mode;
PixmapItem *backgroundItem;
+ ProgressItem *progressItem;
QAction * newAction;
QAction * quitAction;
Boat *boat;
@@ -121,8 +123,11 @@ private:
friend class PauseState;
friend class PlayState;
+ friend class LevelState;
friend class LostState;
friend class WinState;
+ friend class WinTransition;
+ friend class UpdateScoreTransition;
};
#endif //__GRAPHICSSCENE__H__
diff --git a/examples/animation/sub-attaq/progressitem.cpp b/examples/animation/sub-attaq/progressitem.cpp
new file mode 100644
index 0000000..59ccb9a
--- /dev/null
+++ b/examples/animation/sub-attaq/progressitem.cpp
@@ -0,0 +1,67 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "progressitem.h"
+#include "pixmapitem.h"
+
+ProgressItem::ProgressItem (QGraphicsItem * parent)
+ : QGraphicsTextItem(parent), currentLevel(1), currentScore(0)
+{
+ setFont(QFont("Comic Sans MS"));
+ setPos(parentItem()->boundingRect().topRight() - QPointF(180, -5));
+}
+
+void ProgressItem::setLevel(int level)
+{
+ currentLevel = level;
+ updateProgress();
+}
+
+void ProgressItem::setScore(int score)
+{
+ currentScore = score;
+ updateProgress();
+}
+
+void ProgressItem::updateProgress()
+{
+ setHtml(QString("Level : %1 Score : %2").arg(currentLevel).arg(currentScore));
+}
diff --git a/examples/animation/sub-attaq/progressitem.h b/examples/animation/sub-attaq/progressitem.h
new file mode 100644
index 0000000..006cc6d
--- /dev/null
+++ b/examples/animation/sub-attaq/progressitem.h
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef PROGRESSITEM_H
+#define PROGRESSITEM_H
+
+//Qt
+#include <QGraphicsTextItem>
+
+class ProgressItem : public QGraphicsTextItem
+{
+public:
+ ProgressItem(QGraphicsItem * parent = 0);
+ void setLevel(int level);
+ void setScore(int score);
+
+private:
+ void updateProgress();
+ int currentLevel;
+ int currentScore;
+};
+
+#endif // PROGRESSITEM_H
diff --git a/examples/animation/sub-attaq/states.cpp b/examples/animation/sub-attaq/states.cpp
index f476f55..5dce445 100644
--- a/examples/animation/sub-attaq/states.cpp
+++ b/examples/animation/sub-attaq/states.cpp
@@ -46,6 +46,7 @@
#include "submarine.h"
#include "torpedo.h"
#include "animationmanager.h"
+#include "progressitem.h"
//Qt
#include <QMessageBox>
@@ -81,27 +82,31 @@ void PlayState::onEntry()
if (machine) {
machine->stop();
scene->clearScene();
+ currentLevel = 0;
+ score = 0;
delete machine;
}
machine = new QStateMachine(this);
//This state is when player is playing
- QState *playState = new QState(machine->rootState());
+ LevelState *levelState = new LevelState(scene, this, machine->rootState());
- initializeLevel();
+ //This state is when the player is actually playing but the game is not paused
+ QState *playingState = new QState(levelState);
+ levelState->setInitialState(playingState);
//This state is when the game is paused
- PauseState *pauseState = new PauseState(scene, machine->rootState());
+ PauseState *pauseState = new PauseState(scene, levelState);
//We have one view, it receive the key press event
QKeyEventTransition *pressPplay = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P);
pressPplay->setTargetState(pauseState);
QKeyEventTransition *pressPpause = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P);
- pressPpause->setTargetState(playState);
+ pressPpause->setTargetState(playingState);
//Pause "P" is triggered, the player pause the game
- playState->addTransition(pressPplay);
+ playingState->addTransition(pressPplay);
//To get back playing when the game has been paused
pauseState->addTransition(pressPpause);
@@ -113,34 +118,37 @@ void PlayState::onEntry()
WinState *winState = new WinState(scene, this, machine->rootState());
//The boat has been destroyed then the game is finished
- playState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
+ levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
//This transition check if we won or not
WinTransition *winTransition = new WinTransition(scene, this, winState);
//The boat has been destroyed then the game is finished
- playState->addTransition(winTransition);
+ levelState->addTransition(winTransition);
//This state is an animation when the score changed
- UpdateScoreState *scoreState = new UpdateScoreState(this, machine->rootState());
+ UpdateScoreState *scoreState = new UpdateScoreState(this, levelState);
//This transition update the score when a submarine die
- UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, scoreState);
+ UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, levelState);
+ scoreTransition->setTargetState(scoreState);
//The boat has been destroyed then the game is finished
- playState->addTransition(scoreTransition);
+ playingState->addTransition(scoreTransition);
//We go back to play state
- scoreState->addTransition(playState);
+ scoreState->addTransition(playingState);
//We start playing!!!
- machine->setInitialState(playState);
+ machine->setInitialState(levelState);
//Final state
QFinalState *final = new QFinalState(machine->rootState());
- //We win we should reach the final state
- winState->addFinishedTransition(final);
+ //This transition is triggered when the player press space after completing a level
+ CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space);
+ spaceTransition->setTargetState(levelState);
+ winState->addTransition(spaceTransition);
//We lost we should reach the final state
lostState->addFinishedTransition(final);
@@ -148,14 +156,29 @@ void PlayState::onEntry()
machine->start();
}
-void PlayState::initializeLevel()
+LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
{
- scene->boat = new Boat();
- scene->addItem(scene->boat);
- scene->setFocusItem(scene->boat,Qt::OtherFocusReason);
+}
+void LevelState::onEntry()
+{
+ initializeLevel();
+}
+
+void LevelState::initializeLevel()
+{
+ //we re-init the boat
scene->boat->setPos(scene->width()/2, scene->sealLevel() - scene->boat->size().height());
+ scene->boat->setCurrentSpeed(0);
+ scene->boat->setCurrentDirection(Boat::None);
+ scene->boat->setBombsLaunched(0);
+ scene->boat->show();
+ scene->setFocusItem(scene->boat,Qt::OtherFocusReason);
+ scene->boat->run();
+
+ scene->progressItem->setScore(game->score);
+ scene->progressItem->setLevel(game->currentLevel + 1);
- GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(currentLevel);
+ GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel);
for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) {
@@ -226,7 +249,7 @@ void WinState::onEntry()
QString message;
if (scene->levelsData.size() - 1 != game->currentLevel) {
- message = QString("You win the level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score);
+ message = QString("You win the level %1. Your score is %2.\nPress Space to continue after closing this dialog.").arg(game->currentLevel+1).arg(game->score);
//We increment the level number
game->currentLevel++;
} else {
@@ -248,14 +271,13 @@ UpdateScoreState::UpdateScoreState(PlayState *game, QState *parent) : QState(par
}
void UpdateScoreState::onEntry()
{
- //### Make a nice anim to update the score in the scene
QState::onEntry();
}
/** Win transition */
UpdateScoreTransition::UpdateScoreTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
: QSignalTransition(scene,SIGNAL(subMarineDestroyed(int)), QList<QAbstractState*>() << target),
- game(game)
+ game(game), scene(scene)
{
}
@@ -266,6 +288,7 @@ bool UpdateScoreTransition::eventTest(QEvent *event) const
else {
QSignalEvent *se = static_cast<QSignalEvent*>(event);
game->score += se->arguments().at(0).toInt();
+ scene->progressItem->setScore(game->score);
return true;
}
}
@@ -273,7 +296,7 @@ bool UpdateScoreTransition::eventTest(QEvent *event) const
/** Win transition */
WinTransition::WinTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
: QSignalTransition(scene,SIGNAL(allSubMarineDestroyed(int)), QList<QAbstractState*>() << target),
- game(game)
+ game(game), scene(scene)
{
}
@@ -284,6 +307,26 @@ bool WinTransition::eventTest(QEvent *event) const
else {
QSignalEvent *se = static_cast<QSignalEvent*>(event);
game->score += se->arguments().at(0).toInt();
+ scene->progressItem->setScore(game->score);
return true;
}
}
+
+/** Space transition */
+CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key)
+ : QKeyEventTransition(widget, type, key),
+ game(game)
+{
+}
+
+bool CustomSpaceTransition::eventTest(QEvent *event) const
+{
+ Q_UNUSED(event);
+ if (!QKeyEventTransition::eventTest(event))
+ return false;
+ if (game->currentLevel != 0)
+ return true;
+ else
+ return false;
+
+}
diff --git a/examples/animation/sub-attaq/states.h b/examples/animation/sub-attaq/states.h
index 52d4ffa..8dbdf86 100644
--- a/examples/animation/sub-attaq/states.h
+++ b/examples/animation/sub-attaq/states.h
@@ -47,10 +47,12 @@
#include "qstate.h"
#include "qsignaltransition.h"
#include "qpropertyanimation.h"
+#include "qkeyeventtransition.h"
#else
#include <QState>
#include <QSignalTransition>
#include <QPropertyAnimation>
+# include <QKeyEventTransition>
#endif
#include <QSet>
@@ -64,7 +66,6 @@ class PlayState : public QState
public:
PlayState(GraphicsScene *scene, QState *parent = 0);
~PlayState();
- void initializeLevel();
protected:
void onEntry();
@@ -79,8 +80,22 @@ private :
friend class UpdateScoreState;
friend class UpdateScoreTransition;
friend class WinTransition;
+ friend class CustomSpaceTransition;
friend class WinState;
friend class LostState;
+ friend class LevelState;
+};
+
+class LevelState : public QState
+{
+public:
+ LevelState(GraphicsScene *scene, PlayState *game, QState *parent = 0);
+protected:
+ void onEntry();
+private :
+ void initializeLevel();
+ GraphicsScene *scene;
+ PlayState *game;
};
class PauseState : public QState
@@ -140,6 +155,7 @@ protected:
virtual bool eventTest(QEvent *event) const;
private:
PlayState * game;
+ GraphicsScene *scene;
};
//These transtion test if we have won the game
@@ -151,6 +167,19 @@ protected:
virtual bool eventTest(QEvent *event) const;
private:
PlayState * game;
+ GraphicsScene *scene;
+};
+
+//These transtion is true if one level has been completed and the player want to continue
+ class CustomSpaceTransition : public QKeyEventTransition
+{
+public:
+ CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key);
+protected:
+ virtual bool eventTest(QEvent *event) const;
+private:
+ PlayState *game;
+ int key;
};
#endif // STATES_H
diff --git a/examples/animation/sub-attaq/sub-attaq.pro b/examples/animation/sub-attaq/sub-attaq.pro
index 1456d0e..961a9b5 100644
--- a/examples/animation/sub-attaq/sub-attaq.pro
+++ b/examples/animation/sub-attaq/sub-attaq.pro
@@ -2,10 +2,10 @@
# Automatically generated by qmake (2.01a) Thu Oct 9 10:53:30 2008
# #####################################################################
TEMPLATE = app
-TARGET =
+TARGET =
DEPENDPATH += .
INCLUDEPATH += .
-QT+= xml
+QT += xml
contains(QT_CONFIG, opengl):QT += opengl
# Input
@@ -21,7 +21,8 @@ HEADERS += boat.h \
boat_p.h \
submarine_p.h \
custompropertyanimation.h \
- qanimationstate.h
+ qanimationstate.h \
+ progressitem.h
SOURCES += boat.cpp \
bomb.cpp \
main.cpp \
@@ -33,6 +34,6 @@ SOURCES += boat.cpp \
animationmanager.cpp \
states.cpp \
custompropertyanimation.cpp \
- qanimationstate.cpp
-
+ qanimationstate.cpp \
+ progressitem.cpp
RESOURCES += subattaq.qrc
diff --git a/examples/animation/sub-attaq/submarine.cpp b/examples/animation/sub-attaq/submarine.cpp
index 555617c..b8a476e 100644
--- a/examples/animation/sub-attaq/submarine.cpp
+++ b/examples/animation/sub-attaq/submarine.cpp
@@ -126,15 +126,15 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem *
QState *moving = new QState(machine->rootState());
//This state is when the boat is moving from left to right
- MovementState *movement = new MovementState(this,moving);
+ MovementState *movement = new MovementState(this, moving);
//This state is when the boat is moving from left to right
- ReturnState *rotation = new ReturnState(this,moving);
+ ReturnState *rotation = new ReturnState(this, moving);
//This is the initial state of the moving root state
moving->setInitialState(movement);
- movement->addTransition(this, SIGNAL(subMarineStateChanged()),moving);
+ movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving);
//This is the initial state of the machine
machine->setInitialState(moving);
@@ -153,7 +153,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem *
destroyedState->setAnimation(setupDestroyAnimation(this));
//Play a nice animation when the submarine is destroyed
- moving->addTransition(this, SIGNAL(subMarineDestroyed()),destroyedState);
+ moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState);
//Transition to final state when the destroyed animation is finished
destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);