#include "actor.h" #include "animationmanager.h" #include #include #include #include #include #define SPRITE_MARGIN 2 #define SPRITE_PLAYER_WIDTH 16 #define SPRITE_PLAYER_HEIGHT 16 static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) { return (progress == 1.0) ? end : start; } Actor::Actor(Type type) : m_type(type), m_direction(Actor::None) { m_pix = ":/" + QString("actor%1").arg(type); setPixmap(m_pix); setSprite(82, 82, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT); m_direction = Actor::Left; qRegisterAnimationInterpolator(myBooleanInterpolator); QSequentialAnimationGroup *eating = new QSequentialAnimationGroup(this); QParallelAnimationGroup *moving = new QParallelAnimationGroup(this); eating->setLoopCount(-1); for (int i = 0; i < 4; i++) { PixmapItem *img = new PixmapItem(m_pix, this); int x = i * 20 + SPRITE_MARGIN; int y = m_direction * 20 + SPRITE_MARGIN; img->setSprite(x, y, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT); img->setVisible(false); img->setPos(QPointF(200, 0)); QPropertyAnimation *fadein = new QPropertyAnimation(img, "visible", eating); fadein->setDuration(0); fadein->setEndValue(true); eating->addPause(100); QPropertyAnimation *fadeout = new QPropertyAnimation(img, "visible", eating); fadeout->setDuration(0); fadeout->setEndValue(false); QPropertyAnimation *move = new QPropertyAnimation(img, "pos", moving); move->setDuration(10000); move->setEndValue(QPointF(0, 0)); } AnimationManager::self()->registerAnimation(eating); eating->start(); moving->start(); }