summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/tankgame/mainwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/statemachine/tankgame/mainwindow.cpp')
-rw-r--r--examples/statemachine/tankgame/mainwindow.cpp118
1 files changed, 96 insertions, 22 deletions
diff --git a/examples/statemachine/tankgame/mainwindow.cpp b/examples/statemachine/tankgame/mainwindow.cpp
index 870bc9c..46e0db3 100644
--- a/examples/statemachine/tankgame/mainwindow.cpp
+++ b/examples/statemachine/tankgame/mainwindow.cpp
@@ -1,7 +1,49 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples 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 "mainwindow.h"
#include "tankitem.h"
#include "rocketitem.h"
#include "plugin.h"
+#include "gameovertransition.h"
#include <QStateMachine>
#include <QGraphicsView>
@@ -53,7 +95,7 @@ void MainWindow::init()
{
TankItem *item = new TankItem(this);
- item->setPos(m_scene->sceneRect().topLeft() + QPointF(15.0, 15.0));
+ item->setPos(m_scene->sceneRect().topLeft() + QPointF(30.0, 30.0));
item->setDirection(45.0);
item->setColor(Qt::red);
@@ -63,7 +105,7 @@ void MainWindow::init()
{
TankItem *item = new TankItem(this);
- item->setPos(m_scene->sceneRect().topRight() + QPointF(-15.0, 15.0));
+ item->setPos(m_scene->sceneRect().topRight() + QPointF(-30.0, 30.0));
item->setDirection(135.0);
item->setColor(Qt::green);
@@ -73,7 +115,7 @@ void MainWindow::init()
{
TankItem *item = new TankItem(this);
- item->setPos(m_scene->sceneRect().bottomRight() + QPointF(-15.0, -15.0));
+ item->setPos(m_scene->sceneRect().bottomRight() + QPointF(-30.0, -30.0));
item->setDirection(225.0);
item->setColor(Qt::blue);
@@ -83,7 +125,7 @@ void MainWindow::init()
{
TankItem *item = new TankItem(this);
- item->setPos(m_scene->sceneRect().bottomLeft() + QPointF(15.0, -15.0));
+ item->setPos(m_scene->sceneRect().bottomLeft() + QPointF(30.0, -30.0));
item->setDirection(315.0);
item->setColor(Qt::yellow);
@@ -96,7 +138,7 @@ void MainWindow::init()
addWall(QRectF(centerOfMap - QPointF(-50.0, -60.0), centerOfMap - QPointF(50.0, -50.0)));
addWall(QRectF(centerOfMap + QPointF(-50.0, -50.0), centerOfMap + QPointF(-40.0, 50.0)));
addWall(QRectF(centerOfMap - QPointF(-50.0, -50.0), centerOfMap - QPointF(-40.0, 50.0)));
-
+
addWall(QRectF(sceneRect.topLeft() + QPointF(sceneRect.width() / 2.0 - 5.0, -10.0),
sceneRect.topLeft() + QPointF(sceneRect.width() / 2.0 + 5.0, 100.0)));
addWall(QRectF(sceneRect.bottomLeft() + QPointF(sceneRect.width() / 2.0 - 5.0, 10.0),
@@ -107,31 +149,33 @@ void MainWindow::init()
sceneRect.topRight() + QPointF(-100.0, sceneRect.height() / 2.0 + 5.0)));
- QAction *addTankAction = menuBar()->addAction("&Add tank");
- QAction *runGameAction = menuBar()->addAction("&Run game");
+ QAction *addTankAction = menuBar()->addAction("&Add tank");
+ QAction *runGameAction = menuBar()->addAction("&Run game");
runGameAction->setObjectName("runGameAction");
- QAction *stopGameAction = menuBar()->addAction("&Stop game");
+ QAction *stopGameAction = menuBar()->addAction("&Stop game");
menuBar()->addSeparator();
QAction *quitAction = menuBar()->addAction("&Quit");
-
+
connect(addTankAction, SIGNAL(triggered()), this, SLOT(addTank()));
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
-
- m_machine = new QStateMachine(this);
- QState *stoppedState = new QState(m_machine->rootState());
+
+ m_machine = new QStateMachine(this);
+ QState *stoppedState = new QState(m_machine->rootState());
stoppedState->setObjectName("stoppedState");
stoppedState->assignProperty(runGameAction, "enabled", true);
stoppedState->assignProperty(stopGameAction, "enabled", false);
stoppedState->assignProperty(this, "started", false);
m_machine->setInitialState(stoppedState);
+//! [5]
QState *spawnsAvailable = new QState(stoppedState);
- spawnsAvailable->setObjectName("spawnsAvailable");
spawnsAvailable->assignProperty(addTankAction, "enabled", true);
QState *noSpawnsAvailable = new QState(stoppedState);
- noSpawnsAvailable->setObjectName("noSpawnsAvailable");
noSpawnsAvailable->assignProperty(addTankAction, "enabled", false);
+//! [5]
+ spawnsAvailable->setObjectName("spawnsAvailable");
+ noSpawnsAvailable->setObjectName("noSpawnsAvailable");
spawnsAvailable->addTransition(this, SIGNAL(mapFull()), noSpawnsAvailable);
@@ -139,6 +183,7 @@ void MainWindow::init()
QHistoryState *hs = new QHistoryState(stoppedState);
hs->setDefaultState(spawnsAvailable);
//! [3]
+ hs->setObjectName("hs");
stoppedState->setInitialState(hs);
@@ -149,19 +194,27 @@ void MainWindow::init()
m_runningState->assignProperty(addTankAction, "enabled", false);
m_runningState->assignProperty(runGameAction, "enabled", false);
m_runningState->assignProperty(stopGameAction, "enabled", true);
-
+
+ QState *gameOverState = new QState(m_machine->rootState());
+ gameOverState->setObjectName("gameOverState");
+ gameOverState->assignProperty(stopGameAction, "enabled", false);
+ connect(gameOverState, SIGNAL(entered()), this, SLOT(gameOver()));
+
stoppedState->addTransition(runGameAction, SIGNAL(triggered()), m_runningState);
m_runningState->addTransition(stopGameAction, SIGNAL(triggered()), stoppedState);
+ m_gameOverTransition = new GameOverTransition(gameOverState);
+ m_runningState->addTransition(m_gameOverTransition);
+
QTimer *timer = new QTimer(this);
timer->setInterval(100);
connect(timer, SIGNAL(timeout()), this, SLOT(runStep()));
- connect(m_runningState, SIGNAL(entered()), timer, SLOT(start()));
+ connect(m_runningState, SIGNAL(entered()), timer, SLOT(start()));
connect(m_runningState, SIGNAL(exited()), timer, SLOT(stop()));
m_machine->start();
m_time.start();
-}
+}
void MainWindow::runStep()
{
@@ -182,13 +235,29 @@ void MainWindow::runStep()
}
}
+void MainWindow::gameOver()
+{
+ QList<QGraphicsItem *> items = m_scene->items();
+
+ TankItem *lastTankStanding = 0;
+ foreach (QGraphicsItem *item, items) {
+ if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(item)) {
+ if ((lastTankStanding = qobject_cast<TankItem *>(gameItem)) != 0)
+ break;
+ }
+ }
+
+ QMessageBox::information(this, "Game over!",
+ QString::fromLatin1("The tank played by '%1' has won!").arg(lastTankStanding->objectName()));
+}
+
void MainWindow::addRocket()
{
TankItem *tankItem = qobject_cast<TankItem *>(sender());
if (tankItem != 0) {
RocketItem *rocketItem = new RocketItem;
- QPointF s = tankItem->mapToScene(QPointF(tankItem->boundingRect().right() + 10.0,
+ QPointF s = tankItem->mapToScene(QPointF(tankItem->boundingRect().right() + 10.0,
tankItem->boundingRect().center().y()));
rocketItem->setPos(s);
rocketItem->setDirection(tankItem->direction());
@@ -236,29 +305,34 @@ void MainWindow::addTank()
bool ok;
//! [1]
- QString selectedName = QInputDialog::getItem(this, "Select a tank type", "Tank types",
+ QString selectedName = QInputDialog::getItem(this, "Select a tank type", "Tank types",
itemNames, 0, false, &ok);
//! [1]
-
+
if (ok && !selectedName.isEmpty()) {
int idx = itemNames.indexOf(selectedName);
if (Plugin *plugin = idx >= 0 ? items.at(idx) : 0) {
TankItem *tankItem = m_spawns.takeLast();
+ tankItem->setObjectName(selectedName);
+ tankItem->setToolTip(selectedName);
m_scene->addItem(tankItem);
connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket()));
if (m_spawns.isEmpty())
emit mapFull();
-
+
+ m_gameOverTransition->addTankItem(tankItem);
+
QState *region = new QState(m_runningState);
+ region->setObjectName(QString::fromLatin1("region%1").arg(m_spawns.size()));
//! [2]
QState *pluginState = plugin->create(region, tankItem);
//! [2]
region->setInitialState(pluginState);
-
// If the plugin has an error it is disabled
//! [4]
QState *errorState = new QState(region);
+ errorState->setObjectName(QString::fromLatin1("errorState%1").arg(m_spawns.size()));
errorState->assignProperty(tankItem, "enabled", false);
pluginState->setErrorState(errorState);
//! [4]