summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/errorstate/mainwindow.cpp
blob: 4d9b88e74734bf42e05ee028d298677429beb022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "mainwindow.h"
#include "tankitem.h"
#include "rocketitem.h"
#include "plugin.h"

#include <QStateMachine>
#include <QGraphicsView>
#include <QAction>
#include <QMenuBar>
#include <QState>
#include <QHistoryState>
#include <QTimer>
#include <QFileDialog>
#include <QPluginLoader>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), m_scene(0), m_machine(0), m_runningState(0), m_started(false)
{
    init();
}

MainWindow::~MainWindow()
{
}

void MainWindow::addWall(const QRectF &wall)
{
    QGraphicsRectItem *item = new QGraphicsRectItem;
    item->setRect(wall);
    item->setBrush(Qt::darkGreen);
    item->setPen(QPen(Qt::black, 0));

    m_scene->addItem(item);
}

void MainWindow::init()
{
    setWindowTitle("Pluggable Tank Game");

    QGraphicsView *view = new QGraphicsView(this);
    view->setRenderHints(QPainter::Antialiasing);
    setCentralWidget(view);

    m_scene = new QGraphicsScene(this);
    view->setScene(m_scene);

    QRectF sceneRect = QRectF(-200.0, -200.0, 400.0, 400.0);
    m_scene->setSceneRect(sceneRect);

    {
        TankItem *item = new TankItem(this);

        item->setPos(m_scene->sceneRect().topLeft() + QPointF(15.0, 15.0));
        item->setDirection(45.0);
        item->setColor(Qt::red);

        m_spawns.append(item);
    }

    {
        TankItem *item = new TankItem(this);

        item->setPos(m_scene->sceneRect().topRight() + QPointF(-15.0, 15.0));
        item->setDirection(135.0);
        item->setColor(Qt::green);

        m_spawns.append(item);
    }

    {
        TankItem *item = new TankItem(this);

        item->setPos(m_scene->sceneRect().bottomRight() + QPointF(-15.0, -15.0));
        item->setDirection(225.0);
        item->setColor(Qt::blue);

        m_spawns.append(item);
    }

    {
        TankItem *item = new TankItem(this);

        item->setPos(m_scene->sceneRect().bottomLeft() + QPointF(15.0, -15.0));
        item->setDirection(315.0);
        item->setColor(Qt::yellow);

        m_spawns.append(item);
    }

    QPointF centerOfMap = sceneRect.center();
    addWall(QRectF(centerOfMap + QPointF(-50.0, -60.0), centerOfMap + QPointF(50.0, -50.0)));
    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, 0.0),
                   sceneRect.topLeft() + QPointF(sceneRect.width() / 2.0 + 5.0, 100.0)));
    addWall(QRectF(sceneRect.bottomLeft() + QPointF(sceneRect.width() / 2.0 - 5.0, 0.0),
                   sceneRect.bottomLeft() + QPointF(sceneRect.width() / 2.0 + 5.0, -100.0)));
    addWall(QRectF(sceneRect.topLeft() + QPointF(0.0, sceneRect.height() / 2.0 - 5.0),
                   sceneRect.topLeft() + QPointF(100.0, sceneRect.height() / 2.0 + 5.0)));
    addWall(QRectF(sceneRect.topRight() + QPointF(0.0, sceneRect.height() / 2.0 - 5.0),
                   sceneRect.topRight() + QPointF(-100.0, sceneRect.height() / 2.0 + 5.0)));


    QAction *addTankAction = menuBar()->addAction("&Add tank");   
    QAction *runGameAction = menuBar()->addAction("&Run game");    
    runGameAction->setObjectName("runGameAction");
    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());        
    stoppedState->setObjectName("stoppedState");
    stoppedState->assignProperty(runGameAction, "enabled", true);
    stoppedState->assignProperty(stopGameAction, "enabled", false);
    stoppedState->assignProperty(this, "started", false);
    m_machine->setInitialState(stoppedState);

    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);

    spawnsAvailable->addTransition(this, SIGNAL(mapFull()), noSpawnsAvailable);

    QHistoryState *hs = new QHistoryState(stoppedState);
    hs->setDefaultState(spawnsAvailable);

    stoppedState->setInitialState(hs);

    m_runningState = new QState(QState::ParallelStates, m_machine->rootState());
    m_runningState->setObjectName("runningState");
    m_runningState->assignProperty(addTankAction, "enabled", false);
    m_runningState->assignProperty(runGameAction, "enabled", false);
    m_runningState->assignProperty(stopGameAction, "enabled", true);
        
    stoppedState->addTransition(runGameAction, SIGNAL(triggered()), m_runningState);
    m_runningState->addTransition(stopGameAction, SIGNAL(triggered()), stoppedState);

    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(exited()), timer, SLOT(stop()));

    m_machine->start();
    m_time.start();
}   

void MainWindow::runStep()
{
    if (!m_started) {
        m_time.restart();
        m_started = true;
    } else {
        int elapsed = m_time.elapsed();
        if (elapsed > 0) {
            m_time.restart();
            qreal elapsedSecs = elapsed / 1000.0;
            QList<QGraphicsItem *> items = m_scene->items();
            foreach (QGraphicsItem *item, items) {
                if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(item))
                    gameItem->idle(elapsedSecs);
            }
        }
    }
}

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, 
                                                 tankItem->boundingRect().center().y()));
        rocketItem->setPos(s);
        rocketItem->setDirection(tankItem->direction());
        m_scene->addItem(rocketItem);
    }
}

void MainWindow::addTank()
{
    Q_ASSERT(!m_spawns.isEmpty());

    QString fileName = QFileDialog::getOpenFileName(this, "Select plugin file", "plugins/", "*.dll");
    QPluginLoader loader(fileName);
    
    Plugin *plugin = qobject_cast<Plugin *>(loader.instance());
    if (plugin != 0) {
        TankItem *tankItem = m_spawns.takeLast();
        m_scene->addItem(tankItem);
        connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket()));
        if (m_spawns.isEmpty())
            emit mapFull();
        
        QState *region = new QState(m_runningState);
        QState *pluginState = plugin->create(region, tankItem);
        region->setInitialState(pluginState);

        // If the plugin has an error it is disabled
        QState *errorState = new QState(region);
        errorState->assignProperty(tankItem, "enabled", false);
        pluginState->setErrorState(errorState);
    }
}