diff options
| author | manuel <manuel@mausz.at> | 2011-04-12 18:08:02 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-12 18:08:02 +0200 |
| commit | 651a1bee1adc5318922d1b37b0cea11a65df71e2 (patch) | |
| tree | a60d4921d569f0b4b817e31e049cbc64d11d05f9 /pacman-c++/sceneholder.cpp | |
| parent | fae7c3377c8995b217e620c5cf1e963b3ab1e84a (diff) | |
| download | foop-651a1bee1adc5318922d1b37b0cea11a65df71e2.tar.gz foop-651a1bee1adc5318922d1b37b0cea11a65df71e2.tar.bz2 foop-651a1bee1adc5318922d1b37b0cea11a65df71e2.zip | |
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()
Diffstat (limited to 'pacman-c++/sceneholder.cpp')
| -rw-r--r-- | pacman-c++/sceneholder.cpp | 26 |
1 files changed, 20 insertions, 6 deletions
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 @@ | |||
| 1 | #include "sceneholder.h" | 1 | #include "sceneholder.h" |
| 2 | #include "constants.h" | 2 | #include "constants.h" |
| 3 | #include "pixmapitem.h" | 3 | #include "gameentity.h" |
| 4 | #include "block.h" | 4 | #include "block.h" |
| 5 | #include "actor.h" | 5 | #include "actor.h" |
| 6 | #include "bonuspoint.h" | 6 | #include "bonuspoint.h" |
| @@ -20,6 +20,18 @@ SceneHolder::SceneHolder(QObject *parent) | |||
| 20 | 20 | ||
| 21 | void SceneHolder::updateMap(const Transmission::map_t& map) | 21 | void SceneHolder::updateMap(const Transmission::map_t& map) |
| 22 | { | 22 | { |
| 23 | /* remove items that got marked for removal from scene */ | ||
| 24 | QMutableListIterator<GameEntity *> i(m_oldItems); | ||
| 25 | while(i.hasNext()) | ||
| 26 | { | ||
| 27 | i.next(); | ||
| 28 | GameEntity *item = i.value(); | ||
| 29 | removeItem(item); | ||
| 30 | i.remove(); | ||
| 31 | delete item; | ||
| 32 | } | ||
| 33 | |||
| 34 | /* process update */ | ||
| 23 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | 35 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) |
| 24 | { | 36 | { |
| 25 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | 37 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) |
| @@ -29,17 +41,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 29 | continue; | 41 | continue; |
| 30 | 42 | ||
| 31 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); | 43 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); |
| 32 | PixmapItem* item = NULL; | 44 | GameEntity* item = NULL; |
| 33 | 45 | ||
| 34 | if (cur & Transmission::empty) | 46 | if (cur & Transmission::empty) |
| 35 | { | 47 | { |
| 36 | PixmapItem *oldItem = visualMap[x][y]; | 48 | GameEntity *oldItem = visualMap[x][y]; |
| 37 | /* special handling for purging field | 49 | /* special handling for purging field |
| 38 | * remove elements (in case it's not an actor) | 50 | * remove elements (in case it's not an actor) |
| 39 | */ | 51 | */ |
| 40 | if (oldItem != NULL && dynamic_cast<Actor *>(oldItem) == NULL) | 52 | if (oldItem != NULL && dynamic_cast<Actor *>(oldItem) == NULL) |
| 41 | { | 53 | { |
| 42 | removeItem(oldItem); | ||
| 43 | visualMap[x][y] = NULL; | 54 | visualMap[x][y] = NULL; |
| 44 | Actor *actor = NULL; | 55 | Actor *actor = NULL; |
| 45 | foreach (Actor *tmp, m_actors) | 56 | foreach (Actor *tmp, m_actors) |
| @@ -51,10 +62,13 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 51 | } | 62 | } |
| 52 | } | 63 | } |
| 53 | 64 | ||
| 54 | /* no actor removed that item */ | 65 | /* an item must be removed by an actor */ |
| 55 | if (actor == NULL) | 66 | if (actor == NULL) |
| 56 | Q_ASSERT(false); | 67 | Q_ASSERT(false); |
| 57 | oldItem->onDie(actor); | 68 | oldItem->onDie(actor); |
| 69 | |||
| 70 | /* register item for removal in next update */ | ||
| 71 | m_oldItems.append(oldItem); | ||
| 58 | } | 72 | } |
| 59 | } | 73 | } |
| 60 | 74 | ||
| @@ -122,7 +136,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 122 | { | 136 | { |
| 123 | addItem(item); | 137 | addItem(item); |
| 124 | item->setPos(mapPositionToCoord(x, y)); | 138 | item->setPos(mapPositionToCoord(x, y)); |
| 125 | PixmapItem *oldItem = visualMap[x][y]; | 139 | GameEntity *oldItem = visualMap[x][y]; |
| 126 | visualMap[x][y] = item; | 140 | visualMap[x][y] = item; |
| 127 | if (oldItem != NULL) | 141 | if (oldItem != NULL) |
| 128 | { | 142 | { |
