summaryrefslogtreecommitdiffstats
path: root/pacman-c++/mainwidget.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-11 16:58:36 +0200
committermanuel <manuel@mausz.at>2011-04-11 16:58:36 +0200
commit7667effa9635b0c25088305fe89c15a9805d6dbb (patch)
tree587cdd64e633c2940f71e269cbb119cfebf4ce48 /pacman-c++/mainwidget.cpp
parentee094b8cd15d040cb64b2082673f7bd2a2c61f4b (diff)
downloadfoop-7667effa9635b0c25088305fe89c15a9805d6dbb.tar.gz
foop-7667effa9635b0c25088305fe89c15a9805d6dbb.tar.bz2
foop-7667effa9635b0c25088305fe89c15a9805d6dbb.zip
- server doesn't neet mainwidget.cpp
- removed local mode (doesn't work anyway) - made movement more like orginal pacman
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
-rw-r--r--pacman-c++/mainwidget.cpp120
1 files changed, 45 insertions, 75 deletions
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 @@
7#include "pacman.pb.h" 7#include "pacman.pb.h"
8 8
9MainWidget::MainWidget(QWidget *parent) 9MainWidget::MainWidget(QWidget *parent)
10: SceneHolder(parent), m_currentKey(0), m_running(false) 10 : SceneHolder(parent), m_currentKey(Transmission::none), m_running(false)
11{ 11{
12 m_color = connectToServer(); 12 m_color = connectToServer();
13 if (m_color == Color::none) 13 if (m_color == Color::none)
14 { 14 {
15 QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); 15 QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode");
16 // TODO: quit application here or sth 16 // TODO: quit application here or sth
17 m_socket = NULL; 17 return;
18 m_color = Color::red;
19 QTimer *timer = new QTimer(this);
20 connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
21 timer->start(Constants::tick);
22 } 18 }
23 19
24 /* call updateMap after m_color ist set! */ 20 /* call updateMap after m_color ist set! */
25 createGui(); 21 createGui();
26 updateMap(Util::createDemoMap()); 22 updateMap(Util::createDemoMap());
27 23
28 if (m_socket != NULL) 24 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick()));
29 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); 25
26 QTimer *sendTimer = new QTimer();
27 connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate()));
28 sendTimer->start(Constants::tick);
30 29
31 // TODO: use mycolor
32 qDebug() << "mycolor=" << m_color; 30 qDebug() << "mycolor=" << m_color;
33 31
34 //TODO: play intro as soon as there are enough players 32 //TODO: play intro as soon as there are enough players
@@ -37,6 +35,11 @@ MainWidget::MainWidget(QWidget *parent)
37 startGame(); 35 startGame();
38} 36}
39 37
38bool MainWidget::connected()
39{
40 return m_socket != NULL;
41}
42
40void MainWidget::createGui() 43void MainWidget::createGui()
41{ 44{
42 setFocusPolicy(Qt::StrongFocus); 45 setFocusPolicy(Qt::StrongFocus);
@@ -124,47 +127,26 @@ Transmission::field_t MainWidget::translateKey(int key)
124 127
125void MainWidget::tick() 128void MainWidget::tick()
126{ 129{
127 if (m_socket == NULL) 130 std::string dataStr;
128 { 131 Util::QByteArrayToStdString(m_socket->readAll(), dataStr);
129 // OLD TEST MODE 132
130 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); 133 //qDebug() << "read str " << dataStr.length();
131 QMapIterator<Color::Color, Actor*> i(m_actors); 134 ProtoBuf::MapUpdate packet;
132 while (i.hasNext()) 135 bool worked = packet.ParseFromString(dataStr);
133 { 136 Q_ASSERT(worked);
134 i.next(); 137 Transmission::map_t map = Util::createUninitialisedMap();
135 i.value()->move(mov); 138 Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
136 QList<QGraphicsItem *> list(i.value()->collidingItems()); 139 int i = 0;
137 for(int j = 0; j < list.count(); ++j) 140 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
138 {
139 if (list.at(j)->parentItem() == i.value())
140 continue;
141 list.at(j)->setOpacity(0.6);
142 }
143 }
144 }
145 else
146 { 141 {
147 std::string dataStr; 142 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
148 Util::QByteArrayToStdString(m_socket->readAll(), dataStr);
149
150 //qDebug() << "read str " << dataStr.length();
151 ProtoBuf::MapUpdate packet;
152 bool worked = packet.ParseFromString(dataStr);
153 Q_ASSERT(worked);
154 Transmission::map_t map = Util::createUninitialisedMap();
155 Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
156 int i = 0;
157 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
158 { 143 {
159 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 144 map[x][y] = packet.field(i);
160 { 145 ++i;
161 map[x][y] = packet.field(i);
162 ++i;
163 }
164 } 146 }
165 updateMap(map);
166 updateScore(packet);
167 } 147 }
148 updateMap(map);
149 updateScore(packet);
168} 150}
169 151
170void MainWidget::keyPressEvent(QKeyEvent* event) 152void MainWidget::keyPressEvent(QKeyEvent* event)
@@ -174,46 +156,34 @@ void MainWidget::keyPressEvent(QKeyEvent* event)
174 156
175 QWidget::keyPressEvent(event); 157 QWidget::keyPressEvent(event);
176 Transmission::field_t newKey = translateKey(event->key()); 158 Transmission::field_t newKey = translateKey(event->key());
177 if (m_currentKey == newKey || newKey == Transmission::direction_none) 159 if (newKey == Transmission::direction_none)
178 return; 160 return;
161 bool sendUpdate = (m_currentKey != newKey);
179 m_currentKey = newKey; 162 m_currentKey = newKey;
163 if (sendUpdate)
164 sendKeyUpdate();
165}
180 166
181 if (m_socket != NULL) 167void MainWidget::sendKeyUpdate()
182 { 168{
183 // send to server 169 if (!m_running)
184 ProtoBuf::KeyPressUpdate packet; 170 return;
185 packet.set_newkey(m_currentKey); 171 if (m_currentKey == Transmission::direction_none)
186 Util::sendPacket(packet, m_socket); 172 return;
187 qDebug() << "send key: " << m_currentKey; 173 qDebug() << "send key: " << m_currentKey;
188 } 174 ProtoBuf::KeyPressUpdate packet;
189 else 175 packet.set_newkey(m_currentKey);
190 { 176 Util::sendPacket(packet, m_socket);
191 // test stuff
192 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);
193 QMapIterator<Color::Color, Actor*> i(m_actors);
194 while (i.hasNext())
195 {
196 i.next();
197 i.value()->move(mov);
198 }
199 }
200} 177}
201 178
202void MainWidget::keyReleaseEvent(QKeyEvent* event) 179void MainWidget::keyReleaseEvent(QKeyEvent* event)
203{ 180{
204 return; // currently not needed
205 if (!m_running) 181 if (!m_running)
206 return; 182 return;
207 183
208 QWidget::keyReleaseEvent(event); 184 QWidget::keyReleaseEvent(event);
209 Transmission::field_t releasedKey = translateKey(event->key()); 185 m_currentKey = Transmission::none;
210 if (releasedKey == m_currentKey) 186 return;
211 {
212 // current key got released
213 // if this is false, a key got released which has already
214 // been replaced by a different key, so this case is disregarded
215 m_currentKey = Transmission::direction_none;
216 }
217} 187}
218 188
219void MainWidget::startGame() 189void MainWidget::startGame()