summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/statemachine/rogue/window.cpp
blob: 6dfba5974e1bcc54201f4339234fd3ff2c293c24 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtWidgets>

#include "window.h"
#include "movementtransition.h"

//![0]
Window::Window()
{
    pX = 5;
    pY = 5;
//![0]

    QFont font;
    const QStringList families = QFontDatabase::families();
    if (families.contains("Monospace")) {
        font = QFont(QStringList{"Monospace"});
    }
    else {
        for (const QString &family : families) {
            if (QFontDatabase::isFixedPitch(family)) {
                font = QFont(QStringList{family});
                break;
            }
        }
    }
    font.setPointSize(12);
    setFont(font);

//![1]
    setupMap();
    buildMachine();
}
//![1]

void Window::setStatus(const QString &status)
{
    myStatus = status;
    repaint();
}

QString Window::status() const
{
    return myStatus;
}

void Window::paintEvent(QPaintEvent * /* event */)
{
    QFontMetrics metrics(font());
    QPainter painter(this);
    int fontHeight = metrics.height();
    int fontWidth = metrics.horizontalAdvance('X');
    int yPos = fontHeight;
    int xPos;

    painter.fillRect(rect(), Qt::black);
    painter.setPen(Qt::white);

    painter.drawText(QPoint(0, yPos), status());

    for (int y = 0; y < HEIGHT; ++y) {
        yPos += fontHeight;
        xPos = 0;

        for (int x = 0; x < WIDTH; ++x) {
            if (y == pY && x == pX) {
                xPos += fontWidth;
                continue;
            }

            painter.setPen(Qt::white);

            double x1 = static_cast<double>(pX);
            double y1 = static_cast<double>(pY);
            double x2 = static_cast<double>(x);
            double y2 = static_cast<double>(y);

            if (x2<x1) {
                x2+=0.5;
            } else if (x2>x1) {
                 x2-=0.5;
            }

            if (y2<y1) {
                 y2+=0.5;
            } else if (y2>y1) {
                 y2-=0.5;
            }

            double dx = x2 - x1;
            double dy = y2 - y1;

            double length = qSqrt(dx*dx+dy*dy);

            dx /= length;
            dy /= length;

            double xi = x1;
            double yi = y1;

            while (length > 0) {
                int cx = static_cast<int>(xi+0.5);
                int cy = static_cast<int>(yi+0.5);

                if (x2 == cx && y2 == cy)
                    break;

                if (!(x1==cx && y1==cy)
                    && (map[cx][cy] == '#' || (length-10) > 0)) {
                    painter.setPen(QColor(60,60,60));
                    break;
                }

                xi += dx;
                yi += dy;
                --length;
            }

            painter.drawText(QPoint(xPos, yPos), map[x][y]);
            xPos += fontWidth;
        }
    }
    painter.setPen(Qt::white);
    painter.drawText(QPoint(pX * fontWidth, (pY + 2) * fontHeight), QChar('@'));
}

QSize Window::sizeHint() const
{
    QFontMetrics metrics(font());

    return QSize(metrics.horizontalAdvance('X') * WIDTH, metrics.height() * (HEIGHT + 1));
}

//![2]
void Window::buildMachine()
{
    machine = new QStateMachine;

    QState *inputState = new QState(machine);
    inputState->assignProperty(this, "status", "Move the rogue with 2, 4, 6, and 8");

    MovementTransition *transition = new MovementTransition(this);
    inputState->addTransition(transition);
//![2]

//![3]
    QState *quitState = new QState(machine);
    quitState->assignProperty(this, "status", "Really quit(y/n)?");

    QKeyEventTransition *yesTransition = new
        QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Y);
    yesTransition->setTargetState(new QFinalState(machine));
    quitState->addTransition(yesTransition);

    QKeyEventTransition *noTransition =
        new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_N);
    noTransition->setTargetState(inputState);
    quitState->addTransition(noTransition);
//![3]

//![4]
    QKeyEventTransition *quitTransition =
        new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Q);
    quitTransition->setTargetState(quitState);
    inputState->addTransition(quitTransition);
//![4]

//![5]
    machine->setInitialState(inputState);

    connect(machine, &QStateMachine::finished,
            qApp, &QApplication::quit);

    machine->start();
}
//![5]

void Window::movePlayer(Direction direction)
{
    switch (direction) {
        case Left:
            if (map[pX - 1][pY] != '#')
                --pX;
            break;
        case Right:
            if (map[pX + 1][pY] != '#')
                ++pX;
            break;
        case Up:
            if (map[pX][pY - 1] != '#')
                --pY;
            break;
        case Down:
            if (map[pX][pY + 1] != '#')
                ++pY;
            break;
    }
    repaint();
}

void Window::setupMap()
{
    for (int x = 0; x < WIDTH; ++x)
        for (int y = 0; y < HEIGHT; ++y) {
        if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1 || QRandomGenerator::global()->bounded(40) == 0)
            map[x][y] = '#';
        else
            map[x][y] = '.';
    }
}