diff options
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/actor.cpp | 48 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 11 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 28 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 17 | ||||
| -rw-r--r-- | pacman-c++/pacman.qrc | 6 | ||||
| -rw-r--r-- | pacman-c++/pixmapitem.h | 4 | ||||
| -rw-r--r-- | pacman-c++/point.h | 2 |
7 files changed, 101 insertions, 15 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index b6de49c..7358756 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #include "animationmanager.h" | 2 | #include "animationmanager.h" |
| 3 | #include <QtCore/QPropertyAnimation> | 3 | #include <QtCore/QPropertyAnimation> |
| 4 | #include <QtCore/QVariantAnimation> | 4 | #include <QtCore/QVariantAnimation> |
| 5 | #include <phonon/AudioOutput> | ||
| 5 | #include <QDebug> | 6 | #include <QDebug> |
| 6 | 7 | ||
| 7 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) | 8 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) |
| @@ -13,9 +14,8 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 13 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local) | 14 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local) |
| 14 | { | 15 | { |
| 15 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); | 16 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); |
| 16 | // DON'T set any pixmap here. we've a pixmap in the animation | 17 | /* DON'T set any pixmap here. we've a pixmap in the animation */ |
| 17 | //setPixmap(m_pix); | 18 | /* higher player "over" lower player */ |
| 18 | // higher player "over" lower player | ||
| 19 | setZValue(m_color * 10); | 19 | setZValue(m_color * 10); |
| 20 | 20 | ||
| 21 | /* setup icon for player */ | 21 | /* setup icon for player */ |
| @@ -46,6 +46,17 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 46 | m_eating.append(setupEatingAnimation(Actor::Up)); | 46 | m_eating.append(setupEatingAnimation(Actor::Up)); |
| 47 | m_eating.append(setupEatingAnimation(Actor::Down)); | 47 | m_eating.append(setupEatingAnimation(Actor::Down)); |
| 48 | 48 | ||
| 49 | /* setup player */ | ||
| 50 | m_player = new Phonon::MediaObject(this); | ||
| 51 | Phonon::AudioOutput *audio_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); | ||
| 52 | Phonon::createPath(m_player, audio_output); | ||
| 53 | connect(m_player, SIGNAL(finished()), this, SLOT(enqueue())); | ||
| 54 | |||
| 55 | /* preload sounds */ | ||
| 56 | m_sounds.append(new QFile(":/sound/wakawaka")); | ||
| 57 | m_sounds.append(new QFile(":/sound/die")); | ||
| 58 | m_sounds.append(new QFile(":/sound/eatingcherry")); | ||
| 59 | |||
| 49 | /* make the picture showing the current direction visible */ | 60 | /* make the picture showing the current direction visible */ |
| 50 | m_images[m_direction]->setVisible(true); | 61 | m_images[m_direction]->setVisible(true); |
| 51 | } | 62 | } |
| @@ -103,6 +114,13 @@ void Actor::move(Actor::Movement direction) | |||
| 103 | if (isMoving()) | 114 | if (isMoving()) |
| 104 | return; | 115 | return; |
| 105 | 116 | ||
| 117 | if (m_local && m_player->state() != Phonon::PlayingState) | ||
| 118 | { | ||
| 119 | m_player->stop(); | ||
| 120 | m_player->setCurrentSource(m_sounds[0]); | ||
| 121 | m_player->play(); | ||
| 122 | } | ||
| 123 | |||
| 106 | /* stop current animation */ | 124 | /* stop current animation */ |
| 107 | if (direction != m_direction) | 125 | if (direction != m_direction) |
| 108 | { | 126 | { |
| @@ -162,3 +180,27 @@ bool Actor::isMoving() | |||
| 162 | { | 180 | { |
| 163 | return (m_moving->state() == QAbstractAnimation::Running); | 181 | return (m_moving->state() == QAbstractAnimation::Running); |
| 164 | } | 182 | } |
| 183 | |||
| 184 | void Actor::enqueue() | ||
| 185 | { | ||
| 186 | if (isMoving()) | ||
| 187 | m_player->enqueue(m_sounds[0]); | ||
| 188 | } | ||
| 189 | |||
| 190 | void Actor::die() | ||
| 191 | { | ||
| 192 | if (!m_local) | ||
| 193 | return; | ||
| 194 | m_player->stop(); | ||
| 195 | m_player->setCurrentSource(m_sounds[1]); | ||
| 196 | m_player->play(); | ||
| 197 | } | ||
| 198 | |||
| 199 | void Actor::eatingCherry() | ||
| 200 | { | ||
| 201 | if (!m_local) | ||
| 202 | return; | ||
| 203 | m_player->stop(); | ||
| 204 | m_player->setCurrentSource(m_sounds[2]); | ||
| 205 | m_player->play(); | ||
| 206 | } | ||
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 68bf06f..36b2a49 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h | |||
| @@ -6,10 +6,14 @@ | |||
| 6 | #include <QtCore/QSequentialAnimationGroup> | 6 | #include <QtCore/QSequentialAnimationGroup> |
| 7 | #include <QtCore/QParallelAnimationGroup> | 7 | #include <QtCore/QParallelAnimationGroup> |
| 8 | #include <QList> | 8 | #include <QList> |
| 9 | #include <QFile> | ||
| 10 | #include <phonon/MediaObject> | ||
| 9 | 11 | ||
| 10 | class Actor | 12 | class Actor |
| 11 | : public PixmapItem | 13 | : public PixmapItem |
| 12 | { | 14 | { |
| 15 | Q_OBJECT | ||
| 16 | |||
| 13 | public: | 17 | public: |
| 14 | enum Movement { | 18 | enum Movement { |
| 15 | None = 0, | 19 | None = 0, |
| @@ -28,6 +32,11 @@ public: | |||
| 28 | bool isLocal(); | 32 | bool isLocal(); |
| 29 | void move(Movement direction); | 33 | void move(Movement direction); |
| 30 | bool isMoving(); | 34 | bool isMoving(); |
| 35 | void die(); | ||
| 36 | void eatingCherry(); | ||
| 37 | |||
| 38 | private slots: | ||
| 39 | void enqueue(); | ||
| 31 | 40 | ||
| 32 | private: | 41 | private: |
| 33 | QPixmap m_pix; | 42 | QPixmap m_pix; |
| @@ -39,6 +48,8 @@ private: | |||
| 39 | QList<PixmapItem *> m_images; | 48 | QList<PixmapItem *> m_images; |
| 40 | QList<QSequentialAnimationGroup *> m_eating; | 49 | QList<QSequentialAnimationGroup *> m_eating; |
| 41 | QParallelAnimationGroup *m_moving; | 50 | QParallelAnimationGroup *m_moving; |
| 51 | Phonon::MediaObject *m_player; | ||
| 52 | QList<QFile *> m_sounds; | ||
| 42 | }; | 53 | }; |
| 43 | 54 | ||
| 44 | #endif // ACTOR_H | 55 | #endif // ACTOR_H |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index d0b2ad7..73612af 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -1,11 +1,13 @@ | |||
| 1 | #include "mainwidget.h" | 1 | #include "mainwidget.h" |
| 2 | |||
| 3 | #include "actor.h" | 2 | #include "actor.h" |
| 4 | #include "block.h" | 3 | #include "block.h" |
| 5 | #include "bonuspoint.h" | 4 | #include "bonuspoint.h" |
| 6 | #include "point.h" | 5 | #include "point.h" |
| 7 | #include "constants.h" | 6 | #include "constants.h" |
| 8 | 7 | ||
| 8 | #include <phonon/MediaObject> | ||
| 9 | #include <QFile> | ||
| 10 | |||
| 9 | // temporary | 11 | // temporary |
| 10 | Transmission::map_t createDummyMap() | 12 | Transmission::map_t createDummyMap() |
| 11 | { | 13 | { |
| @@ -204,7 +206,7 @@ Transmission::map_t createDummyMap() | |||
| 204 | } | 206 | } |
| 205 | 207 | ||
| 206 | MainWidget::MainWidget() | 208 | MainWidget::MainWidget() |
| 207 | : m_currentKey(0) | 209 | : m_currentKey(0), m_running(false) |
| 208 | { | 210 | { |
| 209 | visualMap.resize(Constants::map_size.width); | 211 | visualMap.resize(Constants::map_size.width); |
| 210 | for (int i=0; i<visualMap.size(); ++i) { | 212 | for (int i=0; i<visualMap.size(); ++i) { |
| @@ -213,6 +215,15 @@ MainWidget::MainWidget() | |||
| 213 | 215 | ||
| 214 | createGui(); | 216 | createGui(); |
| 215 | updateMap(createDummyMap()); | 217 | updateMap(createDummyMap()); |
| 218 | |||
| 219 | #if 0 | ||
| 220 | emit startGame(); | ||
| 221 | #else | ||
| 222 | Phonon::MediaObject *player = Phonon::createPlayer(Phonon::MusicCategory, | ||
| 223 | Phonon::MediaSource(new QFile(":/sound/intro"))); | ||
| 224 | connect(player, SIGNAL(finished()), this, SLOT(startGame())); | ||
| 225 | player->play(); | ||
| 226 | #endif | ||
| 216 | } | 227 | } |
| 217 | 228 | ||
| 218 | void MainWidget::createGui() | 229 | void MainWidget::createGui() |
| @@ -321,7 +332,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 321 | if (actor == NULL) | 332 | if (actor == NULL) |
| 322 | { | 333 | { |
| 323 | //qDebug() << "new actor of col" << color; | 334 | //qDebug() << "new actor of col" << color; |
| 324 | actor = new Actor(color); | 335 | actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing |
| 325 | m_actors[color] = actor; | 336 | m_actors[color] = actor; |
| 326 | m_scene->addItem(actor); | 337 | m_scene->addItem(actor); |
| 327 | actor->setPos(mapPositionToCoord(x, y)); | 338 | actor->setPos(mapPositionToCoord(x, y)); |
| @@ -403,6 +414,9 @@ Transmission::field_t MainWidget::translateKey(int key) | |||
| 403 | 414 | ||
| 404 | void MainWidget::keyPressEvent(QKeyEvent* event) | 415 | void MainWidget::keyPressEvent(QKeyEvent* event) |
| 405 | { | 416 | { |
| 417 | if (!m_running) | ||
| 418 | return; | ||
| 419 | |||
| 406 | QWidget::keyPressEvent(event); | 420 | QWidget::keyPressEvent(event); |
| 407 | m_currentKey = translateKey(event->key()); | 421 | m_currentKey = translateKey(event->key()); |
| 408 | 422 | ||
| @@ -436,6 +450,9 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 436 | 450 | ||
| 437 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | 451 | void MainWidget::keyReleaseEvent(QKeyEvent* event) |
| 438 | { | 452 | { |
| 453 | if (!m_running) | ||
| 454 | return; | ||
| 455 | |||
| 439 | QWidget::keyReleaseEvent(event); | 456 | QWidget::keyReleaseEvent(event); |
| 440 | Transmission::field_t releasedKey = translateKey(event->key()); | 457 | Transmission::field_t releasedKey = translateKey(event->key()); |
| 441 | if (releasedKey == m_currentKey) | 458 | if (releasedKey == m_currentKey) |
| @@ -446,3 +463,8 @@ void MainWidget::keyReleaseEvent(QKeyEvent* event) | |||
| 446 | m_currentKey = Transmission::direction_none; | 463 | m_currentKey = Transmission::direction_none; |
| 447 | } | 464 | } |
| 448 | } | 465 | } |
| 466 | |||
| 467 | void MainWidget::startGame() | ||
| 468 | { | ||
| 469 | m_running = true; | ||
| 470 | } | ||
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index b15427c..6adcba2 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -1,11 +1,10 @@ | |||
| 1 | #ifndef MAINWIDGET_H | 1 | #ifndef MAINWIDGET_H |
| 2 | #define MAINWIDGET_H | 2 | #define MAINWIDGET_H |
| 3 | 3 | ||
| 4 | #include <QtGui> | ||
| 5 | #include <QtCore> | ||
| 6 | |||
| 7 | #include "constants.h" | 4 | #include "constants.h" |
| 8 | #include "pixmapitem.h" | 5 | #include "pixmapitem.h" |
| 6 | #include <QtGui> | ||
| 7 | #include <QtCore> | ||
| 9 | 8 | ||
| 10 | class Actor; | 9 | class Actor; |
| 11 | 10 | ||
| @@ -24,12 +23,15 @@ protected: | |||
| 24 | 23 | ||
| 25 | private: | 24 | private: |
| 26 | void createGui(); | 25 | void createGui(); |
| 27 | |||
| 28 | void updateMap(const Transmission::map_t& map); | 26 | void updateMap(const Transmission::map_t& map); |
| 29 | |||
| 30 | void updateScore(); | 27 | void updateScore(); |
| 28 | bool isRunning(); | ||
| 31 | 29 | ||
| 32 | QVector< QVector< PixmapItem* > > visualMap; | 30 | private slots: |
| 31 | void startGame(); | ||
| 32 | |||
| 33 | private: | ||
| 34 | QVector< QVector<PixmapItem *> > visualMap; | ||
| 33 | 35 | ||
| 34 | // data conversion | 36 | // data conversion |
| 35 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); | 37 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); |
| @@ -46,6 +48,9 @@ private: | |||
| 46 | 48 | ||
| 47 | // translate Qt::Key to our key format | 49 | // translate Qt::Key to our key format |
| 48 | Transmission::field_t translateKey(int); | 50 | Transmission::field_t translateKey(int); |
| 51 | |||
| 52 | // game running | ||
| 53 | bool m_running; | ||
| 49 | }; | 54 | }; |
| 50 | 55 | ||
| 51 | #endif // MAINWIDGET_H | 56 | #endif // MAINWIDGET_H |
diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc index 6de5cc0..3752ebe 100644 --- a/pacman-c++/pacman.qrc +++ b/pacman-c++/pacman.qrc | |||
| @@ -12,4 +12,10 @@ | |||
| 12 | <file alias="bonuspoints">pics/bonuspoints.png</file> | 12 | <file alias="bonuspoints">pics/bonuspoints.png</file> |
| 13 | <file alias="points">pics/points.png</file> | 13 | <file alias="points">pics/points.png</file> |
| 14 | </qresource> | 14 | </qresource> |
| 15 | <qresource prefix="/sound"> | ||
| 16 | <file alias="wakawaka">sound/waka_waka.ogg</file> | ||
| 17 | <file alias="die">sound/die.ogg</file> | ||
| 18 | <file alias="eatingcherry">sound/eating_cherry.ogg</file> | ||
| 19 | <file alias="intro">sound/intro.ogg</file> | ||
| 20 | </qresource> | ||
| 15 | </RCC> | 21 | </RCC> |
diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 35a6440..e576d77 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef __PIXMAPITEM__H__ | 1 | #ifndef PIXMAPITEM__H |
| 2 | #define __PIXMAPITEM__H__ | 2 | #define PIXMAPITEM__H |
| 3 | 3 | ||
| 4 | #include <QtGui/QGraphicsObject> | 4 | #include <QtGui/QGraphicsObject> |
| 5 | #include <QtGui/QGraphicsScene> | 5 | #include <QtGui/QGraphicsScene> |
diff --git a/pacman-c++/point.h b/pacman-c++/point.h index 847daed..1b5863a 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef POINT_H | 1 | #ifndef POINT_H |
| 2 | #define SPOINT_H | 2 | #define POINT_H |
| 3 | 3 | ||
| 4 | #include "pixmapitem.h" | 4 | #include "pixmapitem.h" |
| 5 | 5 | ||
