diff options
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 142 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 2 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 2 |
3 files changed, 77 insertions, 69 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index da08d89..1631d3f 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -7,7 +7,7 @@ | |||
| 7 | #include "pacman.pb.h" | 7 | #include "pacman.pb.h" |
| 8 | 8 | ||
| 9 | MainWidget::MainWidget(QWidget *parent) | 9 | MainWidget::MainWidget(QWidget *parent) |
| 10 | : SceneHolder(parent), m_currentKey(0), m_running(false) | 10 | : SceneHolder(parent), m_currentKey(0), m_running(false) |
| 11 | { | 11 | { |
| 12 | createGui(); | 12 | createGui(); |
| 13 | updateMap(Util::createDummyMap()); | 13 | updateMap(Util::createDummyMap()); |
| @@ -19,6 +19,7 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 19 | if (!connected) { | 19 | if (!connected) { |
| 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); | 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); |
| 21 | // TODO: quit application here or sth | 21 | // TODO: quit application here or sth |
| 22 | m_socket = NULL; | ||
| 22 | QTimer *timer = new QTimer(this); | 23 | QTimer *timer = new QTimer(this); |
| 23 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 24 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| 24 | timer->start(Constants::tick); | 25 | timer->start(Constants::tick); |
| @@ -85,9 +86,9 @@ void MainWidget::updateScore() | |||
| 85 | } | 86 | } |
| 86 | int id = (i.key() >> 1); | 87 | int id = (i.key() >> 1); |
| 87 | QLabel *turnPointsLbl = | 88 | QLabel *turnPointsLbl = |
| 88 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); | 89 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); |
| 89 | QLabel *allPointsLbl = | 90 | QLabel *allPointsLbl = |
| 90 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); | 91 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); |
| 91 | turnPointsLbl->setText(QString::number(id)); | 92 | turnPointsLbl->setText(QString::number(id)); |
| 92 | allPointsLbl->setText(QString::number(id)); | 93 | allPointsLbl->setText(QString::number(id)); |
| 93 | } | 94 | } |
| @@ -100,34 +101,35 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 100 | } | 101 | } |
| 101 | 102 | ||
| 102 | 103 | ||
| 103 | Transmission::field_t MainWidget::translateKey(int key) | 104 | Transmission::field_t MainWidget::translateKey(int key, int def) |
| 104 | { | 105 | { |
| 105 | switch(key) | 106 | switch(key) |
| 106 | { | 107 | { |
| 107 | case Qt::Key_W: | 108 | case Qt::Key_W: |
| 108 | case Qt::Key_Up: | 109 | case Qt::Key_Up: |
| 109 | return Transmission::direction_up; | 110 | return Transmission::direction_up; |
| 110 | break; | 111 | break; |
| 111 | case Qt::Key_S: | 112 | case Qt::Key_S: |
| 112 | case Qt::Key_Down: | 113 | case Qt::Key_Down: |
| 113 | return Transmission::direction_down; | 114 | return Transmission::direction_down; |
| 114 | break; | 115 | break; |
| 115 | case Qt::Key_A: | 116 | case Qt::Key_A: |
| 116 | case Qt::Key_Left: | 117 | case Qt::Key_Left: |
| 117 | return Transmission::direction_left; | 118 | return Transmission::direction_left; |
| 118 | break; | 119 | break; |
| 119 | case Qt::Key_D: | 120 | case Qt::Key_D: |
| 120 | case Qt::Key_Right: | 121 | case Qt::Key_Right: |
| 121 | return Transmission::direction_right; | 122 | return Transmission::direction_right; |
| 122 | break; | 123 | break; |
| 123 | default: | 124 | default: |
| 124 | return 0; | 125 | return def; |
| 125 | } | 126 | } |
| 126 | } | 127 | } |
| 127 | 128 | ||
| 128 | void MainWidget::tick() | 129 | void MainWidget::tick() |
| 129 | { | 130 | { |
| 130 | #if 0 | 131 | if (m_socket == NULL) { |
| 132 | // OLD TEST MODE | ||
| 131 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); | 133 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); |
| 132 | 134 | ||
| 133 | QMapIterator<Color::Color, Actor*> i(m_actors); | 135 | QMapIterator<Color::Color, Actor*> i(m_actors); |
| @@ -143,33 +145,34 @@ void MainWidget::tick() | |||
| 143 | list.at(j)->setOpacity(0.6); | 145 | list.at(j)->setOpacity(0.6); |
| 144 | } | 146 | } |
| 145 | } | 147 | } |
| 146 | #else | 148 | } else { |
| 147 | QByteArray data = m_socket->readAll(); | 149 | QByteArray data = m_socket->readAll(); |
| 148 | //qDebug() << "read qbytes " << data.length(); | 150 | //qDebug() << "read qbytes " << data.length(); |
| 149 | // TODO: normal conversion to std::string won't work, | 151 | // TODO: normal conversion to std::string won't work, |
| 150 | // probably due to \0-bytes. both those methods will yield | 152 | // probably due to \0-bytes. both those methods will yield |
| 151 | // a 3 char long string | 153 | // a 3 char long string |
| 152 | //std::string dataStr = std::string(data.constData()); | 154 | //std::string dataStr = std::string(data.constData()); |
| 153 | //std::string dataStr = QString(data).toStdString(); | 155 | //std::string dataStr = QString(data).toStdString(); |
| 154 | std::string dataStr; | 156 | std::string dataStr; |
| 155 | for (int i = 0; i < data.size(); ++i) { | 157 | for (int i = 0; i < data.size(); ++i) { |
| 156 | dataStr += data[i]; | 158 | dataStr += data[i]; |
| 157 | } | 159 | } |
| 158 | 160 | ||
| 159 | //qDebug() << "read str " << dataStr.length(); | 161 | //qDebug() << "read str " << dataStr.length(); |
| 160 | ProtoBuf::MapUpdate packet; | 162 | ProtoBuf::MapUpdate packet; |
| 161 | packet.ParseFromString(dataStr); | 163 | packet.ParseFromString(dataStr); |
| 162 | Transmission::map_t map = Util::createUninitialisedMap(); | 164 | Transmission::map_t map = Util::createUninitialisedMap(); |
| 163 | Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height)); | 165 | Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height)); |
| 164 | int i = 0; | 166 | int i = 0; |
| 165 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) { | 167 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) { |
| 166 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) { | 168 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) { |
| 167 | map[x][y] = packet.field(i); | 169 | map[x][y] = packet.field(i); |
| 168 | ++i; | 170 | ++i; |
| 171 | } | ||
| 169 | } | 172 | } |
| 173 | |||
| 174 | updateMap(map); | ||
| 170 | } | 175 | } |
| 171 | updateMap(map); | ||
| 172 | #endif | ||
| 173 | } | 176 | } |
| 174 | 177 | ||
| 175 | 178 | ||
| @@ -179,34 +182,39 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 179 | return; | 182 | return; |
| 180 | 183 | ||
| 181 | QWidget::keyPressEvent(event); | 184 | QWidget::keyPressEvent(event); |
| 182 | // TODO: remove m_currentKey when we don't need it any more for testing | 185 | Transmission::field_t newKey = translateKey(event->key()); |
| 183 | m_currentKey = translateKey(event->key()); | 186 | if (m_currentKey == newKey || newKey == -1) { |
| 184 | 187 | return; | |
| 185 | // send to server | 188 | } |
| 186 | ProtoBuf::KeyPressUpdate packet; | 189 | m_currentKey = newKey; |
| 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 | 190 | ||
| 193 | qDebug() << "send key: " << m_currentKey; | 191 | if (m_socket != NULL) { |
| 192 | // send to server | ||
| 193 | ProtoBuf::KeyPressUpdate packet; | ||
| 194 | packet.set_newkey(m_currentKey); | ||
| 195 | std::string dataStr = packet.SerializeAsString(); | ||
| 196 | unsigned int bytesWritten = m_socket->write(dataStr.c_str(), dataStr.length()); | ||
| 197 | m_socket->flush(); | ||
| 198 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 194 | 199 | ||
| 195 | return; | 200 | qDebug() << "send key: " << m_currentKey; |
| 201 | } else { | ||
| 196 | 202 | ||
| 197 | // test stuff | 203 | // test stuff |
| 198 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); | 204 | Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); |
| 199 | 205 | ||
| 200 | QMapIterator<Color::Color, Actor*> i(m_actors); | 206 | QMapIterator<Color::Color, Actor*> i(m_actors); |
| 201 | while (i.hasNext()) | 207 | while (i.hasNext()) |
| 202 | { | 208 | { |
| 203 | i.next(); | 209 | i.next(); |
| 204 | i.value()->move(mov); | 210 | i.value()->move(mov); |
| 211 | } | ||
| 205 | } | 212 | } |
| 206 | } | 213 | } |
| 207 | 214 | ||
| 208 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | 215 | void MainWidget::keyReleaseEvent(QKeyEvent* event) |
| 209 | { | 216 | { |
| 217 | return; // currently not needed | ||
| 210 | if (!m_running) | 218 | if (!m_running) |
| 211 | return; | 219 | return; |
| 212 | 220 | ||
| @@ -237,6 +245,6 @@ bool MainWidget::connectToServer() | |||
| 237 | { | 245 | { |
| 238 | m_socket = new QTcpSocket(this); | 246 | m_socket = new QTcpSocket(this); |
| 239 | m_socket->connectToHost("127.0.0.1", Constants::port); | 247 | m_socket->connectToHost("127.0.0.1", Constants::port); |
| 240 | return m_socket->waitForConnected(3000); | 248 | return m_socket->waitForConnected(Constants::connection_timeout); |
| 241 | } | 249 | } |
| 242 | 250 | ||
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index eaa8be0..04beec2 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -45,7 +45,7 @@ private: | |||
| 45 | Transmission::field_t m_currentKey; | 45 | Transmission::field_t m_currentKey; |
| 46 | 46 | ||
| 47 | // translate Qt::Key to our key format | 47 | // translate Qt::Key to our key format |
| 48 | Transmission::field_t translateKey(int); | 48 | Transmission::field_t translateKey(int key, int def=0); |
| 49 | 49 | ||
| 50 | // game running | 50 | // game running |
| 51 | bool m_running; | 51 | bool m_running; |
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 8f9e42d..2dc2a13 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -16,7 +16,7 @@ Server::Server(QWidget *parent) | |||
| 16 | 16 | ||
| 17 | QTimer *timer = new QTimer(this); | 17 | QTimer *timer = new QTimer(this); |
| 18 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 18 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| 19 | timer->start(500); | 19 | timer->start(Constants::tick); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | void Server::tick() | 22 | void Server::tick() |
