From 817dcc4e4493fb89c6d52b5e5ce1548b6a047d08 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 10 Apr 2011 21:34:29 +0200 Subject: - first round of coding cleanup - small sound fix --- pacman-c++/mainwidget.cpp | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'pacman-c++/mainwidget.cpp') diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index ebd0f02..f5d4b7f 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -10,26 +10,27 @@ 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); + updateMap(Util::createDemoMap()); Color::Color myColor = connectToServer(); - if (myColor == Color::none) { + 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())); } + else + connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); // TODO: use mycolor - qDebug() << "myc" << myColor; + qDebug() << "mycolor=" << myColor; + //TODO: play intro as soon as there are enough players + //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); + //AudioPlayer::self()->play(AudioPlayer::Intro); startGame(); } @@ -82,6 +83,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet) QLabel *turnPointsLbl; QLabel *allPointsLbl; + //TODO: change proto and move that inside an array turnPointsLbl = dynamic_cast(m_playerScoreLayouts.at(0)->itemAtPosition(0,1)->widget()); allPointsLbl = dynamic_cast(m_playerScoreLayouts.at(0)->itemAtPosition(1,1)->widget()); turnPointsLbl->setText(QString::number(packet.round_points_red())); @@ -98,7 +100,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet) allPointsLbl->setText(QString::number(packet.game_points_green())); } -Transmission::field_t MainWidget::translateKey(int key, int def) +Transmission::field_t MainWidget::translateKey(int key) { switch(key) { @@ -119,16 +121,16 @@ Transmission::field_t MainWidget::translateKey(int key, int def) return Transmission::direction_right; break; default: - return def; + return Transmission::direction_none; } } void MainWidget::tick() { - if (m_socket == NULL) { + if (m_socket == NULL) + { // OLD TEST MODE Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); - QMapIterator i(m_actors); while (i.hasNext()) { @@ -142,7 +144,9 @@ void MainWidget::tick() list.at(j)->setOpacity(0.6); } } - } else { + } + else + { std::string dataStr; Util::QByteArrayToStdString(m_socket->readAll(), dataStr); @@ -152,8 +156,10 @@ void MainWidget::tick() 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) { + 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; } @@ -169,23 +175,23 @@ void MainWidget::keyPressEvent(QKeyEvent* event) return; QWidget::keyPressEvent(event); - Transmission::field_t newKey = translateKey(event->key(), -1); - if (m_currentKey == newKey || newKey == -1) { + Transmission::field_t newKey = translateKey(event->key()); + if (m_currentKey == newKey || newKey == Transmission::direction_none) return; - } m_currentKey = newKey; - if (m_socket != NULL) { + 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 { - + } + else + { // test stuff Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); - QMapIterator i(m_actors); while (i.hasNext()) { @@ -229,11 +235,13 @@ 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) { + if (worked) + { // additional init // first packet is our color worked = m_socket->waitForReadyRead(); - if (worked) { + if (worked) + { std::string data; Util::QByteArrayToStdString(m_socket->readAll(), data); ProtoBuf::WhoAmI packet; @@ -243,4 +251,3 @@ Color::Color MainWidget::connectToServer() } return Color::none; } - -- cgit v1.2.3