summaryrefslogtreecommitdiffstats
path: root/pacman-c++
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++
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++')
-rw-r--r--pacman-c++/actor.cpp27
-rw-r--r--pacman-c++/mainwidget.cpp120
-rw-r--r--pacman-c++/mainwidget.h4
-rw-r--r--pacman-c++/pacman.server.pro2
-rw-r--r--pacman-c++/sceneholder.cpp4
-rw-r--r--pacman-c++/server.cpp19
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()
118 118
119void Actor::move(Actor::Movement direction) 119void Actor::move(Actor::Movement direction)
120{ 120{
121 /*if (Constants::server) 121 if (Constants::server)
122 return moveByServer(direction);*/ 122 return moveByServer(direction);
123 123
124 /* stop current animation */ 124 /* stop current animation */
125 if (direction != m_direction) 125 if (direction != m_direction)
@@ -231,5 +231,28 @@ void Actor::finishRound()
231void Actor::moveByServer(Actor::Movement direction) 231void Actor::moveByServer(Actor::Movement direction)
232{ 232{
233 qDebug() << "move by server"; 233 qDebug() << "move by server";
234
235 QPointF endpos(0, 0);
236 switch(direction)
237 {
238 case Actor::None:
239 break;
240 case Actor::Left:
241 endpos.setX(static_cast<qreal>(Constants::field_size.width) * -1);
242 break;
243 case Actor::Right:
244 endpos.setX(Constants::field_size.width);
245 break;
246 case Actor::Up:
247 endpos.setY(static_cast<qreal>(Constants::field_size.height) * -1);
248 break;
249 case Actor::Down:
250 endpos.setY(Constants::field_size.height);
251 break;
252 default:
253 Q_ASSERT(false);
254 break;
255 }
256 setPos(pos() + endpos);
234 m_direction = direction; 257 m_direction = direction;
235} 258}
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()
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
18 18
19public: 19public:
20 MainWidget(QWidget *parent = 0); 20 MainWidget(QWidget *parent = 0);
21 bool connected();
21 22
22protected: 23protected:
23 // handling of current key 24 /* handling of current key */
24 virtual void keyPressEvent(QKeyEvent* ); 25 virtual void keyPressEvent(QKeyEvent* );
25 virtual void keyReleaseEvent(QKeyEvent* ); 26 virtual void keyReleaseEvent(QKeyEvent* );
26 27
@@ -28,6 +29,7 @@ private slots:
28 void startGame(); 29 void startGame();
29 void playerScoreClicked(); 30 void playerScoreClicked();
30 void tick(); 31 void tick();
32 void sendKeyUpdate();
31 33
32private: 34private:
33 void createGui(); 35 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 \
12 block.cpp \ 12 block.cpp \
13 server.cpp \ 13 server.cpp \
14 bonuspoint.cpp \ 14 bonuspoint.cpp \
15 mainwidget.cpp \
16 point.cpp \ 15 point.cpp \
17 audio.cpp \ 16 audio.cpp \
18 sceneholder.cpp \ 17 sceneholder.cpp \
@@ -25,7 +24,6 @@ HEADERS += pixmapitem.h \
25 block.h \ 24 block.h \
26 server.h \ 25 server.h \
27 bonuspoint.h \ 26 bonuspoint.h \
28 mainwidget.h \
29 constants.h \ 27 constants.h \
30 point.h \ 28 point.h \
31 audio.h \ 29 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)
100 } 100 }
101 else if (cur & Transmission::empty) 101 else if (cur & Transmission::empty)
102 { 102 {
103 // already handled 103 /* already handled */
104 } 104 }
105 else 105 else
106 { 106 {
107 qDebug() << "abort at " << cur; 107 qWarning() << "Unknown data value at" << cur;
108 Q_ASSERT(false); 108 Q_ASSERT(false);
109 } 109 }
110 110
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()
43 while (i.hasNext()) 43 while (i.hasNext())
44 { 44 {
45 i.next(); 45 i.next();
46 int turn = 0;
47
48invalid_direction:
49 ++turn;
46 Actor *actor = m_actors.value(i.key()); 50 Actor *actor = m_actors.value(i.key());
47 QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); 51 QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint());
48 qDebug() << "actor " << i.key() << " is at " << mapPosition << "moving " << i.value(); 52 qDebug() << "actor " << i.key() << " is at " << mapPosition << "moving " << i.value();
@@ -108,8 +112,21 @@ Transmission::map_t Server::calculateUpdates()
108 } 112 }
109 // </t3h g4m2 10gic> 113 // </t3h g4m2 10gic>
110 114
115 /* movement didn't work - e.g. was blocked */
111 if (mapPosition == newMapPosition) 116 if (mapPosition == newMapPosition)
112 m_actorMovements[i.key()] = Actor::None; 117 {
118 if (turn == 1)
119 {
120 /* set direction back to last known direction and try again */
121 m_actorMovements[i.key()] = actor->direction();
122 goto invalid_direction;
123 }
124 else
125 {
126 /* second turn didn't work too -> stop movement */
127 m_actorMovements[i.key()] = Actor::None;
128 }
129 }
113 130
114 map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | 131 map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman |
115 i.key() | Util::actorMovementToTransmission(i.value()); 132 i.key() | Util::actorMovementToTransmission(i.value());