From fae7c3377c8995b217e620c5cf1e963b3ab1e84a Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 12 Apr 2011 16:22:19 +0200 Subject: finding out which actor has eaten which element didn't work correctly. instead of checking that on client side the server now just sends the id of the actor withing the explicit empty id data field --- pacman-c++/actor.cpp | 93 ++++++++++++++++++++++++++-------------------- pacman-c++/actor.h | 3 +- pacman-c++/point.cpp | 8 +++- pacman-c++/sceneholder.cpp | 26 +++++++------ pacman-c++/server.cpp | 5 ++- 5 files changed, 81 insertions(+), 54 deletions(-) diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index baf6dca..41de160 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -11,7 +11,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), - m_player(NULL), m_roundPoints(0), m_gamePoints(0) + m_wakaPlayer(NULL), m_roundPoints(0), m_gamePoints(0) { /* DON'T set any pixmap here. we've a pixmap in the animation * but we need a sprite for the collision detection @@ -55,14 +55,14 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) m_eating.append(setupEatingAnimation(Actor::Up)); m_eating.append(setupEatingAnimation(Actor::Down)); - /* setup sound */ + /* setup waka sound */ if (local) { - m_player = new AudioPlayer(this); - if (m_player->isWorking()) + m_wakaPlayer = new AudioPlayer(this); + if (m_wakaPlayer->isWorking()) { - m_player->setLoop(Sound::WakaWaka); - AudioManager::self()->registerAudioPlayer(m_player); + m_wakaPlayer->setLoop(Sound::WakaWaka); + AudioManager::self()->registerAudioPlayer(m_wakaPlayer); } } @@ -95,7 +95,7 @@ QSequentialAnimationGroup *Actor::setupEatingAnimation(Actor::Movement direction fadeout->setEndValue(false); QPropertyAnimation *move = new QPropertyAnimation(img, "pos", m_moving); - move->setDuration(Constants::tick - 30); //TODO + move->setDuration(Constants::tick - 30); move->setEndValue(QPoint(0, 0)); } @@ -181,16 +181,51 @@ void Actor::move(Actor::Movement direction) if (direction != Actor::None) { - if (m_local && m_player->isWorking() && m_player->state() != Phonon::PlayingState) - m_player->play(); + //if (m_local && m_player->isWorking() && m_player->state() != Phonon::PlayingState) + // m_player->play(); m_moving->start(); } else if (direction != m_direction) { - if (m_local && m_player->isWorking() && m_player->state() != Phonon::PausedState) - m_player->pause(); + //if (m_local && m_player->isWorking() && m_player->state() != Phonon::PausedState) + // m_player->pause(); } + if (direction == Actor::None) + { + qDebug() << "pause"; + m_wakaPlayer->setMuted(true); + } + + m_direction = direction; +} + +void Actor::moveByServer(Actor::Movement direction) +{ + qDebug() << "move by server"; + + QPointF endpos(0, 0); + switch(direction) + { + case Actor::None: + break; + case Actor::Left: + endpos.setX(static_cast(Constants::field_size.width) * -1); + break; + case Actor::Right: + endpos.setX(Constants::field_size.width); + break; + case Actor::Up: + endpos.setY(static_cast(Constants::field_size.height) * -1); + break; + case Actor::Down: + endpos.setY(Constants::field_size.height); + break; + default: + Q_ASSERT(false); + break; + } + setPos(pos() + endpos); m_direction = direction; } @@ -213,6 +248,13 @@ void Actor::eatingCherry() AudioManager::self()->play(Sound::EatingCherry); } +AudioPlayer *Actor::wakaPlayer() +{ + if (!m_local || !m_wakaPlayer->isWorking()) + return NULL; + return m_wakaPlayer; +} + unsigned int Actor::getRoundPoints() { return m_roundPoints; @@ -233,32 +275,3 @@ void Actor::finishRound() m_gamePoints += m_roundPoints; m_roundPoints = 0; } - -void Actor::moveByServer(Actor::Movement direction) -{ - qDebug() << "move by server"; - - QPointF endpos(0, 0); - switch(direction) - { - case Actor::None: - break; - case Actor::Left: - endpos.setX(static_cast(Constants::field_size.width) * -1); - break; - case Actor::Right: - endpos.setX(Constants::field_size.width); - break; - case Actor::Up: - endpos.setY(static_cast(Constants::field_size.height) * -1); - break; - case Actor::Down: - endpos.setY(Constants::field_size.height); - break; - default: - Q_ASSERT(false); - break; - } - setPos(pos() + endpos); - m_direction = direction; -} diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index d1875f1..ae04687 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -35,6 +35,7 @@ public: bool isMoving(); void die(); void eatingCherry(); + AudioPlayer *wakaPlayer(); unsigned int getRoundPoints(); unsigned int getGamePoints(); @@ -50,7 +51,7 @@ private: Movement m_direction; PixmapItem m_icon; bool m_local; - AudioPlayer *m_player; + AudioPlayer *m_wakaPlayer; unsigned int m_roundPoints; unsigned int m_gamePoints; diff --git a/pacman-c++/point.cpp b/pacman-c++/point.cpp index 35266f6..54c0ee4 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp @@ -28,5 +28,11 @@ bool Point::enter(Actor *actor) void Point::onDie(Actor *actor) { - //TODO + AudioPlayer *player = actor->wakaPlayer(); + qDebug() << "onDie" << player; + if (player == NULL) + return; + if (player->state() != Phonon::PlayingState) + player->play(); + player->setMuted(false); } diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index b788a49..fc638f7 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -33,25 +33,28 @@ void SceneHolder::updateMap(const Transmission::map_t& map) if (cur & Transmission::empty) { - /* special handling for purging field */ PixmapItem *oldItem = visualMap[x][y]; - /* remove elements (in case it's not an actor) */ - if (oldItem != NULL && dynamic_cast(item) == NULL) + /* 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 *i, m_actors) + foreach (Actor *tmp, m_actors) { - if (CoordToMapPosition(i->pos().toPoint()) == QPoint(x, y)) + if (cur & tmp->color()) { - actor = i; + actor = tmp; break; } } + + /* no actor removed that item */ + if (actor == NULL) + Q_ASSERT(false); oldItem->onDie(actor); - delete oldItem; - //qDebug() << "deleting " << x << y; } } @@ -112,11 +115,14 @@ void SceneHolder::updateMap(const Transmission::map_t& map) Q_ASSERT(false); } + /* add new created item to scene + * remove old item on that location if there's one + */ if (item != NULL) { addItem(item); item->setPos(mapPositionToCoord(x, y)); - PixmapItem* oldItem = visualMap[x][y]; + PixmapItem *oldItem = visualMap[x][y]; visualMap[x][y] = item; if (oldItem != NULL) { @@ -126,7 +132,6 @@ void SceneHolder::updateMap(const Transmission::map_t& map) } } } - } void SceneHolder::setColor(Color::Color color) @@ -147,7 +152,6 @@ unsigned int SceneHolder::pointsLeft() void SceneHolder::decrementPoints() { --m_pointsLeft; - qDebug() << "points left=" << m_pointsLeft; } QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 9a7b188..b0ac6f8 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -87,8 +87,11 @@ invalid_direction: 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; + map[mapPosition.x()][mapPosition.y()] = Transmission::empty | actor->color(); } // new item -- cgit v1.2.3