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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include "actor.h"
#include "animationmanager.h"
#include <QtCore/QPropertyAnimation>
#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, bool local, QGraphicsItem *parent)
: PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local),
m_player(NULL), m_roundPoints(0), m_gamePoints(0)
{
m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1);
/* DON'T set any pixmap here. we've a pixmap in the animation
* but we need a sprite for the collision detection
*/
setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height);
/* 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);
/* setup static images first
* they are visible if no animation is running
* and will be the first in m_images
*/
for (int i = 0; i < 5; i++)
{
PixmapItem *img = new PixmapItem(m_pix, this);
m_images.append(img);
int x = i * Constants::sprite_offset + Constants::sprite_margin;
int y = Actor::None * Constants::sprite_offset + Constants::sprite_margin;
img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height);
img->setZValue(zValue());
img->setVisible(false);
}
/* setup animation stuff */
qRegisterAnimationInterpolator<bool>(myBooleanInterpolator);
m_moving = new QParallelAnimationGroup(this);
m_eating.append(NULL); // Actor::None
m_eating.append(setupEatingAnimation(Actor::Left));
m_eating.append(setupEatingAnimation(Actor::Right));
m_eating.append(setupEatingAnimation(Actor::Up));
m_eating.append(setupEatingAnimation(Actor::Down));
/* setup sound */
if (local)
{
m_player = new AudioPlayer(this);
if (m_player->isWorking())
{
m_player->setLoop(Sound::WakaWaka);
AudioManager::self()->registerAudioPlayer(m_player);
}
}
/* make the picture showing the current direction visible */
m_images[m_direction]->setVisible(true);
}
QSequentialAnimationGroup *Actor::setupEatingAnimation(Actor::Movement direction)
{
QSequentialAnimationGroup *eating = new QSequentialAnimationGroup(this);
eating->setLoopCount(-1);
for (int i = 0; i < 4; i++)
{
PixmapItem *img = new PixmapItem(m_pix, this);
m_images.append(img);
int x = i * Constants::sprite_offset + Constants::sprite_margin;
int y = 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);
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", m_moving);
move->setDuration(Constants::tick - 30); //TODO
move->setEndValue(QPoint(0, 0));
}
return eating;
}
Color::Color Actor::getColor()
{
return m_color;
}
PixmapItem &Actor::getIcon()
{
return m_icon;
}
bool Actor::isLocal()
{
return m_local;
}
void Actor::move(Actor::Movement direction)
{
//TODO: remove?
//if (isMoving())
// return;
/* stop current animation */
if (direction != m_direction)
{
/* hide all pictures */
for (int i = 0; i < m_images.size(); ++i)
m_images.at(i)->setVisible(false);
if (m_eating[m_direction] != NULL)
{
m_eating[m_direction]->stop();
//AnimationManager::self()->unregisterAnimation(m_eating[m_direction]);
}
}
QPointF endpos(0, 0);
switch(direction)
{
case Actor::None:
break;
case Actor::Left:
endpos.setX(static_cast<qreal>(Constants::field_size.width) * -1);
break;
case Actor::Right:
endpos.setX(Constants::field_size.width);
break;
case Actor::Up:
endpos.setY(static_cast<qreal>(Constants::field_size.height) * -1);
break;
case Actor::Down:
endpos.setY(Constants::field_size.height);
break;
default:
Q_ASSERT(false);
break;
}
for(int i = 0; i < m_moving->animationCount(); ++i)
{
QPropertyAnimation *move = dynamic_cast<QPropertyAnimation *>(m_moving->animationAt(i));
move->setStartValue(QPoint(0, 0) - endpos);
}
setPos(pos() + endpos);
/* start new animation */
if (direction != m_direction)
{
if (direction == Actor::None)
m_images[m_direction]->setVisible(true);
else
m_eating[direction]->start();
}
if (direction != Actor::None)
{
if (m_local && m_player->isWorking() && m_player->state() != Phonon::PlayingState)
m_player->play();
m_moving->start();
}
else if (direction != m_direction)
{
if (m_local && m_player->isWorking() && m_player->state() != Phonon::PausedState)
m_player->pause();
}
m_direction = direction;
}
bool Actor::isMoving()
{
return (m_moving->state() == QAbstractAnimation::Running);
}
void Actor::die()
{
if (!m_local)
return;
AudioManager::self()->play(Sound::Die);
}
void Actor::eatingCherry()
{
if (!m_local)
return;
AudioManager::self()->play(Sound::EatingCherry);
}
unsigned int Actor::getRoundPoints()
{
return m_roundPoints;
}
unsigned int Actor::getGamePoints()
{
return m_gamePoints;
}
void Actor::addRoundPoints(unsigned int amount)
{
m_roundPoints += amount;
}
void Actor::finishRound()
{
m_gamePoints += m_roundPoints;
m_roundPoints = 0;
}
|