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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
#include "mainwidget.h"
#include "actor.h"
#include "block.h"
#include "constants.h"
#include "util.h"
#include "pacman.pb.h"
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent), m_currentKey(Transmission::none), m_running(false), m_scene(NULL),
m_maxplayers(0)
{
/* create audio player */
m_ambientPlayer = new GaplessAudioPlayer(Sound::Ambient, 100, this);
Color::Color color = connectToServer();
if (color == Color::none)
{
QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode");
// TODO: quit application here or sth
return;
}
/* create our scene */
m_scene = new SceneHolder(this);
m_scene->setColor(color);
/* call updateMap after m_color ist set! */
createGui();
/* wait for the server to send the first map update (initial map)
* WARNING: this will block the gui
*/
m_socket->waitForReadyRead();
tick();
connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick()));
qDebug() << "[Connect] mycolor=" << m_scene->color();
//TODO: play intro as soon as there are enough players
//connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
//AudioPlayer::self()->play(AudioPlayer::Intro);
startGame();
}
bool MainWidget::connected()
{
return m_socket != NULL;
}
void MainWidget::createGui()
{
setFocusPolicy(Qt::StrongFocus);
/* first one is always the own score */
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
QHBoxLayout *scoreLayout = new QHBoxLayout();
for (unsigned int i = 0; i < m_maxplayers; ++i)
{
QGridLayout *playerLayout = new QGridLayout();
playerLayout->addWidget(new QLabel("Current:", this), 0, 0);
playerLayout->addWidget(new QLabel("Total:", this), 1, 0);
playerLayout->addWidget(new QLabel("0", this), 0, 1, Qt::AlignLeft);
playerLayout->addWidget(new QLabel("0", this), 1, 1, Qt::AlignLeft);
playerLayout->setColumnStretch(1, 10);
playerLayout->setSizeConstraint(QLayout::SetMinimumSize);
QGroupBox *scoreBox = new QGroupBox(QString("Player %1").arg(i + 1), this);
scoreBox->setObjectName(QString("actor%1").arg(i + 1));
scoreBox->setCheckable(true);
scoreBox->setDisabled(i >= m_maxplayers);
connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked()));
scoreBox->setLayout(playerLayout);
m_playerScoreLayouts.append(playerLayout);
if (Color::order[i] == m_scene->color())
scoreLayout->insertWidget(0, scoreBox);
else
scoreLayout->addWidget(scoreBox);
}
layout->addLayout(scoreLayout);
/* add some margin to scorebox hopefully won't resize the window */
//setMinimumWidth(scoreLayout->minimumSize().width() + 50);
QGraphicsView *window = new QGraphicsView(m_scene, this);
window->setFrameStyle(0);
window->setFixedSize(m_scene->sceneRect().size().toSize());
window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint);
window->setFocusPolicy(Qt::NoFocus);
layout->addWidget(window, 0, Qt::AlignCenter);
QFile css(":/stylesheet");
css.open(QFile::ReadOnly);
qApp->setStyleSheet(QLatin1String(css.readAll()));
/* add dummy layout at the end which gets streched when resizing */
QHBoxLayout *spacer = new QHBoxLayout();
layout->addLayout(spacer, 10);
setLayout(layout);
}
void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet)
{
for(unsigned i = 0; i < m_maxplayers; ++i)
{
QGridLayout *score = m_playerScoreLayouts.at(i);
QLabel *turnPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(0, 1)->widget());
turnPointsLbl->setText(QString::number(packet.round_points(i)));
QLabel *allPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(1, 1)->widget());
allPointsLbl->setText(QString::number(packet.round_points(i)));
}
}
Transmission::field_t MainWidget::translateKey(int key)
{
switch(key)
{
case Qt::Key_W:
case Qt::Key_Up:
return Transmission::direction_up;
break;
case Qt::Key_S:
case Qt::Key_Down:
return Transmission::direction_down;
break;
case Qt::Key_A:
case Qt::Key_Left:
return Transmission::direction_left;
break;
case Qt::Key_D:
case Qt::Key_Right:
return Transmission::direction_right;
break;
default:
return Transmission::direction_none;
}
}
void MainWidget::tick()
{
while (m_socket->bytesAvailable() > (qint64)sizeof(qint64))
{
QSharedPointer<QByteArray> data = Util::receivePacket(m_socket);
bool worked = m_updatepacket.ParseFromArray(data->data(), data->size());
Q_ASSERT(worked);
Q_UNUSED(worked);
if (m_updatepacket.eating_order_size() > 0)
{
Q_ASSERT(m_scene != NULL);
QList<Color::Color> order;
for(int i = 0; i < m_updatepacket.eating_order_size(); ++i)
order.append(static_cast<Color::Color>(m_updatepacket.eating_order(i) & Transmission::color_mask));
m_scene->setEatingOrder(order);
}
Transmission::map_t map = Util::createUninitialisedMap();
Q_ASSERT(m_updatepacket.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
int i = 0;
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
{
map[x][y] = m_updatepacket.field(i);
++i;
}
}
m_scene->updateMap(map);
Util::deleteMap(map);
updateScore(m_updatepacket);
}
}
void MainWidget::keyPressEvent(QKeyEvent* event)
{
if (!m_running)
return;
if (event->isAutoRepeat())
return;
QWidget::keyPressEvent(event);
Transmission::field_t newKey = translateKey(event->key());
if (newKey == Transmission::direction_none)
return;
bool sendUpdate = (m_currentKey != newKey);
m_currentKey = newKey;
if (sendUpdate)
sendKeyUpdate();
}
void MainWidget::sendKeyUpdate()
{
if (!m_running)
return;
if (m_currentKey == Transmission::direction_none)
return;
qDebug() << "[SendKey] key=" << m_currentKey;
ProtoBuf::KeyPressUpdate packet;
packet.set_newkey(m_currentKey);
Util::sendPacket(packet, m_socket);
}
void MainWidget::keyReleaseEvent(QKeyEvent* event)
{
if (!m_running)
return;
if (event->isAutoRepeat())
return;
QWidget::keyReleaseEvent(event);
m_currentKey = Transmission::none;
return;
}
void MainWidget::startGame()
{
m_running = true;
m_ambientPlayer->play();
}
void MainWidget::setAmbientMuted(bool muted)
{
m_ambientPlayer->setMuted(muted);
}
void MainWidget::playerScoreClicked()
{
QGroupBox *tmp = qobject_cast<QGroupBox *>(sender());
tmp->setChecked(true);
return;
}
Color::Color MainWidget::connectToServer()
{
/* TODO: check command line arguments for server port */
const QStringList &args = QCoreApplication::arguments();
QString srv = args.value(1, "127.0.0.1");
qDebug() << "[Connect] server=" << srv;
/* connect to server */
m_socket = new QTcpSocket(this);
m_socket->connectToHost(srv, Constants::Networking::port);
bool worked = m_socket->waitForConnected(Constants::Networking::connection_timeout);
if (worked)
{
/* additional init: first packet is our color */
worked = m_socket->waitForReadyRead();
if (worked)
{
/* receive color */
QSharedPointer<QByteArray> data = Util::receivePacket(m_socket);
ProtoBuf::Init packet;
bool worked = packet.ParseFromArray(data->data(), data->size());
Q_ASSERT(worked);
Q_UNUSED(worked);
m_maxplayers = packet.maxplayers();
return static_cast<Color::Color>(packet.color() & Transmission::color_mask);
}
}
return Color::none;
}
|