#include "mainwidget.h" #include "actor.h" #include "block.h" #include "bonuspoint.h" #include "point.h" #include "constants.h" #include "audioplayer.h" // temporary Transmission::map_t createDummyMap() { Transmission::map_t map; map = new Transmission::field_t*[Constants::map_size.width]; for (unsigned int i = 0; i < Constants::map_size.width; ++i) map[i] = new Transmission::field_t[Constants::map_size.height]; for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; cur = Transmission::none; } } const char *tmpl[] = { " # # ", " #### ###### # #### # # ###### ### ", " # # ", " # ##### # # # # # ### # # # ", " # # # # # # # # # # ## # # ", " # # # # # # # # ### # # # # ", " # # # # # # # # # # # # ## # ", " # # ### ##### # ### # # # ", " ### # ", " # # ### #### #### #### ##### ", " #### # #..# #..# #..# # # ", " # # ### #..# #..# #### # # # # ", " # # # #..# #..# # # ", " # #### # #### #### # # ##### # ", " # # ", " #### ###### # ##### # ####### ### ", " # # " }; for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; cur = Transmission::none; if (tmpl[y][x] == '#') cur |= Color::none | Transmission::block; /* this is a simple hack to create areas where no * autoplaced points will be placed (see below) */ else if (tmpl[y][x] == '.') cur |= Transmission::point; } } map[0][0] |= Transmission::bonuspoint; map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; /* auto place normal points*/ for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; if (cur == Transmission::none) cur |= Transmission::point; else if (cur == Transmission::point) cur = Transmission::none; } } return map; } MainWidget::MainWidget() : m_currentKey(0), m_running(false) { visualMap.resize(Constants::map_size.width); for (int i=0; iplay(AudioPlayer::Intro); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(tick())); timer->start(100); startGame(); } void MainWidget::createGui() { setFocusPolicy(Qt::StrongFocus); QVBoxLayout *layout = new QVBoxLayout(this); QHBoxLayout *scoreLayout = new QHBoxLayout(); for (unsigned int i = 1; i < 4; ++i) { QGroupBox *scoreBox = new QGroupBox(QString("Spieler %1").arg(i), this); scoreBox->setObjectName(QString("actor%1").arg(i)); scoreBox->setCheckable(true); connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked())); scoreLayout->addWidget(scoreBox); QGridLayout *playerLayout = new QGridLayout(); scoreBox->setLayout(playerLayout); playerLayout->addWidget(new QLabel("Rundenpunkte:", this), 0, 0); playerLayout->addWidget(new QLabel("Gesamtpunkte:", this), 1, 0); playerLayout->addWidget(new QLabel("", this), 0, 1); playerLayout->addWidget(new QLabel("", this), 1, 1); m_playerScoreLayouts.append(playerLayout); } m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); m_scene->setBackgroundBrush(Qt::black); QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); window->setAlignment(Qt::AlignLeft | Qt::AlignTop); window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); layout->addLayout(scoreLayout); layout->addWidget(window); QFile css(":/stylesheet"); css.open(QFile::ReadOnly); qApp->setStyleSheet(QLatin1String(css.readAll())); setLayout(layout); } void MainWidget::updateScore() { QMapIterator i(m_actors); while (i.hasNext()) { i.next(); if (i.key() > Color::max) { /* player #4 isn't supported in score */ Q_ASSERT(false); continue; } int id = (i.key() >> 1); QLabel *turnPointsLbl = dynamic_cast(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); QLabel *allPointsLbl = dynamic_cast(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); turnPointsLbl->setText(QString::number(id)); allPointsLbl->setText(QString::number(id)); } } void MainWidget::updateMap(const Transmission::map_t& map) { for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { const Transmission::field_t &cur = map[x][y]; if (cur == Transmission::none) continue; //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; Color::Color color = static_cast(cur & Transmission::color_mask); //qDebug() << "col=" << color; PixmapItem* item = NULL; if (cur & Transmission::block) { unsigned int neighbours = Block::None; // check left side if (x > 0 && map[x - 1][y] & Transmission::block) neighbours |= Block::Left; // check right side if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) neighbours |= Block::Right; // check upside if (y > 0 && map[x][y - 1] & Transmission::block) neighbours |= Block::Up; // check down side if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) neighbours |= Block::Down; item = new Block(color, neighbours); } else if (cur & Transmission::bonuspoint) item = new BonusPoint(); else if (cur & Transmission::point) item = new Point(); else if (cur & Transmission::pacman) { Actor *actor = m_actors.value(color, NULL); if (actor == NULL) { //qDebug() << "new actor of col" << color; actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing m_actors[color] = actor; m_scene->addItem(actor); actor->setPos(mapPositionToCoord(x, y)); } Actor::Movement direction = Actor::None; switch (cur & Transmission::direction_mask) { case Transmission::direction_none: direction = Actor::None; break; case Transmission::direction_left: direction = Actor::Left; break; case Transmission::direction_right: direction = Actor::Right; break; case Transmission::direction_up: direction = Actor::Up; break; case Transmission::direction_down: direction = Actor::Down; break; default: Q_ASSERT(false); } //actor->move(direction, mapPositionToCoord(x, y)); } else Q_ASSERT(false); if (item != NULL) { m_scene->addItem(item); item->setPos(mapPositionToCoord(x, y)); PixmapItem* oldItem = visualMap[x][y]; visualMap[x][y] = item; if (oldItem != NULL) { m_scene->removeItem(item); delete oldItem; } } } } updateScore(); } QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) { return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); } Transmission::field_t MainWidget::translateKey(int key) { switch(key) { case Qt::Key_W: case Qt::Key_Up: return Transmission::direction_up; break; case Qt::Key_S: case Qt::Key_Down: return Transmission::direction_down; break; case Qt::Key_A: case Qt::Key_Left: return Transmission::direction_left; break; case Qt::Key_D: case Qt::Key_Right: return Transmission::direction_right; break; default: return 0; } } void MainWidget::tick() { Actor::Movement mov = Actor::None; switch(m_currentKey) { case Transmission::direction_up: mov = Actor::Up; break; case Transmission::direction_down: mov = Actor::Down; break; case Transmission::direction_left: mov = Actor::Left; break; case Transmission::direction_right: mov = Actor::Right; break; default: break; } QMapIterator i(m_actors); while (i.hasNext()) { i.next(); i.value()->move(mov); } } void MainWidget::keyPressEvent(QKeyEvent* event) { if (!m_running) return; QWidget::keyPressEvent(event); m_currentKey = translateKey(event->key()); return; // test stuff Actor::Movement mov = Actor::None; switch(m_currentKey) { case Transmission::direction_up: mov = Actor::Up; break; case Transmission::direction_down: mov = Actor::Down; break; case Transmission::direction_left: mov = Actor::Left; break; case Transmission::direction_right: mov = Actor::Right; break; default: break; } QMapIterator i(m_actors); while (i.hasNext()) { i.next(); i.value()->move(mov); } } void MainWidget::keyReleaseEvent(QKeyEvent* event) { if (!m_running) return; QWidget::keyReleaseEvent(event); Transmission::field_t releasedKey = translateKey(event->key()); if (releasedKey == m_currentKey) { // current key got released // if this is false, a key got released which has already // been replaced by a different key, so this case is disregarded m_currentKey = Transmission::direction_none; } } void MainWidget::startGame() { m_running = true; } void MainWidget::playerScoreClicked() { QGroupBox *tmp = qobject_cast(sender()); tmp->setChecked(true); return; }