From 651a1bee1adc5318922d1b37b0cea11a65df71e2 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 12 Apr 2011 18:08:02 +0200 Subject: make removal of items from scene delayed by one tick (looks better) all items are now derived from gameentity and gameentity is derived from pixmapitem: - this is naturally better - allows us to add a generic gameentity.color() --- pacman-c++/sceneholder.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'pacman-c++/sceneholder.cpp') diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index fc638f7..f0a5de3 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -1,6 +1,6 @@ #include "sceneholder.h" #include "constants.h" -#include "pixmapitem.h" +#include "gameentity.h" #include "block.h" #include "actor.h" #include "bonuspoint.h" @@ -20,6 +20,18 @@ SceneHolder::SceneHolder(QObject *parent) void SceneHolder::updateMap(const Transmission::map_t& map) { + /* remove items that got marked for removal from scene */ + QMutableListIterator i(m_oldItems); + while(i.hasNext()) + { + i.next(); + GameEntity *item = i.value(); + removeItem(item); + i.remove(); + delete item; + } + + /* process update */ for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) @@ -29,17 +41,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map) continue; Color::Color color = static_cast(cur & Transmission::color_mask); - PixmapItem* item = NULL; + GameEntity* item = NULL; if (cur & Transmission::empty) { - PixmapItem *oldItem = visualMap[x][y]; + 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) { - removeItem(oldItem); visualMap[x][y] = NULL; Actor *actor = NULL; foreach (Actor *tmp, m_actors) @@ -51,10 +62,13 @@ void SceneHolder::updateMap(const Transmission::map_t& map) } } - /* no actor removed that item */ + /* 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); } } @@ -122,7 +136,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) { addItem(item); item->setPos(mapPositionToCoord(x, y)); - PixmapItem *oldItem = visualMap[x][y]; + GameEntity *oldItem = visualMap[x][y]; visualMap[x][y] = item; if (oldItem != NULL) { -- cgit v1.2.3