diff options
Diffstat (limited to 'pacman-c++/sceneholder.cpp')
| -rw-r--r-- | pacman-c++/sceneholder.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp new file mode 100644 index 0000000..83025d5 --- /dev/null +++ b/pacman-c++/sceneholder.cpp | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | #include "sceneholder.h" | ||
| 2 | |||
| 3 | #include "constants.h" | ||
| 4 | #include "pixmapitem.h" | ||
| 5 | #include "block.h" | ||
| 6 | #include "actor.h" | ||
| 7 | #include "bonuspoint.h" | ||
| 8 | #include "point.h" | ||
| 9 | |||
| 10 | SceneHolder::SceneHolder(QWidget* parent): QWidget(parent) | ||
| 11 | { | ||
| 12 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); | ||
| 13 | m_scene->setBackgroundBrush(Qt::black); | ||
| 14 | |||
| 15 | visualMap.resize(Constants::map_size.width); | ||
| 16 | for (int i=0; i<visualMap.size(); ++i) { | ||
| 17 | visualMap[i].resize(Constants::map_size.height); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | void SceneHolder::updateMap(const Transmission::map_t& map) | ||
| 22 | { | ||
| 23 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 24 | { | ||
| 25 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 26 | { | ||
| 27 | const Transmission::field_t &cur = map[x][y]; | ||
| 28 | if (cur == Transmission::none) | ||
| 29 | continue; | ||
| 30 | //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; | ||
| 31 | |||
| 32 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); | ||
| 33 | //qDebug() << "col=" << color; | ||
| 34 | |||
| 35 | PixmapItem* item = NULL; | ||
| 36 | if (cur & Transmission::block) | ||
| 37 | { | ||
| 38 | unsigned int neighbours = Block::None; | ||
| 39 | // check left side | ||
| 40 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 41 | neighbours |= Block::Left; | ||
| 42 | // check right side | ||
| 43 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 44 | neighbours |= Block::Right; | ||
| 45 | // check upside | ||
| 46 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 47 | neighbours |= Block::Up; | ||
| 48 | // check down side | ||
| 49 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 50 | neighbours |= Block::Down; | ||
| 51 | item = new Block(color, neighbours); | ||
| 52 | } | ||
| 53 | else if (cur & Transmission::bonuspoint) | ||
| 54 | item = new BonusPoint(); | ||
| 55 | else if (cur & Transmission::point) | ||
| 56 | item = new Point(); | ||
| 57 | else if (cur & Transmission::pacman) | ||
| 58 | { | ||
| 59 | Actor *actor = m_actors.value(color, NULL); | ||
| 60 | if (actor == NULL) | ||
| 61 | { | ||
| 62 | //qDebug() << "new actor of col" << color; | ||
| 63 | actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing | ||
| 64 | m_actors[color] = actor; | ||
| 65 | m_scene->addItem(actor); | ||
| 66 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 67 | } | ||
| 68 | |||
| 69 | Actor::Movement direction = Actor::None; | ||
| 70 | switch (cur & Transmission::direction_mask) | ||
| 71 | { | ||
| 72 | case Transmission::direction_none: | ||
| 73 | direction = Actor::None; | ||
| 74 | break; | ||
| 75 | case Transmission::direction_left: | ||
| 76 | direction = Actor::Left; | ||
| 77 | break; | ||
| 78 | case Transmission::direction_right: | ||
| 79 | direction = Actor::Right; | ||
| 80 | break; | ||
| 81 | case Transmission::direction_up: | ||
| 82 | direction = Actor::Up; | ||
| 83 | break; | ||
| 84 | case Transmission::direction_down: | ||
| 85 | direction = Actor::Down; | ||
| 86 | break; | ||
| 87 | default: | ||
| 88 | Q_ASSERT(false); | ||
| 89 | } | ||
| 90 | //actor->move(direction, mapPositionToCoord(x, y)); | ||
| 91 | } | ||
| 92 | else | ||
| 93 | Q_ASSERT(false); | ||
| 94 | |||
| 95 | if (item != NULL) | ||
| 96 | { | ||
| 97 | m_scene->addItem(item); | ||
| 98 | item->setPos(mapPositionToCoord(x, y)); | ||
| 99 | PixmapItem* oldItem = visualMap[x][y]; | ||
| 100 | visualMap[x][y] = item; | ||
| 101 | if (oldItem != NULL) | ||
| 102 | { | ||
| 103 | m_scene->removeItem(item); | ||
| 104 | delete oldItem; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) | ||
| 113 | { | ||
| 114 | return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); | ||
| 115 | } \ No newline at end of file | ||
