-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pista.cpp
169 lines (155 loc) · 4.54 KB
/
Pista.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
/**
* 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 "Pista.h"
#include "ListImage.h"
Pista::Pista(Car*c,RosalilaGraphics*rg, Receiver*r,std::string path) : Pista(c, rg, r)
{
loadTrack(path);
}
Pista::Pista(Car *c, RosalilaGraphics *paint, Receiver* receiver)
{
road =new ListImage(paint);
flags =new ListImage(paint);
this->receiver = receiver;
off_set_y=0;
off_set_x=0;
background = paint->getTexture(assets_directory+"background.png");
this->painter = paint;
car = c;
laps=1;
time=0;
start=NULL;
stop=NULL;
seg=0;
cont=-1;
go=false;
display_time=new Font("airstrikeacad.ttf");
display_time->setSize(20);
display_time->setColor(0,0,0);
contD.push_back(paint->getTexture(assets_directory+"Contador1.png"));
contD.push_back(paint->getTexture(assets_directory+"Contador2.png"));
contD.push_back(paint->getTexture(assets_directory+"Contador3.png"));
contD.push_back(paint->getTexture(assets_directory+"Contador4.png"));
}
Pista::Pista(){
}
void Pista::countdown(){
if(cont>=0 && cont<=3){
painter->draw2DImage(contD[cont],
contD[cont]->getWidth(),contD[cont]->getHeight(),
215,67,
1,
0,false,
0,0,
Color(255,255,255,255),
0,0,
false);
}
if(cont<=3){
if(painter->frame%50==0)
cont++;
}
}
bool Pista::getLose(){
return car->getLose();
}
void Pista::init(){
cont=-1;
go=false;
for(int i=0; i<90; i++)
flags->add("flags_2");
}
void Pista::logica(){
if(go){
start = clock();
off_set_y=car->v;
if(car->turn)
off_set_x=car->TURN;
else
off_set_x=0;
car->update(receiver);
stop = clock();
time +=(stop - start);
seg = ((time)/300);
if(road->isMeta() && road->pops!=0)
{
laps--;
road->add(road->pop());
}
if(laps==0){
if(off_set_y>0)
off_set_y-=1;
else{
off_set_y=0;
}
}
if(getLose()){
off_set_y=0;
off_set_x=0;
}
}
if(cont==4){
if(painter->frame%50==0){
go=true;
}
}
}
void Pista:: draw(){
painter->draw2DImage(background,background->getWidth(),background->getHeight(),0,0,1,0,false,0,0,Color(255,255,255,255),0,0,false);
road->draw(car,off_set_x,off_set_y);
flags->draw(off_set_x,off_set_y);
display_time->drawText("Time: "+ toString(seg)+" seg.",115,120);
car->draw();
if(!go){
countdown();
}
}
Pista::~Pista()
{
delete road;
delete flags;
delete receiver;
delete background;
delete display_time;
}
void Pista::clear()
{
road->clear();
flags->clear();
}
void Pista::loadTrack(std::string path)
{
TiXmlDocument doc((assets_directory+path).c_str());
doc.LoadFile();
TiXmlNode *track = doc.FirstChild("Track");
laps = atoi(track->ToElement()->Attribute("laps"));
for(TiXmlNode* repit=track->FirstChild("repit");
repit!=NULL;
repit=repit->NextSibling("repit"))
{
int rep = atoi(repit->ToElement()->Attribute("for"));
for(int i=0;i<rep;i++)
{
for(TiXmlNode* step=repit->FirstChild("step");
step!=NULL;
step=step->NextSibling("step"))
{
string s = step->ToElement()->Attribute("path");
int d = atoi(step->ToElement()->Attribute("long"));
int hurt = atoi(step->ToElement()->Attribute("hurt"));
for(int i=0;i<d;i++)
road->add(hurt,s);
}
}
}
}