From 65195fdab6262d31056c74f922376de3b009943c Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 17 Apr 2011 19:19:56 +0200 Subject: a bigger commit again: - fix pacman movement. now more like real pacman (again). e.g. if you press a direction-key again: the pacman will move in that direction as soon as possible and no repeated keypress is needed - add random colorized blocks (without dieing yet) - add cmdline-option: --nocolorblocks to disable that --- pacman-c++/sceneholder.cpp | 120 ++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 55 deletions(-) (limited to 'pacman-c++/sceneholder.cpp') diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 11e0772..38b49e5 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -86,68 +86,77 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x { // 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) + else { - Actor *actor = m_actors.value(color, NULL); - if (actor == NULL) + if (cur & Transmission::block) + { + unsigned int neighbours = Block::None; + // check for old block first + if (visualMap[x][y] != NULL) + { + Block *oldItem = dynamic_cast(visualMap[x][y]); + if (oldItem != NULL) + neighbours = oldItem->neighbours(); + } + // 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); + } + + if (cur & Transmission::bonuspoint) + item = new BonusPoint(); + + if (cur & Transmission::point) { - actor = new Actor(color, (color == m_color)); - m_actors[color] = actor; - addItem(actor); - actor->setPos(mapPositionToCoord(x, y)); + item = new Point(); + connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); + ++m_pointsLeft; } - else + + if (cur & Transmission::pacman) { - Actor::Movement direction = Util::transmissionMovementToActor( - cur & Transmission::direction_mask); - /* WARNING: do NOT add actor to visualMap as visualMap-items may + 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) */ - actor->move(direction); - /* that's kind a hack but working right now + 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); + if (!(cur & Transmission::empty)) + actor->stopEating(); + qDebug() << "[SceneUpdate] actor moves: color=" << color + << "direction=" << direction << "newpos=" << QPoint(x, y); + } + } + + if (cur & Transmission::empty) + { + /* already handled */ } - } - else if (cur & Transmission::empty) - { - /* already handled */ - } - else - { - qWarning() << "Unknown data. value=" << cur; - Q_ASSERT(false); } /* add new created item to scene @@ -155,15 +164,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x */ if (item != NULL) { - addItem(item); - item->setPos(mapPositionToCoord(x, y)); GameEntity *oldItem = visualMap[x][y]; - visualMap[x][y] = item; if (oldItem != NULL) { - removeItem(item); + removeItem(oldItem); delete oldItem; } + + addItem(item); + item->setPos(mapPositionToCoord(x, y)); + visualMap[x][y] = item; } } -- cgit v1.2.3