summaryrefslogtreecommitdiffstats
path: root/pacman-c++/actor.cpp
blob: 5080497dbc1aa3eb55c7f05d60406e89e5c278b9 (plain)
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
#include "actor.h"
#include "animationmanager.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QSequentialAnimationGroup>
#include <QtCore/QVariantAnimation>

#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, 16, 16);

  m_direction = Actor::Left;

  qRegisterAnimationInterpolator<bool>(myBooleanInterpolator);
  QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
  group->setLoopCount(-1);
  for (int i = 0; i < 4; i++)
  {
    PixmapItem *step = new PixmapItem(m_pix, this);
    int x = i * 20 + SPRITE_MARGIN;
    int y = m_direction * 20 + SPRITE_MARGIN;
    step->setSprite(x, y, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT);
    step->setVisible(false);


    QPropertyAnimation *anim = new QPropertyAnimation(step, "visible", group);
    anim->setDuration(0);
    anim->setEndValue(true);

    group->addPause(100);

    QPropertyAnimation *anim2 = new QPropertyAnimation(step, "visible", group);
    anim2->setDuration(0);
    anim2->setEndValue(false);
  }

  AnimationManager::self()->registerAnimation(group);
  group->start();
}