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++/actor.cpp | 9 ++------- pacman-c++/actor.h | 6 ++---- pacman-c++/block.cpp | 2 +- pacman-c++/block.h | 4 ++-- pacman-c++/bonuspoint.cpp | 2 +- pacman-c++/bonuspoint.h | 4 ++-- pacman-c++/gameentity.cpp | 11 +++++++---- pacman-c++/gameentity.h | 14 ++++++++++++-- pacman-c++/mainwidget.cpp | 4 ++-- pacman-c++/pixmapitem.h | 3 +-- pacman-c++/point.cpp | 3 +-- pacman-c++/point.h | 4 ++-- pacman-c++/sceneholder.cpp | 26 ++++++++++++++++++++------ pacman-c++/sceneholder.h | 9 +++++++-- pacman-c++/server.cpp | 23 ++++++++--------------- 15 files changed, 70 insertions(+), 54 deletions(-) diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 41de160..d53566e 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -10,7 +10,7 @@ static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal } Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) - : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), + : GameEntity(color, parent),m_direction(Actor::None), m_local(local), m_wakaPlayer(NULL), m_roundPoints(0), m_gamePoints(0) { /* DON'T set any pixmap here. we've a pixmap in the animation @@ -102,11 +102,6 @@ QSequentialAnimationGroup *Actor::setupEatingAnimation(Actor::Movement direction return eating; } -Color::Color Actor::color() -{ - return m_color; -} - PixmapItem &Actor::icon() { return m_icon; @@ -194,7 +189,7 @@ void Actor::move(Actor::Movement direction) if (direction == Actor::None) { qDebug() << "pause"; - m_wakaPlayer->setMuted(true); + m_wakaPlayer->pause(); } m_direction = direction; diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index ae04687..6b6f743 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -1,7 +1,7 @@ #ifndef ACTOR_H #define ACTOR_H -#include "pixmapitem.h" +#include "gameentity.h" #include "constants.h" #include "audio.h" #include @@ -9,7 +9,7 @@ #include class Actor - : public PixmapItem + : public GameEntity { Q_OBJECT @@ -27,7 +27,6 @@ public: {}; QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); - Color::Color color(); PixmapItem &icon(); Movement direction(); bool isLocal(); @@ -47,7 +46,6 @@ private: private: QPixmap m_pix; - Color::Color m_color; Movement m_direction; PixmapItem m_icon; bool m_local; diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index b1ce0e0..c8607ac 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp @@ -5,7 +5,7 @@ QMap Block::m_pixmaps; Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) - : PixmapItem(parent) + : GameEntity(color, parent) { /* empty object for servers */ if (Constants::server) diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 9e49a7d..db104e3 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -1,12 +1,12 @@ #ifndef BLOCK_H #define BLOCK_H -#include "pixmapitem.h" +#include "gameentity.h" #include "constants.h" #include class Block - : public PixmapItem + : public GameEntity { public: enum Neighbour diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index bbb26b7..5bb470e 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -10,7 +10,7 @@ namespace } BonusPoint::BonusPoint(QGraphicsItem *parent) - : PixmapItem(parent) + : GameEntity(parent) { /* empty object for servers */ if (Constants::server) diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h index 222e046..2c5a07d 100644 --- a/pacman-c++/bonuspoint.h +++ b/pacman-c++/bonuspoint.h @@ -1,10 +1,10 @@ #ifndef BONUSPOINT_H #define BONUSPOINT_H -#include "pixmapitem.h" +#include "gameentity.h" class BonusPoint - : public PixmapItem + : public GameEntity { public: BonusPoint(QGraphicsItem *parent=0); diff --git a/pacman-c++/gameentity.cpp b/pacman-c++/gameentity.cpp index 8711ebe..9dc72ec 100644 --- a/pacman-c++/gameentity.cpp +++ b/pacman-c++/gameentity.cpp @@ -1,6 +1,9 @@ #include "gameentity.h" -GameEntity::GameEntity() - : m_eaten(false) -{ -} +GameEntity::GameEntity(Color::Color color, QGraphicsItem *parent) + : PixmapItem(parent), m_eaten(false), m_color(color) +{} + +GameEntity::GameEntity(QGraphicsItem *parent) + : PixmapItem(parent), m_eaten(false), m_color(Color::none) +{} diff --git a/pacman-c++/gameentity.h b/pacman-c++/gameentity.h index afa3aba..2fde095 100644 --- a/pacman-c++/gameentity.h +++ b/pacman-c++/gameentity.h @@ -1,6 +1,8 @@ #ifndef GAMEENTITY_H #define GAMEENTITY_H +#include "constants.h" +#include "pixmapitem.h" #include class Actor; @@ -9,12 +11,20 @@ class Actor; * Base class for entities that interact in the game */ class GameEntity + : public PixmapItem { public: - GameEntity(); + GameEntity(Color::Color color = Color::none, QGraphicsItem *parent = 0); + GameEntity(QGraphicsItem *parent); virtual ~GameEntity() {}; + /* color of entity */ + virtual Color::Color color() + { + return m_color; + } + /* returns whether the actor may enter this field */ virtual bool checkEnter(Actor *) { @@ -42,9 +52,9 @@ public: virtual void onDie(Actor *) {}; - protected: bool m_eaten; + Color::Color m_color; }; #endif // GAMEENTITY_H diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 028bfad..ba09f7d 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -96,10 +96,10 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet) for(unsigned i = 0; Color::order[i] != Color::none; ++i) { QGridLayout *score = m_playerScoreLayouts.at(i); - QLabel *turnPointsLbl = dynamic_cast(score->itemAtPosition(0, 1)->widget()); + QLabel *turnPointsLbl = dynamic_cast(score->itemAtPosition(0, 1)->widget()); turnPointsLbl->setText(QString::number(packet.round_points(i))); - QLabel *allPointsLbl = dynamic_cast(score->itemAtPosition(1, 1)->widget()); + QLabel *allPointsLbl = dynamic_cast(score->itemAtPosition(1, 1)->widget()); allPointsLbl->setText(QString::number(packet.round_points(i))); } } diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 7560556..f57c22a 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h @@ -1,12 +1,11 @@ #ifndef PIXMAPITEM__H #define PIXMAPITEM__H -#include "gameentity.h" #include #include class PixmapItem - : public QGraphicsObject, public GameEntity + : public QGraphicsObject { public: PixmapItem(QGraphicsItem *parent = 0); diff --git a/pacman-c++/point.cpp b/pacman-c++/point.cpp index 54c0ee4..1943756 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp @@ -8,7 +8,7 @@ namespace } Point::Point(QGraphicsItem *parent) - : PixmapItem(parent) + : GameEntity(parent) { /* empty object for servers */ if (Constants::server) @@ -34,5 +34,4 @@ void Point::onDie(Actor *actor) return; if (player->state() != Phonon::PlayingState) player->play(); - player->setMuted(false); } diff --git a/pacman-c++/point.h b/pacman-c++/point.h index 944764b..9fd9929 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h @@ -1,10 +1,10 @@ #ifndef POINT_H #define POINT_H -#include "pixmapitem.h" +#include "gameentity.h" class Point - : public PixmapItem + : public GameEntity { public: Point(QGraphicsItem *parent=0); 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) { diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h index 61cff3e..5183f65 100644 --- a/pacman-c++/sceneholder.h +++ b/pacman-c++/sceneholder.h @@ -4,7 +4,7 @@ #include "constants.h" #include -class PixmapItem; +class GameEntity; class Actor; class SceneHolder @@ -32,11 +32,16 @@ protected: QPoint CoordToMapPosition(QPoint point); /* map of all pixmap instances */ - QVector< QVector > visualMap; + QVector< QVector > visualMap; /* map of actors in order to keep track of those instances */ QMap m_actors; + /* items that got removed/has been eaten + * must be remove one tick later + */ + QList m_oldItems; + /* my local color */ Color::Color m_color; diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index b0ac6f8..dd15934 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -83,21 +83,11 @@ invalid_direction: // // TODO: support actors eating each other - // old item - PixmapItem *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; - if (oldItem != NULL) - { - /* set item to explicit empty - * and add actor that removed/has eaten that item - */ - if (oldItem->eaten()) - map[mapPosition.x()][mapPosition.y()] = Transmission::empty | actor->color(); - } - - // new item - PixmapItem *item = visualMap[newMapPosition.x()][newMapPosition.y()]; + /* check if there's an item at new location of actor */ + GameEntity *item = visualMap[newMapPosition.x()][newMapPosition.y()]; if (item != NULL && oldItem != item) { + qDebug() << "item at new actor location found"; if (!item->checkEnter(actor)) { /* movement invalid. e.g. move against wall */ @@ -105,11 +95,11 @@ invalid_direction: } else { - // apply actions of entering this field + /* apply actions of entering this field */ bool survive = item->enter(actor); if (!survive) { - //map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty; + map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); } } } @@ -134,6 +124,9 @@ invalid_direction: map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | i.key() | Util::actorMovementToTransmission(i.value()); + /* DEBUG: uncomments to disable auto-movement */ + //m_actorMovements[i.key()] = Actor::None; + if (i.value() == Actor::None) { /* set actor to non-moving */ -- cgit v1.2.3