File: gameview.cpp

package info (click to toggle)
knetwalk 4%3A25.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,396 kB
  • sloc: cpp: 1,153; xml: 204; javascript: 75; makefile: 5; sh: 2
file content (217 lines) | stat: -rw-r--r-- 7,015 bytes parent folder | download | duplicates (2)
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
217
/*
    SPDX-FileCopyrightText: 2013 Ashwin Rajeev <ashwin_rajeev@hotmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "gameview.h"

// game
#include "globals.h"
#include "settings.h"
// KDEGames
#include <KGameSound>
// KF
#include <KMessageBox>
#include <KLocalizedContext>
#include <KLocalizedString>
// Qt
#include <QGraphicsObject>
#include <QQmlContext>


GameView::GameView(QWidget *parent) :
    QQuickWidget(parent),
    grid(new AbstractGrid),
    m_provider(new KGameThemeProvider)
{
    QQmlEngine *engine = this->engine();

    auto *localizedContextObject = new KLocalizedContext(engine);
    engine->rootContext()->setContextObject(localizedContextObject);

    setResizeMode(SizeRootObjectToView);

    m_provider->discoverThemes(QStringLiteral("themes"));
    m_provider->setDeclarativeEngine(QStringLiteral("themeProvider"), engine);
    m_soundTurn = new KGameSound(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("sounds/turn.wav")), this);
    m_soundClick = new KGameSound(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("sounds/click.wav")), this);
    m_soundConnect = new KGameSound(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("sounds/connect.wav")), this);
    QString path = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("qml/main.qml"));

    setSource(QUrl::fromLocalFile(path));
    updateSettings();

    connect(rootObject(), SIGNAL(clicked(int)),this, SLOT(clicked(int)));
    connect(rootObject(), SIGNAL(rotated(int,int)), this, SLOT(rotated(int,int)));
    connect(this, SIGNAL(setSize(QVariant,QVariant)),
            rootObject(), SLOT(setBoardSize(QVariant,QVariant)));
    connect(this, SIGNAL(newCell(QVariant,QVariant)),
            rootObject(), SLOT(addCell(QVariant,QVariant)));
    connect(this, SIGNAL(setSprite(QVariant,QVariant,QVariant)),
            rootObject(), SLOT(setSprite(QVariant,QVariant,QVariant)));
    connect(this, SIGNAL(lock(QVariant)), rootObject(), SLOT(lock(QVariant)));
    connect(this, SIGNAL(gameOver(QVariant)), rootObject(), SLOT(gameOver(QVariant)));
}

GameView::~GameView()
{
    delete m_provider;
    delete grid;
}

void GameView::startNewGame(uint width, uint height, Wrapping w=NotWrapped)
{
    setSize(width, height);
    grid->initializeGrid(width, height, w);

    for(int i = 0; i < grid->cellCount(); i++)
    {
        QString code = getCableCode(grid->cellAt(i)->cables());
        QString type = QStringLiteral("none");
        if(grid->cellAt(i)->isConnected()){
            code = QLatin1String("con") + code;
        }
        if(grid->cellAt(i)->isServer()){
            type = QStringLiteral("server");
        }
        else if(grid->cellAt(i)->isTerminal()) {
            type = (grid->cellAt(i)->isConnected())? QStringLiteral("computer2"): QStringLiteral("computer1");
        }
        newCell(code, type);
    }
}

void GameView::clicked(int index)
{
    if (index >= 0) {
        rotatingCells.insert(index);
        Q_EMIT rotationStarted();
        if (Settings::playSounds()) {
            m_soundTurn->start();
        }
    }
    else if (Settings::playSounds()) { //invalid click
        m_soundClick->start();
    }
}

void GameView::rotated(int index, int angle)
{
    switch (angle) {
    case 90: case -270:
        grid->cellAt(index)->rotateClockwise();
        break;
    case -90: case 270:
        grid->cellAt(index)->rotateCounterclockwise();
        break;
    case 180: case -180:
        grid->cellAt(index)->invert();
    }
    const QList<int> changedCells = grid->updateConnections();
    bool newTerminalConnected = false;
    rotatingCells.remove(index);

    for (int i : changedCells) {
        if(grid->cellAt(i)->isTerminal()){
            newTerminalConnected = true;
        }
        updateSprite(i);
    }

    if(newTerminalConnected && Settings::playSounds())
        m_soundConnect->start();

    if(!changedCells.contains(index)) {
        updateSprite(index);
    }

    if (Settings::autolock()) {
        Q_EMIT lock(index);
    }
    if (rotatingCells.count() == 0) {
        checkCompleted();
    }
}

void GameView::updateSprite(int index)
{
    QString type = QStringLiteral("none");
    if(grid->cellAt(index)->isTerminal()){
        type = (grid->cellAt(index)->isConnected())? QStringLiteral("computer2"): QStringLiteral("computer1");
    }

    QString code = getCableCode(grid->cellAt(index)->cables());
    if(grid->cellAt(index)->isConnected()) {
        code.insert(0, QLatin1String("con"));
    }

    if (!rotatingCells.contains(index)) {
        setSprite(index, code, type);
    }
}

void GameView::checkCompleted()
{
    if (!grid->allTerminalsConnected()) {
        return;
    }
    for(int i = 0; i < grid->cellCount(); ++i) {
        if (grid->cellAt(i)->cables() != None && !grid->cellAt(i)->isConnected()) {
            KMessageBox::information(this,
                i18n("Note: to win the game all terminals "
                "<strong>and all <em>cables</em></strong> "
                "need to be connected to the server!"),
                i18n("The game is not won yet!"),
                QStringLiteral( "dontShowGameNotWonYet" ));
            return;
        }
    }
    Q_EMIT gameOver(QLatin1String("won"));
}

void GameView::solve()
{
    if (!rotatingCells.isEmpty()) {
        return;
    }
    for(int i = 0; i < grid->cellCount(); i++) {
        grid->cellAt(i)->reset();
        QString code = QLatin1String("con") + getCableCode(grid->cellAt(i)->cables());
        if (grid->cellAt(i)->isTerminal()){
            setSprite(i, code, QLatin1String("computer2"));
        }
        else {
            setSprite(i, code, QLatin1String("none"));
        }
    }
    Q_EMIT gameOver(QLatin1String("solved"));
}

void GameView::updateSettings()
{
    rootObject()->setProperty("rotateDuration", Settings::rotateDuration());
    rootObject()->setProperty("reverseButtons", Settings::reverseButtons());
}

QString GameView::getCableCode(int cables)
{
    QHash<int, QString> directionNames;
    directionNames[Left]            = QStringLiteral("0001");
    directionNames[Down]            = QStringLiteral("0010");
    directionNames[Down|Left]       = QStringLiteral("0011");
    directionNames[Right]           = QStringLiteral("0100");
    directionNames[Right|Left]      = QStringLiteral("0101");
    directionNames[Right|Down]      = QStringLiteral("0110");
    directionNames[Right|Down|Left] = QStringLiteral("0111");
    directionNames[Up]              = QStringLiteral("1000");
    directionNames[Up|Left]         = QStringLiteral("1001");
    directionNames[Up|Down]         = QStringLiteral("1010");
    directionNames[Up|Down|Left]    = QStringLiteral("1011");
    directionNames[Up|Right]        = QStringLiteral("1100");
    directionNames[Up|Right|Left]   = QStringLiteral("1101");
    directionNames[Up|Right|Down]   = QStringLiteral("1110");
    return directionNames[cables];
}

#include "moc_gameview.cpp"