1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include "actor.h"
#include "animationmanager.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QSequentialAnimationGroup>
#include <QtCore/QParallelAnimationGroup>
#include <QtCore/QVariantAnimation>
#include <QDebug>
#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, QGraphicsItem *parent)
: PixmapItem(parent), m_type(type), m_direction(Actor::None)
{
m_pix = ":/" + QString("actor%1").arg(m_type);
// DON'T set any pixmap here. we've a pixmap in the animation
//setPixmap(m_pix);
// higher player "over" lower player
setZValue(m_type * 10);
m_direction = Actor::Left;
qRegisterAnimationInterpolator<bool>(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->setZValue(zValue());
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(5000);
move->setEndValue(QPointF(0, 0));
}
AnimationManager::self()->registerAnimation(eating);
eating->start();
moving->start();
}
|