diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-09 13:54:18 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-09 13:54:18 +0200 |
| commit | d547dec802f76c346538144f4eacf6d8ca6310c4 (patch) | |
| tree | 50613653be09988bfd1a122496faed93c960de45 /pacman-c++/mainwidget.cpp | |
| parent | 7e4bc8ece49543533c4e27f5d6bc867c1bada601 (diff) | |
| download | foop-d547dec802f76c346538144f4eacf6d8ca6310c4.tar.gz foop-d547dec802f76c346538144f4eacf6d8ca6310c4.tar.bz2 foop-d547dec802f76c346538144f4eacf6d8ca6310c4.zip | |
Pull up methods from mainwidget to sceneholder for sharing in both client and server
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 179 |
1 files changed, 4 insertions, 175 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 63280f2..34ff63e 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -6,89 +6,11 @@ | |||
| 6 | #include "constants.h" | 6 | #include "constants.h" |
| 7 | #include "audioplayer.h" | 7 | #include "audioplayer.h" |
| 8 | 8 | ||
| 9 | // temporary | 9 | #include "util.h" |
| 10 | Transmission::map_t createDummyMap() | ||
| 11 | { | ||
| 12 | Transmission::map_t map; | ||
| 13 | map = new Transmission::field_t*[Constants::map_size.width]; | ||
| 14 | for (unsigned int i = 0; i < Constants::map_size.width; ++i) | ||
| 15 | map[i] = new Transmission::field_t[Constants::map_size.height]; | ||
| 16 | |||
| 17 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 18 | { | ||
| 19 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 20 | { | ||
| 21 | Transmission::field_t &cur = map[x][y]; | ||
| 22 | cur = Transmission::none; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | const char *tmpl[] = { | ||
| 28 | " # # ", | ||
| 29 | " #### ###### # #### # # ###### ### ", | ||
| 30 | " # # ", | ||
| 31 | " # ##### # # # # # ### # # # ", | ||
| 32 | " # # # # # # # # # # ## # # ", | ||
| 33 | " # # # # # # # # ### # # # # ", | ||
| 34 | " # # # # # # # # # # # # ## # ", | ||
| 35 | " # # ### ##### # ### # # # ", | ||
| 36 | " ### # ", | ||
| 37 | " # # ### #### #### #### ##### ", | ||
| 38 | " #### # #..# #..# #..# # # ", | ||
| 39 | " # # ### #..# #..# #### # # # # ", | ||
| 40 | " # # # #..# #..# # # ", | ||
| 41 | " # #### # #### #### # # ##### # ", | ||
| 42 | " # # ", | ||
| 43 | " #### ###### # ##### # ####### ### ", | ||
| 44 | " # # " | ||
| 45 | }; | ||
| 46 | |||
| 47 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 48 | { | ||
| 49 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 50 | { | ||
| 51 | Transmission::field_t &cur = map[x][y]; | ||
| 52 | cur = Transmission::none; | ||
| 53 | if (tmpl[y][x] == '#') | ||
| 54 | cur |= Color::none | Transmission::block; | ||
| 55 | /* this is a simple hack to create areas where no | ||
| 56 | * autoplaced points will be placed (see below) | ||
| 57 | */ | ||
| 58 | else if (tmpl[y][x] == '.') | ||
| 59 | cur |= Transmission::point; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | 10 | ||
| 63 | map[0][0] |= Transmission::bonuspoint; | 11 | MainWidget::MainWidget(QWidget *parent) |
| 64 | map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; | 12 | : SceneHolder(parent), m_currentKey(0), m_running(false) |
| 65 | //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; | ||
| 66 | //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; | ||
| 67 | |||
| 68 | /* auto place normal points*/ | ||
| 69 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 70 | { | ||
| 71 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 72 | { | ||
| 73 | Transmission::field_t &cur = map[x][y]; | ||
| 74 | if (cur == Transmission::none) | ||
| 75 | cur |= Transmission::point; | ||
| 76 | else if (cur == Transmission::point) | ||
| 77 | cur = Transmission::none; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | return map; | ||
| 82 | } | ||
| 83 | |||
| 84 | MainWidget::MainWidget() | ||
| 85 | : m_currentKey(0), m_running(false) | ||
| 86 | { | 13 | { |
| 87 | visualMap.resize(Constants::map_size.width); | ||
| 88 | for (int i=0; i<visualMap.size(); ++i) { | ||
| 89 | visualMap[i].resize(Constants::map_size.height); | ||
| 90 | } | ||
| 91 | |||
| 92 | createGui(); | 14 | createGui(); |
| 93 | updateMap(createDummyMap()); | 15 | updateMap(createDummyMap()); |
| 94 | 16 | ||
| @@ -129,9 +51,6 @@ void MainWidget::createGui() | |||
| 129 | m_playerScoreLayouts.append(playerLayout); | 51 | m_playerScoreLayouts.append(playerLayout); |
| 130 | } | 52 | } |
| 131 | 53 | ||
| 132 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); | ||
| 133 | m_scene->setBackgroundBrush(Qt::black); | ||
| 134 | |||
| 135 | QGraphicsView *window = new QGraphicsView(m_scene, this); | 54 | QGraphicsView *window = new QGraphicsView(m_scene, this); |
| 136 | window->setFrameStyle(0); | 55 | window->setFrameStyle(0); |
| 137 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); | 56 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
| @@ -172,100 +91,10 @@ void MainWidget::updateScore() | |||
| 172 | 91 | ||
| 173 | void MainWidget::updateMap(const Transmission::map_t& map) | 92 | void MainWidget::updateMap(const Transmission::map_t& map) |
| 174 | { | 93 | { |
| 175 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | 94 | SceneHolder::updateMap(map); |
| 176 | { | ||
| 177 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 178 | { | ||
| 179 | const Transmission::field_t &cur = map[x][y]; | ||
| 180 | if (cur == Transmission::none) | ||
| 181 | continue; | ||
| 182 | //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; | ||
| 183 | |||
| 184 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); | ||
| 185 | //qDebug() << "col=" << color; | ||
| 186 | |||
| 187 | PixmapItem* item = NULL; | ||
| 188 | if (cur & Transmission::block) | ||
| 189 | { | ||
| 190 | unsigned int neighbours = Block::None; | ||
| 191 | // check left side | ||
| 192 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 193 | neighbours |= Block::Left; | ||
| 194 | // check right side | ||
| 195 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 196 | neighbours |= Block::Right; | ||
| 197 | // check upside | ||
| 198 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 199 | neighbours |= Block::Up; | ||
| 200 | // check down side | ||
| 201 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 202 | neighbours |= Block::Down; | ||
| 203 | item = new Block(color, neighbours); | ||
| 204 | } | ||
| 205 | else if (cur & Transmission::bonuspoint) | ||
| 206 | item = new BonusPoint(); | ||
| 207 | else if (cur & Transmission::point) | ||
| 208 | item = new Point(); | ||
| 209 | else if (cur & Transmission::pacman) | ||
| 210 | { | ||
| 211 | Actor *actor = m_actors.value(color, NULL); | ||
| 212 | if (actor == NULL) | ||
| 213 | { | ||
| 214 | //qDebug() << "new actor of col" << color; | ||
| 215 | actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing | ||
| 216 | m_actors[color] = actor; | ||
| 217 | m_scene->addItem(actor); | ||
| 218 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 219 | } | ||
| 220 | |||
| 221 | Actor::Movement direction = Actor::None; | ||
| 222 | switch (cur & Transmission::direction_mask) | ||
| 223 | { | ||
| 224 | case Transmission::direction_none: | ||
| 225 | direction = Actor::None; | ||
| 226 | break; | ||
| 227 | case Transmission::direction_left: | ||
| 228 | direction = Actor::Left; | ||
| 229 | break; | ||
| 230 | case Transmission::direction_right: | ||
| 231 | direction = Actor::Right; | ||
| 232 | break; | ||
| 233 | case Transmission::direction_up: | ||
| 234 | direction = Actor::Up; | ||
| 235 | break; | ||
| 236 | case Transmission::direction_down: | ||
| 237 | direction = Actor::Down; | ||
| 238 | break; | ||
| 239 | default: | ||
| 240 | Q_ASSERT(false); | ||
| 241 | } | ||
| 242 | //actor->move(direction, mapPositionToCoord(x, y)); | ||
| 243 | } | ||
| 244 | else | ||
| 245 | Q_ASSERT(false); | ||
| 246 | |||
| 247 | if (item != NULL) | ||
| 248 | { | ||
| 249 | m_scene->addItem(item); | ||
| 250 | item->setPos(mapPositionToCoord(x, y)); | ||
| 251 | PixmapItem* oldItem = visualMap[x][y]; | ||
| 252 | visualMap[x][y] = item; | ||
| 253 | if (oldItem != NULL) | ||
| 254 | { | ||
| 255 | m_scene->removeItem(item); | ||
| 256 | delete oldItem; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | updateScore(); | 95 | updateScore(); |
| 263 | } | 96 | } |
| 264 | 97 | ||
| 265 | QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | ||
| 266 | { | ||
| 267 | return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); | ||
| 268 | } | ||
| 269 | 98 | ||
| 270 | Transmission::field_t MainWidget::translateKey(int key) | 99 | Transmission::field_t MainWidget::translateKey(int key) |
| 271 | { | 100 | { |
