-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListImage.cpp
250 lines (223 loc) · 5.8 KB
/
ListImage.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Copyright (C) Kevin J. Estevez (kenystev) and Luis C. Isaula (lisaula)
*
* GNU GENERAL PUBLIC LICENSE Version 2
* The licenses for most software are designed to take away your
* freedom to share and change it. By contrast, the GNU General Public
* License is intended to guarantee your freedom to share and change free
* software--to make sure the software is free for all its users. This
* General Public License applies to most of the Free Software
* Foundation's software and to any other program whose authors commit to
* using it.
*/
#include "ListImage.h"
#include <cmath>
ListImage::ListImage(RosalilaGraphics *paint)
{
root = NULL;
this->painter = paint;
root_scale=0.66;
max_draw = 11;
size=0;
pops=0;
loadSteps("steps.xml");
}
ListImage::ListImage()
{
root = NULL;
this->painter = NULL;
root_scale=0;
}
ListImage::~ListImage()
{
delete root;
}
void ListImage::add(int hurt,string path)
{
Step *newStep = new Step(painter,texturas[path]);
if(strcmp(path.c_str(),"Meta")==0)
newStep->isMeta=true;
if(strcmp(path.c_str(),"puas_left")==0)
newStep->setHit_Left(true);
if(strcmp(path.c_str(),"puas_right")==0)
newStep->setHit_Right(true);
if(strcmp(path.c_str(),"puas_center")==0)
newStep->setHit_Center(true);
newStep->hurt=hurt;
if(strcmp(path.c_str(),"flags")!=0 && strcmp(path.c_str(),"flags_2")!=0)
newStep->setBorders();
if(strcmp(path.c_str(),"puas_1-2")==0)
{
newStep->setHit_Left(true);
newStep->setHit_Center(true);
}
if(strcmp(path.c_str(),"puas_2-3")==0)
{
newStep->setHit_Right(true);
newStep->setHit_Center(true);
}
if(strcmp(path.c_str(),"puas_1-3")==0)
{
newStep->setHit_Right(true);
newStep->setHit_Left(true);
}
if(root==NULL)
{
root = newStep;
double w, h, x, y;
queryData(root, &w, &h, &x, &y);
root->setX(painter->screen_width*0.5 - w*0.5);
root->setY(painter->screen_height - h);
return;
}
Step *temp = root;
while(temp->next!=NULL)
temp = temp->next;
temp->next = newStep;
size++;
}
void ListImage::add(std::string path)
{
Step *newStep = new Step(painter,texturas[path]);
if(strcmp(path.c_str(),"puas_left")==0)
newStep->setHit_Left(true);
if(strcmp(path.c_str(),"puas_right")==0)
newStep->setHit_Right(true);
if(strcmp(path.c_str(),"puas_center")==0)
newStep->setHit_Center(true);
if(root==NULL)
{
root = newStep;
double w, h, x, y;
queryData(root, &w, &h, &x, &y);
root->setX(painter->screen_width*0.5 - w*0.5);
root->setY(painter->screen_height - h);
return;
}
Step *temp = root;
while(temp->next!=NULL)
temp = temp->next;
temp->next = newStep;
}
void ListImage::add(Step *nuevo)
{
if(root==NULL)
{
root = nuevo;
return;
}
Step *temp = root;
while(temp->next!=NULL)
temp = temp->next;
temp->next = nuevo;
}
Step* ListImage::pop()
{
if(pops==0)
pops++;
Step* temp = root;
root = temp->next;
temp->next = NULL;
return temp;
}
void ListImage::draw(Car*car,double off_set_x, double off_set_y)
{
Step *tmp = root;
while(tmp)
{
tmp->addOff_X(off_set_x);
tmp = tmp->next;
}
draw(car,root,root->next,0,off_set_x,off_set_y);
}
void ListImage::draw(double off_set_x, double off_set_y)
{
Step *tmp = root;
while(tmp)
{
tmp->addOff_X(off_set_x);
tmp = tmp->next;
}
draw(NULL,root,root->next,0,off_set_x,off_set_y);
}
void ListImage::draw(Car*car,Step*anterior,Step*actual,int c,double off_set_x, double off_set_y)
{
if(c==max_draw)
{
if(anterior->scale>=0.0563964)//0.108468)
{
add(pop());
}
return;
}
checkColosions(car,anterior);
anterior->addY(off_set_y);
anterior->draw(painter);
actual->init(anterior);
draw(car,actual,actual->next,c+1,off_set_x,off_set_y);
}
void ListImage::checkColosions(Car*car,Step *actual)
{
if(car!=NULL)
{
SDL_Rect r, c=car->wheels;
if(actual->hit_center)
{
r = actual->rect_center;
if(hitboxCollision(r,0,c,0))
{
if(car->v_max-actual->hurt>0)
car->hurt=car->v_max-actual->hurt;
else
car->hurt=car->v_max*0.5;
}
}
if(actual->hit_left)
{
r = actual->rect_left;
if(hitboxCollision(r,0,c,0))
{
if(car->v_max-actual->hurt>0)
car->hurt=car->v_max-actual->hurt;
else
car->hurt=car->v_max*0.5;
}
}
if(actual->hit_right)
{
r = actual->rect_right;
if(hitboxCollision(r,0,c,0))
{
if(car->v_max-actual->hurt>0)
car->hurt=car->v_max-actual->hurt;
else
car->hurt=car->v_max*0.5;
}
}
if(hitboxCollision(actual->border_left,0,c,0) || hitboxCollision(actual->border_right,0,c,0))
car->outOfRoad=true;
}
}
void ListImage::clear()
{
root=NULL;
}
bool ListImage::isMeta()
{
if(root!=NULL)
return root->isMeta;
return false;
}
void ListImage::loadSteps(std::string path)
{
TiXmlDocument doc((assets_directory+path).c_str());
doc.LoadFile();
TiXmlNode *Steps = doc.FirstChild("Steps");
for(TiXmlNode* step=Steps->FirstChild("step");
step!=NULL;
step=step->NextSibling("step"))
{
string s = step->ToElement()->Attribute("path");
texturas[s] = new Step(painter,assets_directory+s+".png");
}
}