-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAnimatedGeometryEntity.cpp
More file actions
138 lines (121 loc) · 4.5 KB
/
CAnimatedGeometryEntity.cpp
File metadata and controls
138 lines (121 loc) · 4.5 KB
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
#include "CAnimatedGeometryEntity.h"
#include "QGraphicsAnimatedSprite.h"
#include "CResource.h"
#include "Globals.h"
#include <QDebug>
#include <QVariant>
#include <QMessageBox>
const QString CAnimatedGeometryEntity::PROP_KEY_ANIMATION_NAME = "anim_name";
const QString CAnimatedGeometryEntity::PROP_KEY_RESOURCE = "resource";
//----------------------------------------------------
CAnimatedGeometryEntity::CAnimatedGeometryEntity(long id, CResource* resource) :
CEntity(id), mResource(resource)
{
QString filePath = Globals::getProjectPath() + "/" + resource->getProperty("file").toString();
qDebug() << "File path: " << filePath;
QImage sprite = QImage(filePath);
mSprite = new QGraphicsAnimatedSprite(sprite);
// Read resource file for settings
QVariantMap rsrc_anim = mResource->getProperty("anim").toMap();
mSprite->setSheetSize(rsrc_anim["lines"].toInt(), rsrc_anim["columns"].toInt());
mSprite->setElementSize(rsrc_anim["width"].toInt(), rsrc_anim["height"].toInt());
mSprite->setFrameReversion(rsrc_anim["reverse"].toBool());
mSprite->setSpeed(rsrc_anim["speed"].toInt());
_loadAnimations();
// test
playAnimation("idle");
}
//----------------------------------------------------
CAnimatedGeometryEntity::~CAnimatedGeometryEntity()
{
if (mSprite)
delete mSprite;
}
//----------------------------------------------------
void CAnimatedGeometryEntity::addToScene(QGraphicsScene *scene)
{
scene->addItem(mSprite);
mSprite->setPos(mPosition.x, mPosition.y);
}
//----------------------------------------------------
void CAnimatedGeometryEntity::setAngle(float angle)
{
CEntity::setAngle(angle);
mSprite->setRotation(angle);
}
//-----------------------------------------------------
void CAnimatedGeometryEntity::setPosition(const Vector2D &pos)
{
CEntity::setPosition(pos);
mSprite->setPos(pos.x, pos.y);
}
//-----------------------------------------------------
void CAnimatedGeometryEntity::_loadAnimations()
{
// Read the "anims" array from resource file
QVariantMap rsrc_anim = mResource->getProperty("anim").toMap();
QVariantList anims = rsrc_anim["anims"].toList();
for (QVariantList::iterator it = anims.begin(); it != anims.end(); ++it)
{
QVariantMap current_anim = (*it).toMap();
Animation anim_struct;
anim_struct.start_col = current_anim["start_col"].toInt();
anim_struct.start_line = current_anim["start_line"].toInt();
anim_struct.end_col = current_anim["end_col"].toInt();
anim_struct.end_line = current_anim["end_line"].toInt();
anim_struct.name = current_anim["name"].toString();
mAnimations[anim_struct.name] = anim_struct;
}
}
//-----------------------------------------------------
void CAnimatedGeometryEntity::playAnimation(const QString &name)
{
if (!mAnimations.contains(name))
{
qDebug() << "No animation named '" << name << "'";
return;
}
Animation anim = mAnimations[name];
mCurrentAnimation = name;
mSprite->setAnimationFrames(anim.start_col, anim.start_line, anim.end_col, anim.end_line);
}
//-----------------------------------------------------
QVariantMap CAnimatedGeometryEntity::getProperties()
{
QVariantMap map = CEntity::getProperties();
map.insert(PROP_KEY_ANIMATION_NAME, mCurrentAnimation);
map.insert(PROP_KEY_RESOURCE, mResource->getRelativeFilePath());
return map;
}
//-----------------------------------------------------
bool CAnimatedGeometryEntity::setProperty(const QString &key, const QVariant &value)
{
if (key == PROP_KEY_ANIMATION_NAME)
{
QString valueStr = value.toString().trimmed();
playAnimation(valueStr);
return true;
}
if (key == PROP_KEY_RESOURCE)
{
//QString valueStr = value.toString().trimmed();
//QMessageBox::warning(0, "Bwah", "Flemme de faire cette option : read-only");
return true;
}
// Fall back on parent class implementation
return CEntity::setProperty(key,value);
}
//-----------------------------------------------------
void CAnimatedGeometryEntity::setZIndex(unsigned short layer)
{
CEntity::setZIndex(layer);
mSprite->setZValue(layer);
}
//-----------------------------------------------------
void CAnimatedGeometryEntity::copyPropertiesTo(CEntity *ent)
{
CEntity::copyPropertiesTo(ent);
CAnimatedGeometryEntity* ent_cast = static_cast<CAnimatedGeometryEntity*>(ent);
ent_cast->playAnimation(mCurrentAnimation);
}
//-----------------------------------------------------