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++/actor.cpp | |
| 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++/actor.cpp')
| -rw-r--r-- | pacman-c++/actor.cpp | 127 |
1 files changed, 112 insertions, 15 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 | } | ||
