File: Sprite.cc

package info (click to toggle)
hannah 1.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,712 kB
  • ctags: 281
  • sloc: cpp: 2,210; sh: 214; makefile: 117
file content (137 lines) | stat: -rw-r--r-- 2,729 bytes parent folder | download | duplicates (5)
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
#include "Sprite.h"
#include "Defines.h"
#include <string>

#define delay_set 3

class Sprite;

Sprite::Sprite(char* spritename, AnimationFactory* af){
	//printf(">-------Creating sprite\n");
	this->name = spritename;
	this->af = af;
	this->delay = delay_set;
	this->currentframe = 0;
	string s(spritename);
	s = s + "-default";
	//Animation* def = new Animation(spritename, "default", true, spritename);
	//animations.push_back(def);
	//printf("Adding animation to store\n");
	animations.push_back(af->getByName(s.c_str()));
//	animations.push_back(af->getLast());
	//current = def;
	current = animations[animations.size()-1];
	alive = true;
};

Sprite::~Sprite(){
	//int i;
	//printf("<-------Killing sprite\n");
	//for(i=0; i<animations.size(); i++){
	//	delete(animations[i]);
	//}
};

bool Sprite::animationFinished(){
//	if(!current->loop && current->currentframe < current->numframes){
//		return false;
//	} else if(!current->loop && current->currentframe >= current->numframes){
//		return true;
//	} else {
//		return false;
//	}
	if(current->numframes == currentframe && !current->loop){
		return true;
	} else {
		return false;
	}
};

bool Sprite::isAnimation(char* name){
	if(strcmp(name,current->name)==0){
		return true;
	} else {
		return false;
	}
};

char* Sprite::getAnimation(){
	return current->name;
};
/*
void Sprite::loadAnimation(char* animName, bool loop){
	Animation* tmp = new Animation(this->name,animName,loop);
	animations.push_back(tmp);
};*/

void Sprite::setAnimation(char* name){
	alive = true;
	string s(name);
	string nn(this->name);
	s = nn + "-" + s;
	int cur;
	//printf("Setting animation to %s\n",s.c_str());
	for(cur=0; cur<af->size(); cur++){
		//printf("Current animation is called %s\n",animations[cur]->name);
		Animation* tmp = (Animation*)(af->getByNumber(cur));
		if(strcmp(tmp->getKnown(),s.c_str()) == 0){
			current = tmp;
			currentframe = 0;
		}
	}
};

void Sprite::kill(){
	setAnimation("die");
};

SDL_Surface* Sprite::frame(){
	//setAnimation("default");
	SDL_Surface* tmp = current->getFrame(currentframe);
	delay--;
	if(delay==0){
		currentframe++;
		delay = delay_set;
	}
	if(currentframe>=current->numframes && current->loop){
		currentframe = 0;
	}
	return tmp;
};

void Sprite::move(){
	if(xpos < nextxpos){
		xpos = xpos + speed;
	} else if(xpos > nextxpos){
		xpos = xpos - speed;
	} else if(ypos < nextypos){
		ypos = ypos + speed;
	} else if(ypos > nextypos){
		ypos = ypos - speed;
	}
};

void Sprite::gridx(int x){
	gridxpos = x;
};

void Sprite::gridy(int y){
	gridypos = y;
};

void Sprite::x(int x){
	xpos = x;
};

void Sprite::y(int y){
	ypos = y;
};

int Sprite::x(){
	//xpos = (gridxpos * SIZET) + 50;
	return xpos;
};

int Sprite::y(){
	return ypos;
};