summaryrefslogtreecommitdiffstats
path: root/pacman-c++/mainwidget.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-10 21:34:29 +0200
committermanuel <manuel@mausz.at>2011-04-10 21:34:29 +0200
commit817dcc4e4493fb89c6d52b5e5ce1548b6a047d08 (patch)
tree81f0eea0617d05604e6636cb020509f9ec504e6e /pacman-c++/mainwidget.cpp
parentd0eafb0124a39eeda6c00595a943ce9811d589c4 (diff)
downloadfoop-817dcc4e4493fb89c6d52b5e5ce1548b6a047d08.tar.gz
foop-817dcc4e4493fb89c6d52b5e5ce1548b6a047d08.tar.bz2
foop-817dcc4e4493fb89c6d52b5e5ce1548b6a047d08.zip
- first round of coding cleanup
- small sound fix
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
-rw-r--r--pacman-c++/mainwidget.cpp57
1 files changed, 32 insertions, 25 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index ebd0f02..f5d4b7f 100644
--- a/pacman-c++/mainwidget.cpp
+++ b/pacman-c++/mainwidget.cpp
@@ -10,26 +10,27 @@ 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::createDemoMap());
14
15 //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
16 //AudioPlayer::self()->play(AudioPlayer::Intro);
17 14
18 Color::Color myColor = connectToServer(); 15 Color::Color myColor = connectToServer();
19 if (myColor == Color::none) { 16 if (myColor == Color::none)
17 {
20 QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); 18 QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode");
21 // TODO: quit application here or sth 19 // TODO: quit application here or sth
22 m_socket = NULL; 20 m_socket = NULL;
23 QTimer *timer = new QTimer(this); 21 QTimer *timer = new QTimer(this);
24 connect(timer, SIGNAL(timeout()), this, SLOT(tick())); 22 connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
25 timer->start(Constants::tick); 23 timer->start(Constants::tick);
26 } else {
27 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick()));
28 } 24 }
25 else
26 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick()));
29 27
30 // TODO: use mycolor 28 // TODO: use mycolor
31 qDebug() << "myc" << myColor; 29 qDebug() << "mycolor=" << myColor;
32 30
31 //TODO: play intro as soon as there are enough players
32 //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
33 //AudioPlayer::self()->play(AudioPlayer::Intro);
33 startGame(); 34 startGame();
34} 35}
35 36
@@ -82,6 +83,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet)
82 QLabel *turnPointsLbl; 83 QLabel *turnPointsLbl;
83 QLabel *allPointsLbl; 84 QLabel *allPointsLbl;
84 85
86 //TODO: change proto and move that inside an array
85 turnPointsLbl = dynamic_cast<QLabel*>(m_playerScoreLayouts.at(0)->itemAtPosition(0,1)->widget()); 87 turnPointsLbl = dynamic_cast<QLabel*>(m_playerScoreLayouts.at(0)->itemAtPosition(0,1)->widget());
86 allPointsLbl = dynamic_cast<QLabel*>(m_playerScoreLayouts.at(0)->itemAtPosition(1,1)->widget()); 88 allPointsLbl = dynamic_cast<QLabel*>(m_playerScoreLayouts.at(0)->itemAtPosition(1,1)->widget());
87 turnPointsLbl->setText(QString::number(packet.round_points_red())); 89 turnPointsLbl->setText(QString::number(packet.round_points_red()));
@@ -98,7 +100,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet)
98 allPointsLbl->setText(QString::number(packet.game_points_green())); 100 allPointsLbl->setText(QString::number(packet.game_points_green()));
99} 101}
100 102
101Transmission::field_t MainWidget::translateKey(int key, int def) 103Transmission::field_t MainWidget::translateKey(int key)
102{ 104{
103 switch(key) 105 switch(key)
104 { 106 {
@@ -119,16 +121,16 @@ Transmission::field_t MainWidget::translateKey(int key, int def)
119 return Transmission::direction_right; 121 return Transmission::direction_right;
120 break; 122 break;
121 default: 123 default:
122 return def; 124 return Transmission::direction_none;
123 } 125 }
124} 126}
125 127
126void MainWidget::tick() 128void MainWidget::tick()
127{ 129{
128 if (m_socket == NULL) { 130 if (m_socket == NULL)
131 {
129 // OLD TEST MODE 132 // OLD TEST MODE
130 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); 133 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);
131
132 QMapIterator<Color::Color, Actor*> i(m_actors); 134 QMapIterator<Color::Color, Actor*> i(m_actors);
133 while (i.hasNext()) 135 while (i.hasNext())
134 { 136 {
@@ -142,7 +144,9 @@ void MainWidget::tick()
142 list.at(j)->setOpacity(0.6); 144 list.at(j)->setOpacity(0.6);
143 } 145 }
144 } 146 }
145 } else { 147 }
148 else
149 {
146 std::string dataStr; 150 std::string dataStr;
147 Util::QByteArrayToStdString(m_socket->readAll(), dataStr); 151 Util::QByteArrayToStdString(m_socket->readAll(), dataStr);
148 152
@@ -152,8 +156,10 @@ void MainWidget::tick()
152 Transmission::map_t map = Util::createUninitialisedMap(); 156 Transmission::map_t map = Util::createUninitialisedMap();
153 Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height)); 157 Q_ASSERT(packet.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
154 int i = 0; 158 int i = 0;
155 for (unsigned int x = 0; x < Constants::map_size.width; ++x) { 159 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
156 for (unsigned int y = 0; y < Constants::map_size.height; ++y) { 160 {
161 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
162 {
157 map[x][y] = packet.field(i); 163 map[x][y] = packet.field(i);
158 ++i; 164 ++i;
159 } 165 }
@@ -169,23 +175,23 @@ void MainWidget::keyPressEvent(QKeyEvent* event)
169 return; 175 return;
170 176
171 QWidget::keyPressEvent(event); 177 QWidget::keyPressEvent(event);
172 Transmission::field_t newKey = translateKey(event->key(), -1); 178 Transmission::field_t newKey = translateKey(event->key());
173 if (m_currentKey == newKey || newKey == -1) { 179 if (m_currentKey == newKey || newKey == Transmission::direction_none)
174 return; 180 return;
175 }
176 m_currentKey = newKey; 181 m_currentKey = newKey;
177 182
178 if (m_socket != NULL) { 183 if (m_socket != NULL)
184 {
179 // send to server 185 // send to server
180 ProtoBuf::KeyPressUpdate packet; 186 ProtoBuf::KeyPressUpdate packet;
181 packet.set_newkey(m_currentKey); 187 packet.set_newkey(m_currentKey);
182 Util::sendPacket(packet, m_socket); 188 Util::sendPacket(packet, m_socket);
183 qDebug() << "send key: " << m_currentKey; 189 qDebug() << "send key: " << m_currentKey;
184 } else { 190 }
185 191 else
192 {
186 // test stuff 193 // test stuff
187 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); 194 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);
188
189 QMapIterator<Color::Color, Actor*> i(m_actors); 195 QMapIterator<Color::Color, Actor*> i(m_actors);
190 while (i.hasNext()) 196 while (i.hasNext())
191 { 197 {
@@ -229,11 +235,13 @@ Color::Color MainWidget::connectToServer()
229 m_socket = new QTcpSocket(this); 235 m_socket = new QTcpSocket(this);
230 m_socket->connectToHost("127.0.0.1", Constants::port); 236 m_socket->connectToHost("127.0.0.1", Constants::port);
231 bool worked = m_socket->waitForConnected(Constants::connection_timeout); 237 bool worked = m_socket->waitForConnected(Constants::connection_timeout);
232 if (worked) { 238 if (worked)
239 {
233 // additional init 240 // additional init
234 // first packet is our color 241 // first packet is our color
235 worked = m_socket->waitForReadyRead(); 242 worked = m_socket->waitForReadyRead();
236 if (worked) { 243 if (worked)
244 {
237 std::string data; 245 std::string data;
238 Util::QByteArrayToStdString(m_socket->readAll(), data); 246 Util::QByteArrayToStdString(m_socket->readAll(), data);
239 ProtoBuf::WhoAmI packet; 247 ProtoBuf::WhoAmI packet;
@@ -243,4 +251,3 @@ Color::Color MainWidget::connectToServer()
243 } 251 }
244 return Color::none; 252 return Color::none;
245} 253}
246