#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(); qDebug() << "[Connect] mycolor=" << m_scene->color(); connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); } 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); 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(score->itemAtPosition(0, 1)->widget()); turnPointsLbl->setText(QString::number(packet.round_points(i))); QLabel *allPointsLbl = dynamic_cast(score->itemAtPosition(1, 1)->widget()); allPointsLbl->setText(QString::number(packet.game_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 data = Util::receivePacket(m_socket); bool worked = m_updatepacket.ParseFromArray(data->data(), data->size()); Q_ASSERT(worked); Q_UNUSED(worked); /* eating order data set indicates a new round */ if (m_updatepacket.eating_order_size() > 0) { Q_ASSERT(m_scene != NULL); m_scene->reset(); /* fetch eating order */ QList order; for(int i = 0; i < m_updatepacket.eating_order_size(); ++i) order.append(static_cast(m_updatepacket.eating_order(i) & Transmission::color_mask)); m_scene->setEatingOrder(order); /* stop and restart game */ stopGame(); connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(startGame())); AudioManager::self()->play(Sound::Intro, true); } 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 (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_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 (event->isAutoRepeat()) return; QWidget::keyReleaseEvent(event); m_currentKey = Transmission::none; return; } void MainWidget::startGame() { disconnect(AudioManager::self()->audioPlayer(), NULL, this, SLOT(startGame())); m_running = true; sendKeyUpdate(); m_ambientPlayer->play(); } void MainWidget::stopGame() { m_running = false; m_ambientPlayer->pause(); } void MainWidget::setAmbientMuted(bool muted) { m_ambientPlayer->setMuted(muted); } void MainWidget::playerScoreClicked() { QGroupBox *tmp = qobject_cast(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 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(packet.color() & Transmission::color_mask); } } return Color::none; }