diff options
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 82 |
1 files changed, 68 insertions, 14 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 06a0b9c..da08d89 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | #include "constants.h" | 4 | #include "constants.h" |
| 5 | #include "audioplayer.h" | 5 | #include "audioplayer.h" |
| 6 | #include "util.h" | 6 | #include "util.h" |
| 7 | #include "pacman.pb.h" | ||
| 7 | 8 | ||
| 8 | MainWidget::MainWidget(QWidget *parent) | 9 | MainWidget::MainWidget(QWidget *parent) |
| 9 | : SceneHolder(parent), m_currentKey(0), m_running(false) | 10 | : SceneHolder(parent), m_currentKey(0), m_running(false) |
| @@ -14,9 +15,15 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 14 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); | 15 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); |
| 15 | //AudioPlayer::self()->play(AudioPlayer::Intro); | 16 | //AudioPlayer::self()->play(AudioPlayer::Intro); |
| 16 | 17 | ||
| 17 | QTimer *timer = new QTimer(this); | 18 | bool connected = connectToServer(); |
| 18 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 19 | if (!connected) { |
| 19 | timer->start(Constants::tick); | 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); |
| 21 | // TODO: quit application here or sth | ||
| 22 | QTimer *timer = new QTimer(this); | ||
| 23 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | ||
| 24 | timer->start(Constants::tick); | ||
| 25 | } | ||
| 26 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | ||
| 20 | 27 | ||
| 21 | startGame(); | 28 | startGame(); |
| 22 | } | 29 | } |
| @@ -120,21 +127,49 @@ Transmission::field_t MainWidget::translateKey(int key) | |||
| 120 | 127 | ||
| 121 | void MainWidget::tick() | 128 | void MainWidget::tick() |
| 122 | { | 129 | { |
| 123 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); | 130 | #if 0 |
| 131 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); | ||
| 124 | 132 | ||
| 125 | QMapIterator<Color::Color, Actor*> i(m_actors); | 133 | QMapIterator<Color::Color, Actor*> i(m_actors); |
| 126 | while (i.hasNext()) | 134 | while (i.hasNext()) |
| 127 | { | ||
| 128 | i.next(); | ||
| 129 | i.value()->move(mov); | ||
| 130 | QList<QGraphicsItem *> list(i.value()->collidingItems()); | ||
| 131 | for(int j = 0; j < list.count(); ++j) | ||
| 132 | { | 135 | { |
| 133 | if (list.at(j)->parentItem() == i.value()) | 136 | i.next(); |
| 134 | continue; | 137 | i.value()->move(mov); |
| 135 | list.at(j)->setOpacity(0.6); | 138 | QList<QGraphicsItem *> list(i.value()->collidingItems()); |
| 139 | for(int j = 0; j < list.count(); ++j) | ||
| 140 | { | ||
| 141 | if (list.at(j)->parentItem() == i.value()) | ||
| 142 | continue; | ||
| 143 | list.at(j)->setOpacity(0.6); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | #else | ||
| 147 | QByteArray data = m_socket->readAll(); | ||
| 148 | //qDebug() << "read qbytes " << data.length(); | ||
| 149 | // TODO: normal conversion to std::string won't work, | ||
| 150 | // probably due to \0-bytes. both those methods will yield | ||
| 151 | // a 3 char long string | ||
| 152 | //std::string dataStr = std::string(data.constData()); | ||
| 153 | //std::string dataStr = QString(data).toStdString(); | ||
| 154 | std::string dataStr; | ||
| 155 | for (int i = 0; i < data.size(); ++i) { | ||
| 156 | dataStr += data[i]; | ||
| 157 | } | ||
| 158 | |||
| 159 | //qDebug() << "read str " << dataStr.length(); | ||
| 160 | ProtoBuf::MapUpdate packet; | ||
| 161 | packet.ParseFromString(dataStr); | ||
| 162 | Transmission::map_t map = Util::createUninitialisedMap(); | ||
| 163 | Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height)); | ||
| 164 | int i = 0; | ||
| 165 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) { | ||
| 166 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) { | ||
| 167 | map[x][y] = packet.field(i); | ||
| 168 | ++i; | ||
| 136 | } | 169 | } |
| 137 | } | 170 | } |
| 171 | updateMap(map); | ||
| 172 | #endif | ||
| 138 | } | 173 | } |
| 139 | 174 | ||
| 140 | 175 | ||
| @@ -144,8 +179,19 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 144 | return; | 179 | return; |
| 145 | 180 | ||
| 146 | QWidget::keyPressEvent(event); | 181 | QWidget::keyPressEvent(event); |
| 182 | // TODO: remove m_currentKey when we don't need it any more for testing | ||
| 147 | m_currentKey = translateKey(event->key()); | 183 | m_currentKey = translateKey(event->key()); |
| 148 | 184 | ||
| 185 | // send to server | ||
| 186 | ProtoBuf::KeyPressUpdate packet; | ||
| 187 | packet.set_newkey(m_currentKey); | ||
| 188 | std::string dataStr = packet.SerializeAsString(); | ||
| 189 | unsigned int bytesWritten = m_socket->write(dataStr.c_str(), dataStr.length()); | ||
| 190 | m_socket->flush(); | ||
| 191 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 192 | |||
| 193 | qDebug() << "send key: " << m_currentKey; | ||
| 194 | |||
| 149 | return; | 195 | return; |
| 150 | 196 | ||
| 151 | // test stuff | 197 | // test stuff |
| @@ -186,3 +232,11 @@ void MainWidget::playerScoreClicked() | |||
| 186 | tmp->setChecked(true); | 232 | tmp->setChecked(true); |
| 187 | return; | 233 | return; |
| 188 | } | 234 | } |
| 235 | |||
| 236 | bool MainWidget::connectToServer() | ||
| 237 | { | ||
| 238 | m_socket = new QTcpSocket(this); | ||
| 239 | m_socket->connectToHost("127.0.0.1", Constants::port); | ||
| 240 | return m_socket->waitForConnected(3000); | ||
| 241 | } | ||
| 242 | |||
