summaryrefslogtreecommitdiffstats
path: root/pacman-c++/actor.cpp
blob: 9d35d472d577145ff4984f24981a945b0a1c326f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "actor.h"
#include "animationmanager.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QSequentialAnimationGroup>
#include <QtCore/QParallelAnimationGroup>
#include <QtCore/QVariantAnimation>
#include <QDebug>

static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress)
{
  return (progress == 1.0) ? end : start;
}

Actor::Actor(Color::Color color, QGraphicsItem *parent)
  : PixmapItem(parent), m_color(color), m_direction(Actor::None)
{
  m_pix  = ":/" + QString("actor%1").arg(m_color);
  // DON'T set any pixmap here. we've a pixmap in the animation
  //setPixmap(m_pix);
  // higher player "over" lower player
  setZValue(m_color * 10);

  /* setup icon for player */
  m_icon.setPixmap(m_pix);
  m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height);

  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 * Constants::sprite_offset + Constants::sprite_margin;
    int y = m_direction * Constants::sprite_offset + Constants::sprite_margin;
    img->setSprite(x, y, Constants::field_size.width, Constants::field_size.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();
}

PixmapItem &Actor::getIcon()
{
  return m_icon;
}