diff options
Diffstat (limited to 'pacman-c++/actor.cpp')
| -rw-r--r-- | pacman-c++/actor.cpp | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 860f490..5080497 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -1,7 +1,50 @@ | |||
| 1 | #include "actor.h" | 1 | #include "actor.h" |
| 2 | #include "animationmanager.h" | ||
| 3 | #include <QtCore/QPropertyAnimation> | ||
| 4 | #include <QtCore/QSequentialAnimationGroup> | ||
| 5 | #include <QtCore/QVariantAnimation> | ||
| 6 | |||
| 7 | #define SPRITE_MARGIN 2 | ||
| 8 | #define SPRITE_PLAYER_WIDTH 16 | ||
| 9 | #define SPRITE_PLAYER_HEIGHT 16 | ||
| 10 | |||
| 11 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) | ||
| 12 | { | ||
| 13 | return (progress == 1.0) ? end : start; | ||
| 14 | } | ||
| 2 | 15 | ||
| 3 | Actor::Actor(Type type) | 16 | Actor::Actor(Type type) |
| 4 | : PixmapItem("google-pacman-sprite"), m_type(type), m_direction(Actor::None) | 17 | : m_type(type), m_direction(Actor::None) |
| 5 | { | 18 | { |
| 6 | setSprite(2, 2, 16, 16); | 19 | m_pix = ":/" + QString("actor%1").arg(type); |
| 20 | setPixmap(m_pix); | ||
| 21 | setSprite(82, 82, 16, 16); | ||
| 22 | |||
| 23 | m_direction = Actor::Left; | ||
| 24 | |||
| 25 | qRegisterAnimationInterpolator<bool>(myBooleanInterpolator); | ||
| 26 | QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this); | ||
| 27 | group->setLoopCount(-1); | ||
| 28 | for (int i = 0; i < 4; i++) | ||
| 29 | { | ||
| 30 | PixmapItem *step = new PixmapItem(m_pix, this); | ||
| 31 | int x = i * 20 + SPRITE_MARGIN; | ||
| 32 | int y = m_direction * 20 + SPRITE_MARGIN; | ||
| 33 | step->setSprite(x, y, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT); | ||
| 34 | step->setVisible(false); | ||
| 35 | |||
| 36 | |||
| 37 | QPropertyAnimation *anim = new QPropertyAnimation(step, "visible", group); | ||
| 38 | anim->setDuration(0); | ||
| 39 | anim->setEndValue(true); | ||
| 40 | |||
| 41 | group->addPause(100); | ||
| 42 | |||
| 43 | QPropertyAnimation *anim2 = new QPropertyAnimation(step, "visible", group); | ||
| 44 | anim2->setDuration(0); | ||
| 45 | anim2->setEndValue(false); | ||
| 46 | } | ||
| 47 | |||
| 48 | AnimationManager::self()->registerAnimation(group); | ||
| 49 | group->start(); | ||
| 7 | } | 50 | } |
