From 7e4bc8ece49543533c4e27f5d6bc867c1bada601 Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 9 Apr 2011 12:34:06 +0200 Subject: collision detection fix --- pacman-c++/actor.cpp | 9 ++++++--- pacman-c++/audioplayer.cpp | 6 ++++++ pacman-c++/audioplayer.h | 1 + pacman-c++/mainwidget.cpp | 13 ++++++++++--- pacman-c++/pixmapitem.cpp | 7 +++++++ pacman-c++/pixmapitem.h | 1 + 6 files changed, 31 insertions(+), 6 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 5ccc339..445d63d 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -14,7 +14,10 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local) { m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); - /* DON'T set any pixmap here. we've a pixmap in the animation */ + /* DON'T set any pixmap here. we've a pixmap in the animation + * but we need a sprite for the collision detection + */ + setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); /* higher player "over" lower player */ setZValue(m_color * 10); @@ -165,7 +168,7 @@ void Actor::move(Actor::Movement direction) { if (m_local && AudioPlayer::self()->state() != Phonon::PlayingState) { - AudioPlayer::self()->clearQueue(); + AudioPlayer::self()->clear(); AudioPlayer::self()->play(AudioPlayer::WakaWaka); AudioPlayer::self()->enqueue(AudioPlayer::WakaWaka); } @@ -183,7 +186,7 @@ bool Actor::isMoving() } void Actor::enqueue() -{ +{ if (isMoving()) AudioPlayer::self()->enqueue(AudioPlayer::WakaWaka); } diff --git a/pacman-c++/audioplayer.cpp b/pacman-c++/audioplayer.cpp index d3d2ff6..424d3cf 100644 --- a/pacman-c++/audioplayer.cpp +++ b/pacman-c++/audioplayer.cpp @@ -72,6 +72,12 @@ void AudioPlayer::play(AudioPlayer::Sound sound) m_player->play(); } +void AudioPlayer::clear() +{ + if (!m_working) + return; + m_player->clear(); +} void AudioPlayer::enqueue(AudioPlayer::Sound sound) { diff --git a/pacman-c++/audioplayer.h b/pacman-c++/audioplayer.h index e5344e7..dd0c25a 100644 --- a/pacman-c++/audioplayer.h +++ b/pacman-c++/audioplayer.h @@ -32,6 +32,7 @@ public: void stop(); void setMuted(bool mute = true); void play(Sound sound); + void clear(); void enqueue(Sound sound); void clearQueue(); Phonon::State state(); diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 072d417..63280f2 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -62,8 +62,8 @@ Transmission::map_t createDummyMap() map[0][0] |= Transmission::bonuspoint; map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; - map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; - map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; + //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; + //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; /* auto place normal points*/ for (unsigned int x = 0; x < Constants::map_size.width; ++x) @@ -97,7 +97,7 @@ MainWidget::MainWidget() QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(tick())); - timer->start(100); + timer->start(Constants::tick); startGame(); } @@ -318,6 +318,13 @@ void MainWidget::tick() { i.next(); i.value()->move(mov); + QList list(i.value()->collidingItems()); + for(int j = 0; j < list.count(); ++j) + { + if (list.at(j)->parentItem() == i.value()) + continue; + list.at(j)->setOpacity(0.6); + } } } diff --git a/pacman-c++/pixmapitem.cpp b/pacman-c++/pixmapitem.cpp index 84517c2..1ceeec1 100644 --- a/pacman-c++/pixmapitem.cpp +++ b/pacman-c++/pixmapitem.cpp @@ -63,6 +63,13 @@ QRectF PixmapItem::boundingRect() const return QRectF(QPointF(0, 0), size()); } +QPainterPath PixmapItem::shape() const +{ + QPainterPath path; + path.addRect(boundingRect()); + return path; +} + void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { painter->drawPixmap(QPoint(0, 0), m_pix, QRect(m_x, m_y, m_width, m_height)); diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index e576d77..cff976f 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h @@ -18,6 +18,7 @@ public: void setSprite(int x, int y, int width, int height); QSizeF size() const; QRectF boundingRect() const; + QPainterPath shape() const; void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); private: -- cgit v1.2.3