-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcRoomMan.cpp
219 lines (197 loc) · 4.49 KB
/
cRoomMan.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
#include "cRoomMan.h"
cRoomMan::cRoomMan()
{
items.clear();
score = 0;
score_lab = NULL;
timer = NULL;
maxTime = "";
max_min = 0;
max_sec = 0;
m_title = "";
m_game_end = false;
}
cRoomMan::cRoomMan(const string title, const char *fn)
{
items.clear();
timer = NULL;
maxTime = "";
max_min = 0;
max_sec = 0;
m_title = title;
LoadRoom(fn);
int pos_col[5];
pos_col[0] = 30;
pos_col[1] = WinHeight - 30;
pos_col[2] = 0;
pos_col[3] = 255;
pos_col[4] = 0;
score_lab = new cLabel(FONT, pos_col);
score = 0;
m_game_end = false;
}
cRoomMan::~cRoomMan()
{
if (score_lab != NULL) {
delete score_lab;
score_lab = NULL;
}
if (timer != NULL) {
delete timer;
timer = NULL;
}
vector<cTexture>::iterator iter = items.begin();
while (iter != items.end()) {
iter->cleanTexMemory();
iter++;
}
items.clear();
score = 0;
maxTime = "";
max_min = 0;
max_sec = 0;
m_title = "";
m_game_end = false;
}
void cRoomMan::Draw(const bool named)
{
vector<cTexture>::iterator iter = items.begin();
while (iter != items.end()) {
if (iter->getLive()) {
glPushMatrix();
if (iter->getId() > 1) {
if (iter->getRemoved()) {
if (iter->getXslide() > 5) {
iter->setLive(false);
iter->setRemoved(true);
}
glTranslatef(iter->getXslide(), 0.0, 0.0);
iter->setXslide(iter->getXslide() + iter->getXinc());
}
}
iter->drawQuad(named);
glPopMatrix();
}
iter++;
}
if (!named && score_lab != NULL) {
char tex[10];
_itoa_s(score, tex, size_t(10), 10);
char scor[20] = "Score: ";
strcat_s(scor, tex);
score_lab->OutputLabel(scor);
}
if (!named && timer != NULL)
timer->showTime();
}
int cRoomMan::processTime()
{
if (timer != NULL) {
if (timer->processTime() == 0) return 2;//âðåìÿ âûøëî
}
int i = 0;
vector<cTexture>::iterator iter = items.begin();
while (iter != items.end()) {
if (iter->getRemoved()) i++;
iter++;
}
if (i+1 == items.size() && timer->getWork())
return 1;
return 0;
}
void cRoomMan::LoadRoom(const char *fn)
{
const string textItem = "item";
const string textTime = "time";
string elementName;
int timer_pos_col[5];
timer_pos_col[0] = timer_xpos;
timer_pos_col[1] = timer_ypos;
timer_pos_col[2] = 255;
timer_pos_col[3] = 255;
timer_pos_col[4] = 0;
TiXmlDocument doc(fn);
doc.LoadFile();
TiXmlElement* root = doc.FirstChildElement("room");
TiXmlElement* element = root->FirstChildElement();
cTexture item;
while (element != NULL)
{
elementName = element->Value();
if (elementName == textTime) {//loading clock
TiXmlAttribute* attr = element->FirstAttribute();//first attribute
string val1 = attr->Value();
int min = atoi(val1.c_str());
attr = attr->Next();//second attribute
string val2 = attr->Value();
int sec = atoi(val2.c_str());
timer = new cTimer(min, sec, timer_pos_col);
setMaxTime(val1, val2, min, sec);
} else
if (elementName == textItem) {//loading object
TiXmlAttribute* attr = element->FirstAttribute();//first attribute
string val = attr->Value();
int id = atoi(val.c_str());
attr = attr->Next();//second attribute
string name = attr->Value();
attr = attr->Next();
int xoff = atoi(string(attr->Value()).c_str());
attr = attr->Next();
int yoff = atoi(string(attr->Value()).c_str());
attr = attr->Next();
int width = atoi(string(attr->Value()).c_str());
attr = attr->Next();
int height = atoi(string(attr->Value()).c_str());
attr = attr->Next();
float xy[8];
for (int i = 0; i < 8; i++) {
xy[i] = atof(string(attr->Value()).c_str());
attr = attr->Next();
}
string fileName = attr->Value();
item = cTexture(id, name, xy, xoff, yoff, width, height, fileName.c_str());
items.push_back(item);
}
element = element->NextSiblingElement();
}//while
}
void cRoomMan::setMaxTime(string val1, string val2, const int min, const int sec)
{
if (val1.length() == 1)
val1.insert(0, "0");
if (val2.length() == 1)
val2.insert(0, "0");
maxTime = val1 + ":" + val2;
max_min = min;
max_sec = sec;
}
void cRoomMan::pickItem(const int hit)
{
vector<cTexture>::iterator iter = items.begin();
while (iter != items.end()) {
if (iter->getId() == hit && !iter->getRemoved()) {
iter->setRemoved(true);
addScore(10);
}
iter++;
}
}
void cRoomMan::addScore(const int num)
{
score += num;
}
void cRoomMan::subScore(const int num)
{
score -= num;
}
void cRoomMan::setTimer(const bool work)
{
if (timer != NULL) timer->setWork(work);
}
bool cRoomMan::getTimer()
{
bool b = false;
if (timer != NULL)
b = timer->getWork();
return b;
}