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++/actor.cpp | 27 +++++++++- pacman-c++/mainwidget.cpp | 120 ++++++++++++++++--------------------------- pacman-c++/mainwidget.h | 4 +- pacman-c++/pacman.server.pro | 2 - pacman-c++/sceneholder.cpp | 4 +- pacman-c++/server.cpp | 19 ++++++- 6 files changed, 93 insertions(+), 83 deletions(-) diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 43368d6..bb062ea 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -118,8 +118,8 @@ bool Actor::isLocal() void Actor::move(Actor::Movement direction) { - /*if (Constants::server) - return moveByServer(direction);*/ + if (Constants::server) + return moveByServer(direction); /* stop current animation */ if (direction != m_direction) @@ -231,5 +231,28 @@ void Actor::finishRound() void Actor::moveByServer(Actor::Movement direction) { qDebug() << "move by server"; + + QPointF endpos(0, 0); + switch(direction) + { + case Actor::None: + break; + case Actor::Left: + endpos.setX(static_cast(Constants::field_size.width) * -1); + break; + case Actor::Right: + endpos.setX(Constants::field_size.width); + break; + case Actor::Up: + endpos.setY(static_cast(Constants::field_size.height) * -1); + break; + case Actor::Down: + endpos.setY(Constants::field_size.height); + break; + default: + Q_ASSERT(false); + break; + } + setPos(pos() + endpos); m_direction = direction; } 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() diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index 46de6fb..ec053a0 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -18,9 +18,10 @@ class MainWidget public: MainWidget(QWidget *parent = 0); + bool connected(); protected: - // handling of current key + /* handling of current key */ virtual void keyPressEvent(QKeyEvent* ); virtual void keyReleaseEvent(QKeyEvent* ); @@ -28,6 +29,7 @@ private slots: void startGame(); void playerScoreClicked(); void tick(); + void sendKeyUpdate(); private: void createGui(); diff --git a/pacman-c++/pacman.server.pro b/pacman-c++/pacman.server.pro index 0b5fd6d..1463818 100644 --- a/pacman-c++/pacman.server.pro +++ b/pacman-c++/pacman.server.pro @@ -12,7 +12,6 @@ SOURCES += pixmapitem.cpp \ block.cpp \ server.cpp \ bonuspoint.cpp \ - mainwidget.cpp \ point.cpp \ audio.cpp \ sceneholder.cpp \ @@ -25,7 +24,6 @@ HEADERS += pixmapitem.h \ block.h \ server.h \ bonuspoint.h \ - mainwidget.h \ constants.h \ point.h \ audio.h \ diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index f13bf76..c9bcbf9 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -100,11 +100,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map) } else if (cur & Transmission::empty) { - // already handled + /* already handled */ } else { - qDebug() << "abort at " << cur; + qWarning() << "Unknown data value at" << cur; Q_ASSERT(false); } diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index b054804..c33c559 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -43,6 +43,10 @@ Transmission::map_t Server::calculateUpdates() while (i.hasNext()) { i.next(); + int turn = 0; + +invalid_direction: + ++turn; Actor *actor = m_actors.value(i.key()); QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); qDebug() << "actor " << i.key() << " is at " << mapPosition << "moving " << i.value(); @@ -108,8 +112,21 @@ Transmission::map_t Server::calculateUpdates() } // + /* movement didn't work - e.g. was blocked */ if (mapPosition == newMapPosition) - m_actorMovements[i.key()] = Actor::None; + { + if (turn == 1) + { + /* set direction back to last known direction and try again */ + m_actorMovements[i.key()] = actor->direction(); + goto invalid_direction; + } + else + { + /* second turn didn't work too -> stop movement */ + m_actorMovements[i.key()] = Actor::None; + } + } map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | i.key() | Util::actorMovementToTransmission(i.value()); -- cgit v1.2.3