diff options
| author | manuel <manuel@mausz.at> | 2011-04-06 23:29:31 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-06 23:29:31 +0200 |
| commit | da6ef5d87e1cafc0a7de658dc65a9a4578e28f6c (patch) | |
| tree | 1284bdb56f88c3256abd13c911427ae0ff6b49a6 /pacman-c++ | |
| parent | 079c0ef93c6c72c448120b805d7ebddeaa56a908 (diff) | |
| download | foop-da6ef5d87e1cafc0a7de658dc65a9a4578e28f6c.tar.gz foop-da6ef5d87e1cafc0a7de658dc65a9a4578e28f6c.tar.bz2 foop-da6ef5d87e1cafc0a7de658dc65a9a4578e28f6c.zip | |
- make a few optimizations to actor sprites
- make actor move via keyboard!
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/actor.cpp | 127 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 20 | ||||
| -rw-r--r-- | pacman-c++/constants.h | 13 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 97 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 2 | ||||
| -rw-r--r-- | pacman-c++/pics/actor1.png | bin | 704 -> 796 bytes | |||
| -rw-r--r-- | pacman-c++/pics/actor2.png | bin | 711 -> 805 bytes | |||
| -rw-r--r-- | pacman-c++/pics/actor3.png | bin | 703 -> 793 bytes | |||
| -rw-r--r-- | pacman-c++/pics/actor4.png | bin | 706 -> 804 bytes |
9 files changed, 203 insertions, 56 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index cde20cd..1796091 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -1,8 +1,6 @@ | |||
| 1 | #include "actor.h" | 1 | #include "actor.h" |
| 2 | #include "animationmanager.h" | 2 | #include "animationmanager.h" |
| 3 | #include <QtCore/QPropertyAnimation> | 3 | #include <QtCore/QPropertyAnimation> |
| 4 | #include <QtCore/QSequentialAnimationGroup> | ||
| 5 | #include <QtCore/QParallelAnimationGroup> | ||
| 6 | #include <QtCore/QVariantAnimation> | 4 | #include <QtCore/QVariantAnimation> |
| 7 | #include <QDebug> | 5 | #include <QDebug> |
| 8 | 6 | ||
| @@ -11,8 +9,8 @@ static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal | |||
| 11 | return (progress == 1.0) ? end : start; | 9 | return (progress == 1.0) ? end : start; |
| 12 | } | 10 | } |
| 13 | 11 | ||
| 14 | Actor::Actor(Color::Color color, QGraphicsItem *parent) | 12 | Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) |
| 15 | : PixmapItem(parent), m_color(color), m_direction(Actor::None) | 13 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local) |
| 16 | { | 14 | { |
| 17 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); | 15 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); |
| 18 | // DON'T set any pixmap here. we've a pixmap in the animation | 16 | // DON'T set any pixmap here. we've a pixmap in the animation |
| @@ -24,23 +22,47 @@ Actor::Actor(Color::Color color, QGraphicsItem *parent) | |||
| 24 | m_icon.setPixmap(m_pix); | 22 | m_icon.setPixmap(m_pix); |
| 25 | m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | 23 | m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); |
| 26 | 24 | ||
| 27 | m_direction = Actor::Left; | 25 | /* setup static images first |
| 26 | * they are visible if no animation is running | ||
| 27 | * and will be the first in m_images | ||
| 28 | */ | ||
| 29 | for (int i = 0; i < 5; i++) | ||
| 30 | { | ||
| 31 | PixmapItem *img = new PixmapItem(m_pix, this); | ||
| 32 | m_images.append(img); | ||
| 33 | int x = i * Constants::sprite_offset + Constants::sprite_margin; | ||
| 34 | int y = Actor::None * Constants::sprite_offset + Constants::sprite_margin; | ||
| 35 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); | ||
| 36 | img->setZValue(zValue()); | ||
| 37 | img->setVisible(false); | ||
| 38 | } | ||
| 28 | 39 | ||
| 40 | /* setup animation stuff */ | ||
| 29 | qRegisterAnimationInterpolator<bool>(myBooleanInterpolator); | 41 | qRegisterAnimationInterpolator<bool>(myBooleanInterpolator); |
| 42 | m_moving = new QParallelAnimationGroup(this); | ||
| 43 | m_eating.append(NULL); // Actor::None | ||
| 44 | m_eating.append(setupEatingAnimation(Actor::Left)); | ||
| 45 | m_eating.append(setupEatingAnimation(Actor::Right)); | ||
| 46 | m_eating.append(setupEatingAnimation(Actor::Up)); | ||
| 47 | m_eating.append(setupEatingAnimation(Actor::Down)); | ||
| 48 | |||
| 49 | /* make the picture showing the current direction visible */ | ||
| 50 | m_images[m_direction]->setVisible(true); | ||
| 51 | } | ||
| 30 | 52 | ||
| 53 | QSequentialAnimationGroup *Actor::setupEatingAnimation(Actor::Movement direction) | ||
| 54 | { | ||
| 31 | QSequentialAnimationGroup *eating = new QSequentialAnimationGroup(this); | 55 | QSequentialAnimationGroup *eating = new QSequentialAnimationGroup(this); |
| 32 | QParallelAnimationGroup *moving = new QParallelAnimationGroup(this); | ||
| 33 | eating->setLoopCount(-1); | 56 | eating->setLoopCount(-1); |
| 34 | |||
| 35 | for (int i = 0; i < 4; i++) | 57 | for (int i = 0; i < 4; i++) |
| 36 | { | 58 | { |
| 37 | PixmapItem *img = new PixmapItem(m_pix, this); | 59 | PixmapItem *img = new PixmapItem(m_pix, this); |
| 60 | m_images.append(img); | ||
| 38 | int x = i * Constants::sprite_offset + Constants::sprite_margin; | 61 | int x = i * Constants::sprite_offset + Constants::sprite_margin; |
| 39 | int y = m_direction * Constants::sprite_offset + Constants::sprite_margin; | 62 | int y = direction * Constants::sprite_offset + Constants::sprite_margin; |
| 40 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); | 63 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); |
| 41 | img->setZValue(zValue()); | 64 | img->setZValue(zValue()); |
| 42 | img->setVisible(false); | 65 | img->setVisible(false); |
| 43 | img->setPos(QPointF(200, 0)); | ||
| 44 | 66 | ||
| 45 | QPropertyAnimation *fadein = new QPropertyAnimation(img, "visible", eating); | 67 | QPropertyAnimation *fadein = new QPropertyAnimation(img, "visible", eating); |
| 46 | fadein->setDuration(0); | 68 | fadein->setDuration(0); |
| @@ -52,17 +74,92 @@ Actor::Actor(Color::Color color, QGraphicsItem *parent) | |||
| 52 | fadeout->setDuration(0); | 74 | fadeout->setDuration(0); |
| 53 | fadeout->setEndValue(false); | 75 | fadeout->setEndValue(false); |
| 54 | 76 | ||
| 55 | QPropertyAnimation *move = new QPropertyAnimation(img, "pos", moving); | 77 | QPropertyAnimation *move = new QPropertyAnimation(img, "pos", m_moving); |
| 56 | move->setDuration(5000); | 78 | move->setDuration(Constants::tick); |
| 57 | move->setEndValue(QPointF(0, 0)); | ||
| 58 | } | 79 | } |
| 59 | 80 | ||
| 60 | AnimationManager::self()->registerAnimation(eating); | 81 | return eating; |
| 61 | eating->start(); | 82 | } |
| 62 | moving->start(); | 83 | |
| 84 | Color::Color Actor::getColor() | ||
| 85 | { | ||
| 86 | return m_color; | ||
| 63 | } | 87 | } |
| 64 | 88 | ||
| 65 | PixmapItem &Actor::getIcon() | 89 | PixmapItem &Actor::getIcon() |
| 66 | { | 90 | { |
| 67 | return m_icon; | 91 | return m_icon; |
| 68 | } | 92 | } |
| 93 | |||
| 94 | bool Actor::isLocal() | ||
| 95 | { | ||
| 96 | return m_local; | ||
| 97 | } | ||
| 98 | |||
| 99 | void Actor::move(Actor::Movement direction) | ||
| 100 | { | ||
| 101 | //TODO: remove? | ||
| 102 | if (isMoving()) | ||
| 103 | return; | ||
| 104 | |||
| 105 | /* stop current animation */ | ||
| 106 | if (direction != m_direction) | ||
| 107 | { | ||
| 108 | /* hide all pictures */ | ||
| 109 | for (int i = 0; i < m_images.size(); ++i) | ||
| 110 | m_images.at(i)->setVisible(false); | ||
| 111 | |||
| 112 | if (m_eating[m_direction] != NULL) | ||
| 113 | { | ||
| 114 | m_eating[m_direction]->stop(); | ||
| 115 | //AnimationManager::self()->unregisterAnimation(m_eating[m_direction]); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | QPointF endpos(0, 0); | ||
| 120 | switch(direction) | ||
| 121 | { | ||
| 122 | case Actor::None: | ||
| 123 | break; | ||
| 124 | case Actor::Left: | ||
| 125 | endpos.setX(static_cast<qreal>(Constants::field_size.width) * -1); | ||
| 126 | break; | ||
| 127 | case Actor::Right: | ||
| 128 | endpos.setX(Constants::field_size.width); | ||
| 129 | break; | ||
| 130 | case Actor::Up: | ||
| 131 | endpos.setY(static_cast<qreal>(Constants::field_size.height) * -1); | ||
| 132 | break; | ||
| 133 | case Actor::Down: | ||
| 134 | endpos.setY(Constants::field_size.height); | ||
| 135 | break; | ||
| 136 | default: | ||
| 137 | Q_ASSERT(false); | ||
| 138 | break; | ||
| 139 | } | ||
| 140 | |||
| 141 | qDebug() << __FILE__ << "Move from" << pos() - endpos << "to" << (pos()) << "(" << endpos << ")"; | ||
| 142 | for(int i = 0; i < m_moving->animationCount(); ++i) | ||
| 143 | { | ||
| 144 | QPropertyAnimation *move = dynamic_cast<QPropertyAnimation *>(m_moving->animationAt(i)); | ||
| 145 | move->setStartValue(QPoint(0, 0) - endpos); | ||
| 146 | move->setEndValue(QPoint(0, 0)); | ||
| 147 | } | ||
| 148 | setPos(pos() + endpos); | ||
| 149 | |||
| 150 | /* start new animation */ | ||
| 151 | if (direction != m_direction) | ||
| 152 | { | ||
| 153 | if (direction == Actor::None) | ||
| 154 | m_images[m_direction]->setVisible(true); | ||
| 155 | else | ||
| 156 | m_eating[direction]->start(); | ||
| 157 | m_direction = direction; | ||
| 158 | } | ||
| 159 | m_moving->start(); | ||
| 160 | } | ||
| 161 | |||
| 162 | bool Actor::isMoving() | ||
| 163 | { | ||
| 164 | return (m_moving->state() == QAbstractAnimation::Running); | ||
| 165 | } | ||
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 39ccb88..68bf06f 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h | |||
| @@ -3,30 +3,42 @@ | |||
| 3 | 3 | ||
| 4 | #include "pixmapitem.h" | 4 | #include "pixmapitem.h" |
| 5 | #include "constants.h" | 5 | #include "constants.h" |
| 6 | #include <QtCore/QSequentialAnimationGroup> | ||
| 7 | #include <QtCore/QParallelAnimationGroup> | ||
| 8 | #include <QList> | ||
| 6 | 9 | ||
| 7 | class Actor | 10 | class Actor |
| 8 | : public PixmapItem | 11 | : public PixmapItem |
| 9 | { | 12 | { |
| 10 | public: | 13 | public: |
| 11 | enum Movement { | 14 | enum Movement { |
| 12 | None = 0, | 15 | None = 0, |
| 13 | Left, | 16 | Left, |
| 14 | Right, | 17 | Right, |
| 15 | Up, | 18 | Up, |
| 16 | Down | 19 | Down, |
| 17 | }; | 20 | }; |
| 18 | 21 | ||
| 19 | Actor(Color::Color color, QGraphicsItem *parent = 0); | 22 | Actor(Color::Color color, bool local = false, QGraphicsItem *parent = 0); |
| 20 | virtual ~Actor() {}; | 23 | virtual ~Actor() {}; |
| 21 | 24 | ||
| 25 | QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); | ||
| 26 | Color::Color getColor(); | ||
| 22 | PixmapItem &getIcon(); | 27 | PixmapItem &getIcon(); |
| 23 | Color::Color getColor() { return m_color; } | 28 | bool isLocal(); |
| 29 | void move(Movement direction); | ||
| 30 | bool isMoving(); | ||
| 24 | 31 | ||
| 25 | private: | 32 | private: |
| 26 | QPixmap m_pix; | 33 | QPixmap m_pix; |
| 27 | Color::Color m_color; | 34 | Color::Color m_color; |
| 28 | Movement m_direction; | 35 | Movement m_direction; |
| 29 | PixmapItem m_icon; | 36 | PixmapItem m_icon; |
| 37 | bool m_local; | ||
| 38 | |||
| 39 | QList<PixmapItem *> m_images; | ||
| 40 | QList<QSequentialAnimationGroup *> m_eating; | ||
| 41 | QParallelAnimationGroup *m_moving; | ||
| 30 | }; | 42 | }; |
| 31 | 43 | ||
| 32 | #endif // ACTOR_H | 44 | #endif // ACTOR_H |
diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index 019ac14..f0e7903 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h | |||
| @@ -4,13 +4,16 @@ | |||
| 4 | namespace Constants { | 4 | namespace Constants { |
| 5 | const struct | 5 | const struct |
| 6 | { | 6 | { |
| 7 | const unsigned int width, height; | 7 | const unsigned int width; |
| 8 | } field_size = { 16, 16 }, | 8 | const unsigned int height; |
| 9 | map_size = { 30, 30 }, | 9 | } |
| 10 | map_size_pixel = { field_size.width * map_size.width, | 10 | field_size = { 16, 16 }, |
| 11 | field_size.height * map_size.height}; | 11 | map_size = { 30, 30 }, |
| 12 | map_size_pixel = { field_size.width * map_size.width, | ||
| 13 | field_size.height * map_size.height }; | ||
| 12 | const unsigned int sprite_margin = 2; | 14 | const unsigned int sprite_margin = 2; |
| 13 | const unsigned int sprite_offset = 20; | 15 | const unsigned int sprite_offset = 20; |
| 16 | const unsigned int tick = 250; // ms | ||
| 14 | } | 17 | } |
| 15 | 18 | ||
| 16 | namespace Color | 19 | namespace Color |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 343fe4c..399d13c 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -179,21 +179,21 @@ Transmission::map_t createDummyMap() | |||
| 179 | 179 | ||
| 180 | map[15][15] |= Color::red; | 180 | map[15][15] |= Color::red; |
| 181 | map[15][15] |= Transmission::pacman; | 181 | map[15][15] |= Transmission::pacman; |
| 182 | map[15][15] |= Transmission::direction_left; | 182 | map[15][15] |= Transmission::direction_right; |
| 183 | 183 | ||
| 184 | map[16][15] |= Color::blue; | 184 | /*map[16][15] |= Color::blue; |
| 185 | map[16][15] |= Transmission::pacman; | 185 | map[16][15] |= Transmission::pacman; |
| 186 | map[16][15] |= Transmission::direction_left; | 186 | map[16][15] |= Transmission::direction_up; |
| 187 | 187 | ||
| 188 | map[17][15] |= Color::green; | 188 | map[17][15] |= Color::green; |
| 189 | map[17][15] |= Transmission::pacman; | 189 | map[17][15] |= Transmission::pacman; |
| 190 | map[17][15] |= Transmission::direction_left; | 190 | map[17][15] |= Transmission::direction_left;*/ |
| 191 | 191 | ||
| 192 | return map; | 192 | return map; |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | MainWidget::MainWidget() | 195 | MainWidget::MainWidget() |
| 196 | : currentKey(0) | 196 | : m_currentKey(0) |
| 197 | { | 197 | { |
| 198 | visualMap.resize(Constants::map_size.width); | 198 | visualMap.resize(Constants::map_size.width); |
| 199 | for (int i=0; i<visualMap.size(); ++i) { | 199 | for (int i=0; i<visualMap.size(); ++i) { |
| @@ -237,6 +237,7 @@ void MainWidget::createGui() | |||
| 237 | //window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 237 | //window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 238 | //window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 238 | //window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 239 | window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); | 239 | window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); |
| 240 | window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); | ||
| 240 | 241 | ||
| 241 | layout->addLayout(m_scoreLayout); | 242 | layout->addLayout(m_scoreLayout); |
| 242 | layout->addWidget(window); | 243 | layout->addWidget(window); |
| @@ -251,19 +252,19 @@ void MainWidget::updateScore() | |||
| 251 | while (i.hasNext()) | 252 | while (i.hasNext()) |
| 252 | { | 253 | { |
| 253 | i.next(); | 254 | i.next(); |
| 254 | int id = (i.key() >> 1); | 255 | if (i.key() > Color::max) |
| 255 | if (id > (Color::max >> 1)) | ||
| 256 | { | 256 | { |
| 257 | /* player #4 isn't supported in score */ | 257 | /* player #4 isn't supported in score */ |
| 258 | Q_ASSERT(false); | 258 | Q_ASSERT(false); |
| 259 | continue; | 259 | continue; |
| 260 | } | 260 | } |
| 261 | int id = (i.key() >> 1); | ||
| 261 | QLabel *turnPointsLbl = | 262 | QLabel *turnPointsLbl = |
| 262 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); | 263 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget()); |
| 263 | QLabel *allPointsLbl = | 264 | QLabel *allPointsLbl = |
| 264 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); | 265 | dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget()); |
| 265 | turnPointsLbl->setText(QString::number(id * 100 * qrand())); | 266 | turnPointsLbl->setText(QString::number(id)); |
| 266 | allPointsLbl->setText(QString::number(id * 200 * qrand())); | 267 | allPointsLbl->setText(QString::number(id)); |
| 267 | } | 268 | } |
| 268 | } | 269 | } |
| 269 | 270 | ||
| @@ -303,8 +304,9 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 303 | item = new BonusPoint(); | 304 | item = new BonusPoint(); |
| 304 | else if (cur & Transmission::pacman) | 305 | else if (cur & Transmission::pacman) |
| 305 | { | 306 | { |
| 306 | Actor *actor = m_actors.value(color, 0); | 307 | Actor *actor = m_actors.value(color, NULL); |
| 307 | if (actor == 0) { // 0 entspricht NULL ;) | 308 | if (actor == NULL) |
| 309 | { | ||
| 308 | //qDebug() << "new actor of col" << color; | 310 | //qDebug() << "new actor of col" << color; |
| 309 | actor = new Actor(color); | 311 | actor = new Actor(color); |
| 310 | m_actors[color] = actor; | 312 | m_actors[color] = actor; |
| @@ -312,7 +314,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 312 | actor->setPos(mapPositionToCoord(x, y)); | 314 | actor->setPos(mapPositionToCoord(x, y)); |
| 313 | } | 315 | } |
| 314 | 316 | ||
| 315 | Actor::Movement direction; | 317 | Actor::Movement direction = Actor::None; |
| 316 | switch (cur & Transmission::direction_mask) | 318 | switch (cur & Transmission::direction_mask) |
| 317 | { | 319 | { |
| 318 | case Transmission::direction_none: | 320 | case Transmission::direction_none: |
| @@ -333,7 +335,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) | |||
| 333 | default: | 335 | default: |
| 334 | Q_ASSERT(false); | 336 | Q_ASSERT(false); |
| 335 | } | 337 | } |
| 336 | //actor->move(direction, mapPositionToCoord(x, y); | 338 | //actor->move(direction, mapPositionToCoord(x, y)); |
| 337 | } | 339 | } |
| 338 | else | 340 | else |
| 339 | Q_ASSERT(false); | 341 | Q_ASSERT(false); |
| @@ -363,38 +365,71 @@ QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | |||
| 363 | 365 | ||
| 364 | Transmission::field_t MainWidget::translateKey(int key) | 366 | Transmission::field_t MainWidget::translateKey(int key) |
| 365 | { | 367 | { |
| 366 | switch(key) { | 368 | switch(key) |
| 367 | case Qt::Key_Up: | 369 | { |
| 368 | return Transmission::direction_up; | 370 | case Qt::Key_W: |
| 369 | break; | 371 | case Qt::Key_Up: |
| 370 | case Qt::Key_Down: | 372 | return Transmission::direction_up; |
| 371 | return Transmission::direction_down; | 373 | break; |
| 372 | break; | 374 | case Qt::Key_S: |
| 373 | case Qt::Key_Left: | 375 | case Qt::Key_Down: |
| 374 | return Transmission::direction_left; | 376 | return Transmission::direction_down; |
| 375 | break; | 377 | break; |
| 376 | case Qt::Key_Right: | 378 | case Qt::Key_A: |
| 377 | return Transmission::direction_right; | 379 | case Qt::Key_Left: |
| 378 | break; | 380 | return Transmission::direction_left; |
| 379 | default: | 381 | break; |
| 380 | return 0; | 382 | case Qt::Key_D: |
| 383 | case Qt::Key_Right: | ||
| 384 | return Transmission::direction_right; | ||
| 385 | break; | ||
| 386 | default: | ||
| 387 | return 0; | ||
| 381 | } | 388 | } |
| 382 | } | 389 | } |
| 383 | 390 | ||
| 384 | void MainWidget::keyPressEvent(QKeyEvent* event) | 391 | void MainWidget::keyPressEvent(QKeyEvent* event) |
| 385 | { | 392 | { |
| 386 | QWidget::keyPressEvent(event); | 393 | QWidget::keyPressEvent(event); |
| 387 | currentKey = translateKey( event->key() ); | 394 | m_currentKey = translateKey(event->key()); |
| 395 | |||
| 396 | // test stuff | ||
| 397 | Actor::Movement mov = Actor::None; | ||
| 398 | switch(m_currentKey) | ||
| 399 | { | ||
| 400 | case Transmission::direction_up: | ||
| 401 | mov = Actor::Up; | ||
| 402 | break; | ||
| 403 | case Transmission::direction_down: | ||
| 404 | mov = Actor::Down; | ||
| 405 | break; | ||
| 406 | case Transmission::direction_left: | ||
| 407 | mov = Actor::Left; | ||
| 408 | break; | ||
| 409 | case Transmission::direction_right: | ||
| 410 | mov = Actor::Right; | ||
| 411 | break; | ||
| 412 | default: | ||
| 413 | break; | ||
| 414 | } | ||
| 415 | |||
| 416 | QMapIterator<Color::Color, Actor*> i(m_actors); | ||
| 417 | while (i.hasNext()) | ||
| 418 | { | ||
| 419 | i.next(); | ||
| 420 | i.value()->move(mov); | ||
| 421 | } | ||
| 388 | } | 422 | } |
| 389 | 423 | ||
| 390 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | 424 | void MainWidget::keyReleaseEvent(QKeyEvent* event) |
| 391 | { | 425 | { |
| 392 | QWidget::keyReleaseEvent(event); | 426 | QWidget::keyReleaseEvent(event); |
| 393 | Transmission::field_t releasedKey = translateKey(event->key()); | 427 | Transmission::field_t releasedKey = translateKey(event->key()); |
| 394 | if (releasedKey == currentKey) { | 428 | if (releasedKey == m_currentKey) |
| 429 | { | ||
| 395 | // current key got released | 430 | // current key got released |
| 396 | // if this is false, a key got released which has already | 431 | // if this is false, a key got released which has already |
| 397 | // been replaced by a different key, so this case is disregarded | 432 | // been replaced by a different key, so this case is disregarded |
| 398 | currentKey = 0; | 433 | m_currentKey = Transmission::direction_none; |
| 399 | } | 434 | } |
| 400 | } | 435 | } |
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index 951b6c6..b15427c 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -42,7 +42,7 @@ private: | |||
| 42 | QMap<Color::Color, Actor*> m_actors; | 42 | QMap<Color::Color, Actor*> m_actors; |
| 43 | 43 | ||
| 44 | // key currently pressed by user | 44 | // key currently pressed by user |
| 45 | Transmission::field_t currentKey; | 45 | Transmission::field_t m_currentKey; |
| 46 | 46 | ||
| 47 | // translate Qt::Key to our key format | 47 | // translate Qt::Key to our key format |
| 48 | Transmission::field_t translateKey(int); | 48 | Transmission::field_t translateKey(int); |
diff --git a/pacman-c++/pics/actor1.png b/pacman-c++/pics/actor1.png index b1304cd..ae9f172 100644 --- a/pacman-c++/pics/actor1.png +++ b/pacman-c++/pics/actor1.png | |||
| Binary files differ | |||
diff --git a/pacman-c++/pics/actor2.png b/pacman-c++/pics/actor2.png index cac3786..07ecb3f 100644 --- a/pacman-c++/pics/actor2.png +++ b/pacman-c++/pics/actor2.png | |||
| Binary files differ | |||
diff --git a/pacman-c++/pics/actor3.png b/pacman-c++/pics/actor3.png index 99e025c..486fd37 100644 --- a/pacman-c++/pics/actor3.png +++ b/pacman-c++/pics/actor3.png | |||
| Binary files differ | |||
diff --git a/pacman-c++/pics/actor4.png b/pacman-c++/pics/actor4.png index 9947f9b..9684fc0 100644 --- a/pacman-c++/pics/actor4.png +++ b/pacman-c++/pics/actor4.png | |||
| Binary files differ | |||
