From 7667effa9635b0c25088305fe89c15a9805d6dbb Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 11 Apr 2011 16:58:36 +0200 Subject: - server doesn't neet mainwidget.cpp - removed local mode (doesn't work anyway) - made movement more like orginal pacman --- pacman-c++/mainwidget.cpp | 120 +++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 75 deletions(-) (limited to 'pacman-c++/mainwidget.cpp') diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index d7318c9..518692c 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -7,28 +7,26 @@ #include "pacman.pb.h" MainWidget::MainWidget(QWidget *parent) -: SceneHolder(parent), m_currentKey(0), m_running(false) + : SceneHolder(parent), m_currentKey(Transmission::none), m_running(false) { m_color = connectToServer(); if (m_color == 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; - m_color = Color::red; - QTimer *timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(tick())); - timer->start(Constants::tick); + return; } /* call updateMap after m_color ist set! */ createGui(); updateMap(Util::createDemoMap()); - if (m_socket != NULL) - connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); + connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); + + QTimer *sendTimer = new QTimer(); + connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate())); + sendTimer->start(Constants::tick); - // TODO: use mycolor qDebug() << "mycolor=" << m_color; //TODO: play intro as soon as there are enough players @@ -37,6 +35,11 @@ MainWidget::MainWidget(QWidget *parent) startGame(); } +bool MainWidget::connected() +{ + return m_socket != NULL; +} + void MainWidget::createGui() { setFocusPolicy(Qt::StrongFocus); @@ -124,47 +127,26 @@ Transmission::field_t MainWidget::translateKey(int key) 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; + bool worked = packet.ParseFromString(dataStr); + Q_ASSERT(worked); + 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) { - std::string dataStr; - Util::QByteArrayToStdString(m_socket->readAll(), dataStr); - - //qDebug() << "read str " << dataStr.length(); - ProtoBuf::MapUpdate packet; - bool worked = packet.ParseFromString(dataStr); - Q_ASSERT(worked); - 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 y = 0; y < Constants::map_size.height; ++y) - { - map[x][y] = packet.field(i); - ++i; - } + map[x][y] = packet.field(i); + ++i; } - updateMap(map); - updateScore(packet); } + updateMap(map); + updateScore(packet); } void MainWidget::keyPressEvent(QKeyEvent* event) @@ -174,46 +156,34 @@ void MainWidget::keyPressEvent(QKeyEvent* event) QWidget::keyPressEvent(event); Transmission::field_t newKey = translateKey(event->key()); - if (m_currentKey == newKey || newKey == Transmission::direction_none) + if (newKey == Transmission::direction_none) return; + bool sendUpdate = (m_currentKey != newKey); m_currentKey = newKey; + if (sendUpdate) + sendKeyUpdate(); +} - 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::sendKeyUpdate() +{ + if (!m_running) + return; + if (m_currentKey == Transmission::direction_none) + return; + qDebug() << "send key: " << m_currentKey; + ProtoBuf::KeyPressUpdate packet; + packet.set_newkey(m_currentKey); + Util::sendPacket(packet, m_socket); } 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; - } + m_currentKey = Transmission::none; + return; } void MainWidget::startGame() -- cgit v1.2.3