states.cpp Example File
demos/sub-attaq/states.cpp
#include "states.h"
#include "graphicsscene.h"
#include "boat.h"
#include "submarine.h"
#include "torpedo.h"
#include "animationmanager.h"
#include "progressitem.h"
#include "textinformationitem.h"
#include <QtGui/QMessageBox>
#include <QtGui/QGraphicsView>
#include <QtCore/QStateMachine>
#include <QtGui/QKeyEventTransition>
#include <QtCore/QFinalState>
PlayState::PlayState(GraphicsScene *scene, QState *parent)
: QState(parent),
scene(scene),
machine(0),
currentLevel(0),
score(0)
{
}
PlayState::~PlayState()
{
delete machine;
}
void PlayState::onEntry(QEvent *)
{
if (machine) {
machine->stop();
scene->textInformationItem->hide();
scene->clearScene();
currentLevel = 0;
score = 0;
delete machine;
}
machine = new QStateMachine;
LevelState *levelState = new LevelState(scene, this, machine);
QState *playingState = new QState(levelState);
levelState->setInitialState(playingState);
PauseState *pauseState = new PauseState(scene, levelState);
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(playingState);
playingState->addTransition(pressPplay);
pauseState->addTransition(pressPpause);
LostState *lostState = new LostState(scene, this, machine);
WinState *winState = new WinState(scene, this, machine);
levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
WinTransition *winTransition = new WinTransition(scene, this, winState);
levelState->addTransition(winTransition);
UpdateScoreState *scoreState = new UpdateScoreState(this, levelState);
UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, levelState);
scoreTransition->setTargetState(scoreState);
playingState->addTransition(scoreTransition);
scoreState->addTransition(playingState);
!!!
machine->setInitialState(levelState);
QFinalState *final = new QFinalState(machine);
CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space);
spaceTransition->setTargetState(levelState);
winState->addTransition(spaceTransition);
lostState->addTransition(lostState, SIGNAL(finished()), final);
machine->start();
}
LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
{
}
void LevelState::onEntry(QEvent *)
{
initializeLevel();
}
void LevelState::initializeLevel()
{
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(game->currentLevel);
for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) {
QPair<int,int> subContent = currentLevelDescription.submarines.at(i);
GraphicsScene::SubmarineDescription submarineDesc = scene->submarinesData.at(subContent.first);
for (int j = 0; j < subContent.second; ++j ) {
SubMarine *sub = new SubMarine(submarineDesc.type, submarineDesc.name, submarineDesc.points);
scene->addItem(sub);
int random = (qrand() % 15 + 1);
qreal x = random == 13 || random == 5 ? 0 : scene->width() - sub->size().width();
qreal y = scene->height() -(qrand() % 150 + 1) - sub->size().height();
sub->setPos(x,y);
sub->setCurrentDirection(x == 0 ? SubMarine::Right : SubMarine::Left);
sub->setCurrentSpeed(qrand() % 3 + 1);
}
}
}
PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent),scene(scene)
{
}
void PauseState::onEntry(QEvent *)
{
AnimationManager::self()->pauseAll();
scene->boat->setEnabled(false);
}
void PauseState::onExit(QEvent *)
{
AnimationManager::self()->resumeAll();
scene->boat->setEnabled(true);
scene->boat->setFocus();
}
LostState::LostState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
{
}
void LostState::onEntry(QEvent *)
{
QString message = QString("You lose on level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score);
game->currentLevel = 0;
game->score = 0;
scene->clearScene();
scene->textInformationItem->setMessage(message);
scene->textInformationItem->show();
}
void LostState::onExit(QEvent *)
{
scene->textInformationItem->hide();
}
WinState::WinState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game)
{
}
void WinState::onEntry(QEvent *)
{
scene->clearScene();
QString message;
if (scene->levelsData.size() - 1 != game->currentLevel) {
message = QString("You win the level %1. Your score is %2.\nPress Space to continue.").arg(game->currentLevel+1).arg(game->score);
game->currentLevel++;
} else {
message = QString("You finish the game on level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score);
game->currentLevel = 0;
game->score = 0;
}
scene->textInformationItem->setMessage(message);
scene->textInformationItem->show();
}
void WinState::onExit(QEvent *)
{
scene->textInformationItem->hide();
}
UpdateScoreState::UpdateScoreState(PlayState *g, QState *parent) : QState(parent), game(g)
{
}
UpdateScoreTransition::UpdateScoreTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
: QSignalTransition(scene,SIGNAL(subMarineDestroyed(int))),
game(game), scene(scene)
{
setTargetState(target);
}
bool UpdateScoreTransition::eventTest(QEvent *event)
{
if (!QSignalTransition::eventTest(event))
return false;
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event);
game->score += se->arguments().at(0).toInt();
scene->progressItem->setScore(game->score);
return true;
}
WinTransition::WinTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target)
: QSignalTransition(scene,SIGNAL(allSubMarineDestroyed(int))),
game(game), scene(scene)
{
setTargetState(target);
}
bool WinTransition::eventTest(QEvent *event)
{
if (!QSignalTransition::eventTest(event))
return false;
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event);
game->score += se->arguments().at(0).toInt();
scene->progressItem->setScore(game->score);
return true;
}
CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key)
: QKeyEventTransition(widget, type, key),
game(game)
{
}
bool CustomSpaceTransition::eventTest(QEvent *event)
{
if (!QKeyEventTransition::eventTest(event))
return false;
return (game->currentLevel != 0);
}