diff options
| author | manuel <manuel@mausz.at> | 2011-04-06 15:15:03 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-06 15:15:03 +0200 |
| commit | cbc0da21f393dc7bf2a11beaf1314c9dcaeb0999 (patch) | |
| tree | 534fee860ef8f0064425f98bbbb1febb7335f820 /pacman-c++/mainwidget.cpp | |
| parent | 2b3210e6d19e32b7746e0e1cdf22035ce508ca8a (diff) | |
| parent | dce91729ff119ce6e00340f67297ca9e3e72110c (diff) | |
| download | foop-cbc0da21f393dc7bf2a11beaf1314c9dcaeb0999.tar.gz foop-cbc0da21f393dc7bf2a11beaf1314c9dcaeb0999.tar.bz2 foop-cbc0da21f393dc7bf2a11beaf1314c9dcaeb0999.zip | |
Merge branch 'master' of ssh://manuel.mausz.at/foop
Conflicts:
pacman-c++/actor.cpp
pacman-c++/constants.h
pacman-c++/mainwidget.cpp
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 162 |
1 files changed, 83 insertions, 79 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 062fc6d..fe1ba9c 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -5,9 +5,63 @@ | |||
| 5 | #include "bonuspoint.h" | 5 | #include "bonuspoint.h" |
| 6 | #include "constants.h" | 6 | #include "constants.h" |
| 7 | 7 | ||
| 8 | // temporary | ||
| 9 | Transmission::map_t createDummyMap() | ||
| 10 | { | ||
| 11 | Transmission::map_t map; | ||
| 12 | map = new Transmission::field_t*[Constants::map_size.width]; | ||
| 13 | for (unsigned int i = 0; i < Constants::map_size.width; ++i) | ||
| 14 | map[i] = new Transmission::field_t[Constants::map_size.height]; | ||
| 15 | |||
| 16 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 17 | { | ||
| 18 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 19 | { | ||
| 20 | Transmission::field_t &cur = map[x][y]; | ||
| 21 | cur = 0; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | map[0][0] |= Color::none ^ Transmission::block; | ||
| 26 | map[0][1] |= Color::none ^ Transmission::block; | ||
| 27 | map[0][2] |= Color::none ^ Transmission::block; | ||
| 28 | map[0][3] |= Color::none ^ Transmission::block; | ||
| 29 | map[0][4] |= Color::none ^ Transmission::block; | ||
| 30 | map[0][5] |= Color::none ^ Transmission::block; | ||
| 31 | map[1][0] |= Color::none ^ Transmission::block; | ||
| 32 | map[2][0] |= Color::none ^ Transmission::block; | ||
| 33 | map[1][3] |= Color::none ^ Transmission::block; | ||
| 34 | map[2][3] |= Color::none ^ Transmission::block; | ||
| 35 | map[3][3] |= Color::none ^ Transmission::block; | ||
| 36 | map[2][4] |= Color::none ^ Transmission::block; | ||
| 37 | map[2][5] |= Color::none ^ Transmission::block; | ||
| 38 | map[2][2] |= Color::none ^ Transmission::block; | ||
| 39 | map[2][1] |= Color::none ^ Transmission::block; | ||
| 40 | |||
| 41 | map[4][3] |= Color::green ^ Transmission::block; | ||
| 42 | |||
| 43 | map[5][3] |= Color::none ^ Transmission::block; | ||
| 44 | map[6][3] |= Color::none ^ Transmission::block; | ||
| 45 | map[7][3] |= Color::red ^ Transmission::block; | ||
| 46 | |||
| 47 | map[7][5] |= Transmission::bonuspoint; | ||
| 48 | |||
| 49 | map[5][5] |= Color::blue; | ||
| 50 | map[5][5] |= Transmission::pacman; | ||
| 51 | map[5][5] |= Transmission::direction_left; | ||
| 52 | |||
| 53 | return map; | ||
| 54 | } | ||
| 55 | |||
| 8 | MainWidget::MainWidget() | 56 | MainWidget::MainWidget() |
| 9 | : currentKey(0) | 57 | : currentKey(0) |
| 10 | { | 58 | { |
| 59 | createGui(); | ||
| 60 | updateMap(createDummyMap()); | ||
| 61 | } | ||
| 62 | |||
| 63 | void MainWidget::createGui() | ||
| 64 | { | ||
| 11 | setFocusPolicy(Qt::StrongFocus); | 65 | setFocusPolicy(Qt::StrongFocus); |
| 12 | 66 | ||
| 13 | QVBoxLayout *layout = new QVBoxLayout(this); | 67 | QVBoxLayout *layout = new QVBoxLayout(this); |
| @@ -30,24 +84,23 @@ MainWidget::MainWidget() | |||
| 30 | m_playerScoreLayouts.append(playerLayout); | 84 | m_playerScoreLayouts.append(playerLayout); |
| 31 | } | 85 | } |
| 32 | 86 | ||
| 33 | m_scene = new QGraphicsScene(0, 0, 500, 500, this); | 87 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); |
| 34 | m_scene->setBackgroundBrush(Qt::black); | 88 | m_scene->setBackgroundBrush(Qt::black); |
| 35 | 89 | ||
| 36 | QGraphicsView *window = new QGraphicsView(m_scene, this); | 90 | QGraphicsView *window = new QGraphicsView(m_scene, this); |
| 37 | window->setFrameStyle(0); | 91 | window->setFrameStyle(0); |
| 38 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); | 92 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
| 39 | window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 93 | //window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 40 | window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 94 | //window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 41 | 95 | ||
| 42 | layout->addLayout(m_scoreLayout); | 96 | layout->addLayout(m_scoreLayout); |
| 43 | layout->addWidget(window); | 97 | layout->addWidget(window); |
| 44 | 98 | ||
| 45 | setLayout(layout); | 99 | setLayout(layout); |
| 46 | setWindowTitle("pacman client"); | 100 | setWindowTitle("pacman client"); |
| 47 | |||
| 48 | loadDummyMap(); | ||
| 49 | } | 101 | } |
| 50 | 102 | ||
| 103 | |||
| 51 | void MainWidget::updateScore() | 104 | void MainWidget::updateScore() |
| 52 | { | 105 | { |
| 53 | QMapIterator<Color::Color, Actor*> i(m_actors); | 106 | QMapIterator<Color::Color, Actor*> i(m_actors); |
| @@ -66,91 +119,42 @@ void MainWidget::updateScore() | |||
| 66 | } | 119 | } |
| 67 | } | 120 | } |
| 68 | 121 | ||
| 69 | // temporary | ||
| 70 | transmission::map_t createDummyMap() | ||
| 71 | { | ||
| 72 | transmission::map_t map; | ||
| 73 | map = new transmission::field_t*[map_size[0]]; | ||
| 74 | for (unsigned int i = 0; i < map_size[0]; ++i) | ||
| 75 | map[i] = new transmission::field_t[map_size[1]]; | ||
| 76 | |||
| 77 | for (unsigned int x = 0; x < map_size[0]; ++x) | ||
| 78 | { | ||
| 79 | for (unsigned int y = 0; y < map_size[1]; ++y) | ||
| 80 | { | ||
| 81 | transmission::field_t &cur = map[x][y]; | ||
| 82 | cur = 0; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | 122 | ||
| 86 | map[0][0] |= Color::none ^ transmission::block; | 123 | void MainWidget::updateMap(const Transmission::map_t& map) |
| 87 | map[0][1] |= Color::none ^ transmission::block; | ||
| 88 | map[0][2] |= Color::none ^ transmission::block; | ||
| 89 | map[0][3] |= Color::none ^ transmission::block; | ||
| 90 | map[0][4] |= Color::none ^ transmission::block; | ||
| 91 | map[0][5] |= Color::none ^ transmission::block; | ||
| 92 | map[1][0] |= Color::none ^ transmission::block; | ||
| 93 | map[2][0] |= Color::none ^ transmission::block; | ||
| 94 | map[1][3] |= Color::none ^ transmission::block; | ||
| 95 | map[2][3] |= Color::none ^ transmission::block; | ||
| 96 | map[3][3] |= Color::none ^ transmission::block; | ||
| 97 | map[2][4] |= Color::none ^ transmission::block; | ||
| 98 | map[2][5] |= Color::none ^ transmission::block; | ||
| 99 | map[2][2] |= Color::none ^ transmission::block; | ||
| 100 | map[2][1] |= Color::none ^ transmission::block; | ||
| 101 | |||
| 102 | map[4][3] |= Color::green ^ transmission::block; | ||
| 103 | |||
| 104 | map[5][3] |= Color::none ^ transmission::block; | ||
| 105 | map[6][3] |= Color::none ^ transmission::block; | ||
| 106 | map[7][3] |= Color::red ^ transmission::block; | ||
| 107 | |||
| 108 | map[7][5] |= transmission::bonuspoint; | ||
| 109 | |||
| 110 | map[5][5] |= Color::blue; | ||
| 111 | map[5][5] |= transmission::pacman; | ||
| 112 | map[5][5] |= transmission::direction_left; | ||
| 113 | |||
| 114 | return map; | ||
| 115 | } | ||
| 116 | |||
| 117 | void MainWidget::loadDummyMap() | ||
| 118 | { | 124 | { |
| 119 | transmission::map_t map = createDummyMap(); | 125 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) |
| 120 | |||
| 121 | for (unsigned int x = 0; x < map_size[0]; ++x) | ||
| 122 | { | 126 | { |
| 123 | for (unsigned int y = 0; y < map_size[1]; ++y) | 127 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) |
| 124 | { | 128 | { |
| 125 | const transmission::field_t &cur = map[x][y]; | 129 | const Transmission::field_t &cur = map[x][y]; |
| 126 | if (cur == 0) | 130 | if (cur == 0) |
| 127 | continue; | 131 | continue; |
| 128 | qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; | 132 | qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; |
| 129 | 133 | ||
| 130 | Color::Color color = static_cast<Color::Color>(cur & transmission::color_mask); | 134 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); |
| 131 | qDebug() << "col=" << color; | 135 | qDebug() << "col=" << color; |
| 132 | 136 | ||
| 133 | PixmapItem *item = NULL; | 137 | PixmapItem *item = NULL; |
| 134 | if (cur & transmission::block) | 138 | if (cur & Transmission::block) |
| 135 | { | 139 | { |
| 136 | unsigned int neighbours = Block::None; | 140 | unsigned int neighbours = Block::None; |
| 137 | // check left side | 141 | // check left side |
| 138 | if (x > 0 && map[x - 1][y] & transmission::block) | 142 | if (x > 0 && map[x - 1][y] & Transmission::block) |
| 139 | neighbours |= Block::Left; | 143 | neighbours |= Block::Left; |
| 140 | // check right side | 144 | // check right side |
| 141 | if (x < map_size[0] && map[x + 1][y] & transmission::block) | 145 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) |
| 142 | neighbours |= Block::Right; | 146 | neighbours |= Block::Right; |
| 143 | // check upside | 147 | // check upside |
| 144 | if (y > 0 && map[x][y - 1] & transmission::block) | 148 | if (y > 0 && map[x][y - 1] & Transmission::block) |
| 145 | neighbours |= Block::Up; | 149 | neighbours |= Block::Up; |
| 146 | // check down side | 150 | // check down side |
| 147 | if (y < map_size[1] && map[x][y + 1] & transmission::block) | 151 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) |
| 148 | neighbours |= Block::Down; | 152 | neighbours |= Block::Down; |
| 149 | item = new Block(color, neighbours); | 153 | item = new Block(color, neighbours); |
| 150 | } | 154 | } |
| 151 | else if (cur & transmission::bonuspoint) | 155 | else if (cur & Transmission::bonuspoint) |
| 152 | item = new BonusPoint(); | 156 | item = new BonusPoint(); |
| 153 | else if (cur & transmission::pacman) | 157 | else if (cur & Transmission::pacman) |
| 154 | { | 158 | { |
| 155 | Actor *actor = m_actors.value(color, 0); | 159 | Actor *actor = m_actors.value(color, 0); |
| 156 | if (actor == 0) { // 0 entspricht NULL ;) | 160 | if (actor == 0) { // 0 entspricht NULL ;) |
| @@ -162,21 +166,21 @@ void MainWidget::loadDummyMap() | |||
| 162 | } | 166 | } |
| 163 | 167 | ||
| 164 | Actor::Movement direction; | 168 | Actor::Movement direction; |
| 165 | switch (cur & transmission::direction_mask) | 169 | switch (cur & Transmission::direction_mask) |
| 166 | { | 170 | { |
| 167 | case transmission::direction_none: | 171 | case Transmission::direction_none: |
| 168 | direction = Actor::None; | 172 | direction = Actor::None; |
| 169 | break; | 173 | break; |
| 170 | case transmission::direction_left: | 174 | case Transmission::direction_left: |
| 171 | direction = Actor::Left; | 175 | direction = Actor::Left; |
| 172 | break; | 176 | break; |
| 173 | case transmission::direction_right: | 177 | case Transmission::direction_right: |
| 174 | direction = Actor::Right; | 178 | direction = Actor::Right; |
| 175 | break; | 179 | break; |
| 176 | case transmission::direction_up: | 180 | case Transmission::direction_up: |
| 177 | direction = Actor::Up; | 181 | direction = Actor::Up; |
| 178 | break; | 182 | break; |
| 179 | case transmission::direction_down: | 183 | case Transmission::direction_down: |
| 180 | direction = Actor::Down; | 184 | direction = Actor::Down; |
| 181 | break; | 185 | break; |
| 182 | default: | 186 | default: |
| @@ -199,23 +203,23 @@ void MainWidget::loadDummyMap() | |||
| 199 | 203 | ||
| 200 | QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | 204 | QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) |
| 201 | { | 205 | { |
| 202 | return QPoint(x * field_size[0], y * field_size[1]); | 206 | return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); |
| 203 | } | 207 | } |
| 204 | 208 | ||
| 205 | transmission::field_t MainWidget::translateKey(int key) | 209 | Transmission::field_t MainWidget::translateKey(int key) |
| 206 | { | 210 | { |
| 207 | switch(key) { | 211 | switch(key) { |
| 208 | case Qt::Key_Up: | 212 | case Qt::Key_Up: |
| 209 | return transmission::direction_up; | 213 | return Transmission::direction_up; |
| 210 | break; | 214 | break; |
| 211 | case Qt::Key_Down: | 215 | case Qt::Key_Down: |
| 212 | return transmission::direction_down; | 216 | return Transmission::direction_down; |
| 213 | break; | 217 | break; |
| 214 | case Qt::Key_Left: | 218 | case Qt::Key_Left: |
| 215 | return transmission::direction_left; | 219 | return Transmission::direction_left; |
| 216 | break; | 220 | break; |
| 217 | case Qt::Key_Right: | 221 | case Qt::Key_Right: |
| 218 | return transmission::direction_right; | 222 | return Transmission::direction_right; |
| 219 | break; | 223 | break; |
| 220 | default: | 224 | default: |
| 221 | return 0; | 225 | return 0; |
| @@ -231,7 +235,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 231 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | 235 | void MainWidget::keyReleaseEvent(QKeyEvent* event) |
| 232 | { | 236 | { |
| 233 | QWidget::keyReleaseEvent(event); | 237 | QWidget::keyReleaseEvent(event); |
| 234 | transmission::field_t releasedKey = translateKey(event->key()); | 238 | Transmission::field_t releasedKey = translateKey(event->key()); |
| 235 | if (releasedKey == currentKey) { | 239 | if (releasedKey == currentKey) { |
| 236 | // current key got released | 240 | // current key got released |
| 237 | // if this is false, a key got released which has already | 241 | // if this is false, a key got released which has already |
