1 #include "Sprite.h"
2 #include "Defines.h"
3 #include <string>
4
5 #define delay_set 3
6
7 class Sprite;
8
Sprite(char * spritename,AnimationFactory * af)9 Sprite::Sprite(char* spritename, AnimationFactory* af){
10 //printf(">-------Creating sprite\n");
11 this->name = spritename;
12 this->af = af;
13 this->delay = delay_set;
14 this->currentframe = 0;
15 string s(spritename);
16 s = s + "-default";
17 //Animation* def = new Animation(spritename, "default", true, spritename);
18 //animations.push_back(def);
19 //printf("Adding animation to store\n");
20 animations.push_back(af->getByName(s.c_str()));
21 // animations.push_back(af->getLast());
22 //current = def;
23 current = animations[animations.size()-1];
24 alive = true;
25 };
26
~Sprite()27 Sprite::~Sprite(){
28 //int i;
29 //printf("<-------Killing sprite\n");
30 //for(i=0; i<animations.size(); i++){
31 // delete(animations[i]);
32 //}
33 };
34
animationFinished()35 bool Sprite::animationFinished(){
36 // if(!current->loop && current->currentframe < current->numframes){
37 // return false;
38 // } else if(!current->loop && current->currentframe >= current->numframes){
39 // return true;
40 // } else {
41 // return false;
42 // }
43 if(current->numframes == currentframe && !current->loop){
44 return true;
45 } else {
46 return false;
47 }
48 };
49
isAnimation(char * name)50 bool Sprite::isAnimation(char* name){
51 if(strcmp(name,current->name)==0){
52 return true;
53 } else {
54 return false;
55 }
56 };
57
getAnimation()58 char* Sprite::getAnimation(){
59 return current->name;
60 };
61 /*
62 void Sprite::loadAnimation(char* animName, bool loop){
63 Animation* tmp = new Animation(this->name,animName,loop);
64 animations.push_back(tmp);
65 };*/
66
setAnimation(char * name)67 void Sprite::setAnimation(char* name){
68 alive = true;
69 string s(name);
70 string nn(this->name);
71 s = nn + "-" + s;
72 int cur;
73 //printf("Setting animation to %s\n",s.c_str());
74 for(cur=0; cur<af->size(); cur++){
75 //printf("Current animation is called %s\n",animations[cur]->name);
76 Animation* tmp = (Animation*)(af->getByNumber(cur));
77 if(strcmp(tmp->getKnown(),s.c_str()) == 0){
78 current = tmp;
79 currentframe = 0;
80 }
81 }
82 };
83
kill()84 void Sprite::kill(){
85 setAnimation("die");
86 };
87
frame()88 SDL_Surface* Sprite::frame(){
89 //setAnimation("default");
90 SDL_Surface* tmp = current->getFrame(currentframe);
91 delay--;
92 if(delay==0){
93 currentframe++;
94 delay = delay_set;
95 }
96 if(currentframe>=current->numframes && current->loop){
97 currentframe = 0;
98 }
99 return tmp;
100 };
101
move()102 void Sprite::move(){
103 if(xpos < nextxpos){
104 xpos = xpos + speed;
105 } else if(xpos > nextxpos){
106 xpos = xpos - speed;
107 } else if(ypos < nextypos){
108 ypos = ypos + speed;
109 } else if(ypos > nextypos){
110 ypos = ypos - speed;
111 }
112 };
113
gridx(int x)114 void Sprite::gridx(int x){
115 gridxpos = x;
116 };
117
gridy(int y)118 void Sprite::gridy(int y){
119 gridypos = y;
120 };
121
x(int x)122 void Sprite::x(int x){
123 xpos = x;
124 };
125
y(int y)126 void Sprite::y(int y){
127 ypos = y;
128 };
129
x()130 int Sprite::x(){
131 //xpos = (gridxpos * SIZET) + 50;
132 return xpos;
133 };
134
y()135 int Sprite::y(){
136 return ypos;
137 };
138