diff options
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 97 |
1 files changed, 66 insertions, 31 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 343fe4c..399d13c 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -179,21 +179,21 @@ Transmission::map_t createDummyMap() | |||
| 179 | 179 | ||
| 180 | map[15][15] |= Color::red; | 180 | map[15][15] |= Color::red; |
| 181 | map[15][15] |= Transmission::pacman; | 181 | map[15][15] |= Transmission::pacman; |
| 182 | map[15][15] |= Transmission::direction_left; | 182 | map[15][15] |= Transmission::direction_right; |
| 183 | 183 | ||
| 184 | map[16][15] |= Color::blue; | 184 | /*map[16][15] |= Color::blue; |
| 185 | map[16][15] |= Transmission::pacman; | 185 | map[16][15] |= Transmission::pacman; |
| 186 | map[16][15] |= Transmission::direction_left; | 186 | map[16][15] |= Transmission::direction_up; |
| 187 | 187 | ||
| 188 | map[17][15] |= Color::green; | 188 | map[17][15] |= Color::green; |
| 189 | map[17][15] |= Transmission::pacman; | 189 | map[17][15] |= Transmission::pacman; |
| 190 | map[17][15] |= Transmission::direction_left; | 190 | map[17][15] |= Transmission::direction_left;*/ |
| 191 | 191 | ||
| 192 | return map; | 192 | return map; |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | MainWidget::MainWidget() | 195 | MainWidget::MainWidget() |
| 196 | : currentKey(0) | 196 | : m_currentKey(0) |
| 197 | { | 197 | { |
| 198 | visualMap.resize(Constants::map_size.width); | 198 | visualMap.resize(Constants::map_size.width); |
| 199 | for (int i=0; i<visualMap.size(); ++i) { | 199 | for (int i=0; i<visualMap.size(); ++i) { |
| @@ -237,6 +237,7 @@ void MainWidget::createGui() | |||
| 237 | //window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 237 | //window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 238 | //window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 238 | //window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 239 | window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); | 239 | window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); |
| 240 | window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); | ||
| 240 | 241 | ||
| 241 | layout->addLayout(m_scoreLayout); | 242 | layout->addLayout(m_scoreLayout); |
| 242 | layout->addWidget(window); | 243 | layout->addWidget(window); |
| @@ -251,19 +252,19 @@ void MainWidget::updateScore() | |||
| 251 | while (i.hasNext()) | 252 | while (i.hasNext()) |
| 252 | { | 253 | { |
| 253 | i.next(); | 254 | i.next(); |
| 254 | int id = (i.key() >> 1); | 255 | if (i.key() > Color::max) |
| 255 | if (id > (Color::max >> 1)) | ||
| 256 | { | 256 | { |
| 257 | /* player #4 isn't supported in score */ | 257 | /* player #4 isn't supported in score */ |
| 258 | Q_ASSERT(false); | 258 | Q_ASSERT(false); |
| 259 | continue; | 259 | continue; |
| 260 | } | 260 | } |
| 261 | int id = (i.key() >> 1); | ||
| 261 | QLabel *turnPointsLbl = | 262 | QLabel *turnPointsLbl = |
| 262 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); | 263 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); |
| 263 | QLabel *allPointsLbl = | 264 | QLabel *allPointsLbl = |
| 264 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); | 265 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); |
| 265 | turnPointsLbl->setText(QString::number(id * 100 * qrand())); | 266 | turnPointsLbl->setText(QString::number(id)); |
| 266 | allPointsLbl->setText(QString::number(id * 200 * qrand())); | 267 | allPointsLbl->setText(QString::number(id)); |
| 267 | } | 268 | } |
| 268 | } | 269 | } |
| 269 | 270 | ||
| @@ -303,8 +304,9 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 303 | item = new BonusPoint(); | 304 | item = new BonusPoint(); |
| 304 | else if (cur & Transmission::pacman) | 305 | else if (cur & Transmission::pacman) |
| 305 | { | 306 | { |
| 306 | Actor *actor = m_actors.value(color, 0); | 307 | Actor *actor = m_actors.value(color, NULL); |
| 307 | if (actor == 0) { // 0 entspricht NULL ;) | 308 | if (actor == NULL) |
| 309 | { | ||
| 308 | //qDebug() << "new actor of col" << color; | 310 | //qDebug() << "new actor of col" << color; |
| 309 | actor = new Actor(color); | 311 | actor = new Actor(color); |
| 310 | m_actors[color] = actor; | 312 | m_actors[color] = actor; |
| @@ -312,7 +314,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 312 | actor->setPos(mapPositionToCoord(x, y)); | 314 | actor->setPos(mapPositionToCoord(x, y)); |
| 313 | } | 315 | } |
| 314 | 316 | ||
| 315 | Actor::Movement direction; | 317 | Actor::Movement direction = Actor::None; |
| 316 | switch (cur & Transmission::direction_mask) | 318 | switch (cur & Transmission::direction_mask) |
| 317 | { | 319 | { |
| 318 | case Transmission::direction_none: | 320 | case Transmission::direction_none: |
| @@ -333,7 +335,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 333 | default: | 335 | default: |
| 334 | Q_ASSERT(false); | 336 | Q_ASSERT(false); |
| 335 | } | 337 | } |
| 336 | //actor->move(direction, mapPositionToCoord(x, y); | 338 | //actor->move(direction, mapPositionToCoord(x, y)); |
| 337 | } | 339 | } |
| 338 | else | 340 | else |
| 339 | Q_ASSERT(false); | 341 | Q_ASSERT(false); |
| @@ -363,38 +365,71 @@ QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | |||
| 363 | 365 | ||
| 364 | Transmission::field_t MainWidget::translateKey(int key) | 366 | Transmission::field_t MainWidget::translateKey(int key) |
| 365 | { | 367 | { |
| 366 | switch(key) { | 368 | switch(key) |
| 367 | case Qt::Key_Up: | 369 | { |
| 368 | return Transmission::direction_up; | 370 | case Qt::Key_W: |
| 369 | break; | 371 | case Qt::Key_Up: |
| 370 | case Qt::Key_Down: | 372 | return Transmission::direction_up; |
| 371 | return Transmission::direction_down; | 373 | break; |
| 372 | break; | 374 | case Qt::Key_S: |
| 373 | case Qt::Key_Left: | 375 | case Qt::Key_Down: |
| 374 | return Transmission::direction_left; | 376 | return Transmission::direction_down; |
| 375 | break; | 377 | break; |
| 376 | case Qt::Key_Right: | 378 | case Qt::Key_A: |
| 377 | return Transmission::direction_right; | 379 | case Qt::Key_Left: |
| 378 | break; | 380 | return Transmission::direction_left; |
| 379 | default: | 381 | break; |
| 380 | return 0; | 382 | case Qt::Key_D: |
| 383 | case Qt::Key_Right: | ||
| 384 | return Transmission::direction_right; | ||
| 385 | break; | ||
| 386 | default: | ||
| 387 | return 0; | ||
| 381 | } | 388 | } |
| 382 | } | 389 | } |
| 383 | 390 | ||
| 384 | void MainWidget::keyPressEvent(QKeyEvent* event) | 391 | void MainWidget::keyPressEvent(QKeyEvent* event) |
| 385 | { | 392 | { |
| 386 | QWidget::keyPressEvent(event); | 393 | QWidget::keyPressEvent(event); |
| 387 | currentKey = translateKey( event->key() ); | 394 | m_currentKey = translateKey(event->key()); |
| 395 | |||
| 396 | // test stuff | ||
| 397 | Actor::Movement mov = Actor::None; | ||
| 398 | switch(m_currentKey) | ||
| 399 | { | ||
| 400 | case Transmission::direction_up: | ||
| 401 | mov = Actor::Up; | ||
| 402 | break; | ||
| 403 | case Transmission::direction_down: | ||
| 404 | mov = Actor::Down; | ||
| 405 | break; | ||
| 406 | case Transmission::direction_left: | ||
| 407 | mov = Actor::Left; | ||
| 408 | break; | ||
| 409 | case Transmission::direction_right: | ||
| 410 | mov = Actor::Right; | ||
| 411 | break; | ||
| 412 | default: | ||
| 413 | break; | ||
| 414 | } | ||
| 415 | |||
| 416 | QMapIterator<Color::Color, Actor*> i(m_actors); | ||
| 417 | while (i.hasNext()) | ||
| 418 | { | ||
| 419 | i.next(); | ||
| 420 | i.value()->move(mov); | ||
| 421 | } | ||
| 388 | } | 422 | } |
| 389 | 423 | ||
| 390 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | 424 | void MainWidget::keyReleaseEvent(QKeyEvent* event) |
| 391 | { | 425 | { |
| 392 | QWidget::keyReleaseEvent(event); | 426 | QWidget::keyReleaseEvent(event); |
| 393 | Transmission::field_t releasedKey = translateKey(event->key()); | 427 | Transmission::field_t releasedKey = translateKey(event->key()); |
| 394 | if (releasedKey == currentKey) { | 428 | if (releasedKey == m_currentKey) |
| 429 | { | ||
| 395 | // current key got released | 430 | // current key got released |
| 396 | // if this is false, a key got released which has already | 431 | // if this is false, a key got released which has already |
| 397 | // been replaced by a different key, so this case is disregarded | 432 | // been replaced by a different key, so this case is disregarded |
| 398 | currentKey = 0; | 433 | m_currentKey = Transmission::direction_none; |
| 399 | } | 434 | } |
| 400 | } | 435 | } |
