-
Notifications
You must be signed in to change notification settings - Fork 0
/
animationsprite.cpp
115 lines (91 loc) · 3.44 KB
/
animationsprite.cpp
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
#include "animationsprite.h"
AnimationSprite::AnimationSprite(TimerProxy *tpro, QString name, QGraphicsScene *parent, Maze *mzWidget) :
mSubRect(0, 0, 1, 1), mCurrFrame(0), scene(parent), mazeWidget(mzWidget)
{
// convert JSON file to object
QFile jsonfile;
jsonfile.setFileName(name);
jsonfile.open(QIODevice::ReadOnly | QIODevice::Text);
QJsonParseError jsonError;
QString val = jsonfile.readAll();
chrJsonDoc = QJsonDocument::fromJson(val.toUtf8(), &jsonError);
if (jsonError.error) {
qWarning() << jsonError.errorString();
return;
}
jsonfile.close();
chrJsonObj = chrJsonDoc.object();
// parse animation descriptions
QJsonArray chrJsonArr = chrJsonObj["behaviors"].toArray();
foreach (const QJsonValue &val, chrJsonArr) {
QJsonObject obj = val.toObject();
if (obj["name"].toString() == "SpriteAnimationBehavior") {
animsArr = obj["params"].toObject()["anims"].toArray();
}
}
// connect SIGNAL(TimerProxy::updateTime(int msecs)) to SLOT(timeUpdated(int))
connect(tpro, SIGNAL(updateTime(int)), this, SLOT(timeUpdated(int)));
// assign margin offset / cell unit size
offset = (GAME_WIDTH-GAME_HEIGHT)/2;
cell_unit = MAZE_SIZE/(2*MAZE_PASSAGE_SIZE+1);
}
AnimationSprite::~AnimationSprite() {
}
void AnimationSprite::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
// whichever version of drawPixmap/drawImage is up to you
// void QPainter::drawPixmap(QRectF, QPixmap)
}
QRectF AnimationSprite::boundingRect() const {
return mSubRect;
}
void AnimationSprite::setSubRect(QRect newRect) {
mSubRect = newRect;
}
/**
* @brief change the current animation,
* reset the elapsed time counter,
* update the current sub-rect to draw to match the first frame of the new animation
*
* @param animName
*/
void AnimationSprite::startAnim(const QString animName) {
mAnimName = animName;
// parse current animation object, assign to mCurrentAnimation
foreach(const QJsonValue &val, animsArr) {
QJsonObject obj = val.toObject();
if (obj["animName"].toString() == animName) {
mCurrAnim = obj["animFrames"].toArray().at(mCurrFrame).toObject();
}
}
// assign to mSubRect
mSubRectArr = mCurrAnim["rect"].toArray();
mSubRect = QRect(mSubRectArr.at(0).toInt(),
mSubRectArr.at(1).toInt(),
mSubRectArr.at(2).toInt(),
mSubRectArr.at(3).toInt());
// reset timer
// ...
}
void AnimationSprite::timeUpdated(int msecs) {
prepareGeometryChange();
// parse current animation object, assign to mCurrentAnimation
// and increment frame
foreach(const QJsonValue &val, animsArr) {
QJsonObject obj = val.toObject();
if (obj["animName"].toString() == mAnimName) {
if (mCurrFrame < obj["animFrames"].toArray().size()-1) {
++mCurrFrame;
} else {
mCurrFrame = 0;
}
mCurrAnim = obj["animFrames"].toArray().at(mCurrFrame).toObject();
// update mSubRect
mSubRectArr = mCurrAnim["rect"].toArray();
setSubRect(QRect(mSubRectArr.at(0).toInt(),
mSubRectArr.at(1).toInt(),
mSubRectArr.at(2).toInt(),
mSubRectArr.at(3).toInt()));
}
}
scene->update();
}