-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.hpp
154 lines (115 loc) · 3.55 KB
/
Animation.hpp
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
#ifndef ANIMATION_H
#define ANIMATION_H
#include "TinyXML/tinyxml.h"
#include <SFML/Graphics.hpp>
using namespace sf;
class Animation
{
public:
std::vector<IntRect> frames, frames_flip;
float currentFrame, speed;
bool loop, flip, isPlaying;
Sprite sprite;
Animation()
{
currentFrame = 0;
isPlaying=true;
flip=false;
loop=true;
}
void tick(float time)
{
if (!isPlaying) return;
currentFrame += speed * time;
if (currentFrame > frames.size()) { currentFrame -= frames.size();
if (!loop) {isPlaying=false; return;}
}
int i = int(currentFrame);
sprite.setTextureRect( frames[i] );
if (flip) sprite.setTextureRect( frames_flip[i] );
}
};
class AnimationManager
{
public:
std::string currentAnim;
std::map<std::string, Animation> animList;
AnimationManager()
{}
~AnimationManager()
{ animList.clear();
}
//loop =false if its dont conect to player
void create(std::string name, Texture &texture, int x, int y, int w, int h, int count, float speed, int step=0, bool Loop=true)
{
Animation a;
a.speed = speed;
a.loop = Loop;
a.sprite.setTexture(texture);
a.sprite.setOrigin(float(0), float(h));
///////////////////////////////////////////
////create animation like walk and lump////
/// image ////
for (int i=0;i<count;i++)
{
a.frames.push_back( IntRect(x+i*step, y, w, h) );
a.frames_flip.push_back( IntRect(x+i*step+w, y, -w, h) );
}
animList[name] = a;
currentAnim = name;
}
//
bool loadFromXML(std::string fileName, const Texture & t, sf::Vector2f a)
{
TiXmlDocument animFile(fileName.c_str());
if(!animFile.LoadFile())return false;
TiXmlElement *head;
head = animFile.FirstChildElement("sprites");
TiXmlElement *animElement;
animElement = head->FirstChildElement("animation");
while(animElement)
{
Animation anim;
anim.sprite.scale(a);
currentAnim = animElement->Attribute("title");
int delay = atoi(animElement->Attribute("delay"));
anim.speed = float(1.0/delay);
anim.sprite.setTexture(t);
TiXmlElement *cut;
cut = animElement->FirstChildElement("cut");
while (cut)
{
int x = atoi(cut->Attribute("x"));
int y = atoi(cut->Attribute("y"));
int w = atoi(cut->Attribute("w"));
int h = atoi(cut->Attribute("h"));
anim.frames.push_back( IntRect(x,y,w,h) );
anim.frames_flip.push_back( IntRect(x+w,y,-w,h) );
cut = cut->NextSiblingElement("cut");
}
anim.sprite.setOrigin(float(0), float(anim.frames[0].height));
animList[currentAnim] = anim;
animElement = animElement->NextSiblingElement("animation");
}
return true;
}
void set(std::string name)
{
currentAnim = name;
animList[currentAnim].flip=0;
}
void draw(RenderWindow &window, sf::Vector2f pos)
{
animList[currentAnim].sprite.setPosition(pos);
window.draw( animList[currentAnim].sprite );
}
void flip(bool b=1) {animList[currentAnim].flip = b;}
void tick(float time) {animList[currentAnim].tick(time);}
void pause() {animList[currentAnim].isPlaying=false;}
void play() {animList[currentAnim].isPlaying=true;}
void play(std::string name) {animList[name].isPlaying=true;}
bool isPlaying() {return animList[currentAnim].isPlaying;}
float getH() {return float (animList[currentAnim].frames[0].height);}
float getW() {return float( animList[currentAnim].frames[0].width);}
};
#endif ANIMATION_H