#include "mainwidget.h" #include "actor.h" #include "block.h" #include "constants.h" #include "audioplayer.h" #include "util.h" #include "pacman.pb.h" MainWidget::MainWidget(QWidget *parent) : SceneHolder(parent), m_currentKey(0), m_running(false) { createGui(); updateMap(Util::createDummyMap()); //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); //AudioPlayer::self()->play(AudioPlayer::Intro); Color::Color myColor = connectToServer(); if (myColor == Color::none) { QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); // TODO: quit application here or sth m_socket = NULL; QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(tick())); timer->start(Constants::tick); } else { connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); } // TODO: use mycolor qDebug() << "myc" << myColor; startGame(); } void MainWidget::createGui() { setFocusPolicy(Qt::StrongFocus); QVBoxLayout *layout = new QVBoxLayout(this); QHBoxLayout *scoreLayout = new QHBoxLayout(); for (unsigned int i = 1; i < 4; ++i) { QGroupBox *scoreBox = new QGroupBox(QString("Spieler %1").arg(i), this); scoreBox->setObjectName(QString("actor%1").arg(i)); scoreBox->setCheckable(true); connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked())); scoreLayout->addWidget(scoreBox); QGridLayout *playerLayout = new QGridLayout(); scoreBox->setLayout(playerLayout); playerLayout->addWidget(new QLabel("Rundenpunkte:", this), 0, 0); playerLayout->addWidget(new QLabel("Gesamtpunkte:", this), 1, 0); playerLayout->addWidget(new QLabel("", this), 0, 1); playerLayout->addWidget(new QLabel("", this), 1, 1); m_playerScoreLayouts.append(playerLayout); } QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); window->setAlignment(Qt::AlignLeft | Qt::AlignTop); window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); layout->addLayout(scoreLayout); layout->addWidget(window); QFile css(":/stylesheet"); css.open(QFile::ReadOnly); qApp->setStyleSheet(QLatin1String(css.readAll())); setLayout(layout); } void MainWidget::updateScore() { QMapIterator i(m_actors); while (i.hasNext()) { i.next(); if (i.key() > Color::max) { /* player #4 isn't supported in score */ Q_ASSERT(false); continue; } int id = (i.key() >> 1); QLabel *turnPointsLbl = dynamic_cast(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); QLabel *allPointsLbl = dynamic_cast(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); turnPointsLbl->setText(QString::number(id)); allPointsLbl->setText(QString::number(id)); } } void MainWidget::updateMap(const Transmission::map_t& map) { SceneHolder::updateMap(map); updateScore(); } Transmission::field_t MainWidget::translateKey(int key, int def) { 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 def; } } void MainWidget::tick() { if (m_socket == NULL) { // OLD TEST MODE Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); QMapIterator i(m_actors); while (i.hasNext()) { i.next(); i.value()->move(mov); QList list(i.value()->collidingItems()); for(int j = 0; j < list.count(); ++j) { if (list.at(j)->parentItem() == i.value()) continue; list.at(j)->setOpacity(0.6); } } } else { std::string dataStr; Util::QByteArrayToStdString(m_socket->readAll(), dataStr); //qDebug() << "read str " << dataStr.length(); ProtoBuf::MapUpdate packet; packet.ParseFromString(dataStr); Transmission::map_t map = Util::createUninitialisedMap(); Q_ASSERT(packet.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] = packet.field(i); ++i; } } updateMap(map); } } void MainWidget::keyPressEvent(QKeyEvent* event) { if (!m_running) return; QWidget::keyPressEvent(event); Transmission::field_t newKey = translateKey(event->key(), -1); if (m_currentKey == newKey || newKey == -1) { return; } m_currentKey = newKey; if (m_socket != NULL) { // send to server ProtoBuf::KeyPressUpdate packet; packet.set_newkey(m_currentKey); Util::sendPacket(packet, m_socket); qDebug() << "send key: " << m_currentKey; } else { // test stuff Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); QMapIterator i(m_actors); while (i.hasNext()) { i.next(); i.value()->move(mov); } } } void MainWidget::keyReleaseEvent(QKeyEvent* event) { return; // currently not needed if (!m_running) return; QWidget::keyReleaseEvent(event); Transmission::field_t releasedKey = translateKey(event->key()); if (releasedKey == m_currentKey) { // current key got released // if this is false, a key got released which has already // been replaced by a different key, so this case is disregarded m_currentKey = Transmission::direction_none; } } void MainWidget::startGame() { m_running = true; } void MainWidget::playerScoreClicked() { QGroupBox *tmp = qobject_cast(sender()); tmp->setChecked(true); return; } Color::Color MainWidget::connectToServer() { m_socket = new QTcpSocket(this); m_socket->connectToHost("127.0.0.1", Constants::port); bool worked = m_socket->waitForConnected(Constants::connection_timeout); if (worked) { // additional init // first packet is our color worked = m_socket->waitForReadyRead(); if (worked) { std::string data; Util::QByteArrayToStdString(m_socket->readAll(), data); ProtoBuf::WhoAmI packet; packet.ParseFromString(data); return static_cast(packet.color() & Transmission::color_mask); } } return Color::none; }