From 536ddd91ae7f162045226d4b358ba95d678f6deb Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 15 Apr 2011 03:20:17 +0200 Subject: add support for random bonus points --- pacman-c++/sceneholder.cpp | 215 ++++++++++++++++++++++++--------------------- 1 file changed, 114 insertions(+), 101 deletions(-) (limited to 'pacman-c++/sceneholder.cpp') diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index d9e07e5..11e0772 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -39,117 +39,130 @@ void SceneHolder::updateMap(const Transmission::map_t& map) const Transmission::field_t &cur = map[x][y]; if (cur == Transmission::none) continue; + updateMap(map, x, y); + } + } +} - Color::Color color = static_cast(cur & Transmission::color_mask); - GameEntity* item = NULL; +void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x, unsigned int y) +{ + const Transmission::field_t &cur = map[x][y]; + if (cur == Transmission::none) + return; - if (cur & Transmission::empty) - { - GameEntity *oldItem = visualMap[x][y]; - /* special handling for purging field - * remove elements (in case it's not an actor) - */ - if (oldItem != NULL && dynamic_cast(oldItem) == NULL) - { - visualMap[x][y] = NULL; - Actor *actor = NULL; - foreach (Actor *tmp, m_actors) - { - if (cur & tmp->color()) - { - actor = tmp; - break; - } - } - - /* an item must be removed by an actor */ - if (actor == NULL) - Q_ASSERT(false); - oldItem->onDie(actor); - - /* register item for removal in next update */ - m_oldItems.append(oldItem); - } - } + Color::Color color = static_cast(cur & Transmission::color_mask); + GameEntity* item = NULL; - if (cur == Transmission::none) - { - // no update - } - else 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(); - connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); - ++m_pointsLeft; - } - else if (cur & Transmission::pacman) + if (cur & Transmission::empty) + { + GameEntity *oldItem = visualMap[x][y]; + /* special handling for purging field + * remove elements (in case it's not an actor) + */ + if (oldItem != NULL && dynamic_cast(oldItem) == NULL) + { + visualMap[x][y] = NULL; + Actor *actor = NULL; + foreach (Actor *tmp, m_actors) { - Actor *actor = m_actors.value(color, NULL); - if (actor == NULL) + if (cur & tmp->color()) { - actor = new Actor(color, (color == m_color)); - m_actors[color] = actor; - addItem(actor); - actor->setPos(mapPositionToCoord(x, y)); - } - else - { - Actor::Movement direction = Util::transmissionMovementToActor( - cur & Transmission::direction_mask); - actor->move(direction); - /* that's kind a hack but working right now - * I think that will fall on our's hat sooner or later - */ - if (!(cur & Transmission::empty)) - actor->stopEating(); - qDebug() << "[SceneUpdate] actor moves: color=" << color - << "direction=" << direction << "newpos=" << QPoint(x, y); + actor = tmp; + break; } } - else if (cur & Transmission::empty) - { - /* already handled */ - } - else - { - qWarning() << "Unknown data. value=" << cur; + + /* an item must be removed by an actor */ + if (actor == NULL) Q_ASSERT(false); - } + oldItem->onDie(actor); - /* add new created item to scene - * remove old item on that location if there's one + /* register item for removal in next update */ + m_oldItems.append(oldItem); + } + } + + if (cur == Transmission::none) + { + // no update + } + else 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(); + connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); + ++m_pointsLeft; + } + else if (cur & Transmission::pacman) + { + Actor *actor = m_actors.value(color, NULL); + if (actor == NULL) + { + actor = new Actor(color, (color == m_color)); + m_actors[color] = actor; + addItem(actor); + actor->setPos(mapPositionToCoord(x, y)); + } + else + { + Actor::Movement direction = Util::transmissionMovementToActor( + cur & Transmission::direction_mask); + /* WARNING: do NOT add actor to visualMap as visualMap-items may + * get deleted during update and actors are referenced in_mactors too + * if you REALLY need that you need to changed updateMap so that all actors + * will be moved before any new items get allocated (totally untested) */ - if (item != NULL) - { - addItem(item); - item->setPos(mapPositionToCoord(x, y)); - GameEntity *oldItem = visualMap[x][y]; - visualMap[x][y] = item; - if (oldItem != NULL) - { - removeItem(item); - delete oldItem; - } - } + actor->move(direction); + /* that's kind a hack but working right now + * I think that will fall on our's hat sooner or later + */ + if (!(cur & Transmission::empty)) + actor->stopEating(); + qDebug() << "[SceneUpdate] actor moves: color=" << color + << "direction=" << direction << "newpos=" << QPoint(x, y); + } + } + else if (cur & Transmission::empty) + { + /* already handled */ + } + else + { + qWarning() << "Unknown data. value=" << cur; + Q_ASSERT(false); + } + + /* add new created item to scene + * remove old item on that location if there's one + */ + if (item != NULL) + { + addItem(item); + item->setPos(mapPositionToCoord(x, y)); + GameEntity *oldItem = visualMap[x][y]; + visualMap[x][y] = item; + if (oldItem != NULL) + { + removeItem(item); + delete oldItem; } } } -- cgit v1.2.3