-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWorld.cpp
352 lines (288 loc) · 11.7 KB
/
GameWorld.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "GameWorld.h"
GameWorld::GameWorld()
{
//pocatecni level
level=1;
//vzdalenosti objektu od sebe, zakladni metrika sceny
metric=35;
//vytvoreni vnorenych objektu ve hre
m_tree=new Tree();
m_player=new Hero();
m_enemy=new Enemy();
m_item=new Item();
m_stone=new RuneStone();
m_wiseElder= new FinalEntity();
m_ground=new Terrain(30*metric,30*metric);
m_wall=new Wall(metric/2);
//vykreslovani okoli hrdiny
levelOfdetail=280;
//mapy
path="levels/map";
actual_map=path+itoa(level,buffer,10)+".lvl";
readLevel(actual_map);
//splen hlavni ukol
mainQuest=false;
}
void GameWorld::setNextLevel()
{
level++;
}
GLuint GameWorld::getLevel()
{
return level;
}
void GameWorld::readLevel(string level){
/*
// CTENI PO ZNAKU (NUTNE PRETYPOVAT JELIKOZ SE NACITA ORD. HODNOTA
char znak;
int value;
ifstream in;
in.open("levels/level1.lvl");
if (in.is_open()) {
for (int i=0; i < 20 ; i ++) {
for (int j=0; j < 20; j++){
in.get(znak);
value=znak;
m_Environment[i][j]=value;
cout << m_Environment[i][j] << " ";
}
}
in.close();
}
else { cerr << " Nepodarilo se otevrit soubor" << endl;}
*/
ifstream in(level.c_str());
string line;
int i, j, hodnota;
if (!in.is_open()){
std::cerr << "Nepodarilo se nacist mapu" << std::endl;
};
i = 0;
count_enemy=0;
count_item=0;
while(getline (in, line)){
j = 0;
istringstream linestream(line);
string polozka;
while (getline (linestream, polozka,' ')){
hodnota = atoi(polozka.c_str());
m_Environment[i][j] = hodnota;
if(hodnota>=5 && hodnota<=8){count_enemy++;};
if(hodnota==3){count_item++;};
if(j<30)j++;
}
if(i<30) i++;
}
in.close();
}
void GameWorld::drawGame()
{
glPushMatrix();
//glTranslated(0,50,0);
// rozmístění nepratel, stromů , hrace atd.
for(int i=0; i<30;i++){
for(int j=0; j<30;j++){
int item=m_Environment[i][j];
if(j==10 and i==10)text("Shadowhorn Forest",GLUT_BITMAP_HELVETICA_12,i*metric,100,j*metric,0,0,0);
switch(item){
//stromy
case 1:
glPushMatrix();
glTranslated(i*metric, 0, j*metric);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_tree->draw();
glPopMatrix();
m_player->onCollision(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric);
break;
//hrac
case 2:
glPushMatrix();
glTranslated(i*metric, 0, j*metric);
rozdil_x=i*metric;
rozdil_z=j*metric;
m_player->draw();
glPopMatrix();
break;
//predmety
case 3:
glPushMatrix();
//collected items
if(m_player->CollisionItemDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))
{
m_Environment[i][j]=0;
m_player->xp(50);
m_player->onCollectedItem();
PlaySound("data/sounds/pickup.wav", NULL, SND_ASYNC);
};
text("<Light Orb>",GLUT_BITMAP_HELVETICA_12,i*metric,25,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_item->draw();
glPopMatrix();
break;
//runestone
case 4:
glPushMatrix();
//collected items
if(m_stone->CollisionHeroDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric) && m_player->questComplete)
{
setNextLevel();
actual_map=path+itoa(level,buffer,10)+".lvl";
m_player->getDefault();
readLevel(actual_map);
m_player->infotext="New realm reached";
m_player->show=true;
PlaySound("data/sounds/wow-level-up-sound.wav", NULL, SND_ASYNC);
};
text("<Rune stone>",GLUT_BITMAP_HELVETICA_12,i*metric,85,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))
{
//natoceni kamenu
m_stone->draw();
glTranslated(0,0,-30);
m_stone->draw();
glTranslated(0,55,-10);
glRotated(90,1,0,0);
m_stone->draw();
}
glPopMatrix();
break;
//nepratele
//Trolls
//maly troll
case 5:
glPushMatrix();
if(m_player->CollisionEnemyDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric) && m_player->active_attack)
{
m_Environment[i][j]=0;
m_player->xp(50);
m_player->onKilledEnemy();
};
text("<Troll Acolyte>",GLUT_BITMAP_HELVETICA_12,i*metric,25,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
glScaled(0.5,0.5,0.5);
glRotated(i*metric+j,0,1,0);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_enemy->draw();
glPopMatrix();
break;
//velky troll
case 6:
glPushMatrix();
if(m_player->CollisionEnemyDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric) && m_player->active_attack)
{
m_Environment[i][j]=0;
m_player->xp(50);
m_player->onKilledEnemy();
};
text("<Giant Troll>",GLUT_BITMAP_HELVETICA_12,i*metric,65,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
glScaled(2,2,2);
glRotated(i*metric+j,0,1,0);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_enemy->draw();
glPopMatrix();
break;
//troll
case 7:
//Material::getGold();
glPushMatrix();
if(m_player->CollisionEnemyDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric) && m_player->active_attack)
{
m_Environment[i][j]=0;
m_player->xp(50);
m_player->onKilledEnemy();
};
text("<Troll>",GLUT_BITMAP_HELVETICA_12,i*metric,35,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
glRotated(i*metric+j,0,1,0);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_enemy->draw();
glPopMatrix();
break;
//BOSS
case 8:
//Material::getGold();
glPushMatrix();
if(m_player->CollisionEnemyDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric) && m_player->active_attack)
{
m_Environment[i][j]=0;
m_player->xp(50);
m_player->onKilledEnemy();
};
text("<Lazarus, Troll Titan>",GLUT_BITMAP_HELVETICA_12,i*metric,125,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
glScaled(4,4,4);
glRotated(i*metric+j,0,1,0);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_enemy->draw();
glPopMatrix();
break;
//wall
case 9:
//Material::getGold();
glPushMatrix();
glTranslated(i*metric, 0, j*metric);
m_player->CollisionWallDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric,metric-5);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_wall->draw();
glPopMatrix();
break;
//final object
case 10:
glPushMatrix();
//uvede konec hry
if(m_stone->CollisionHeroDetection(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))
{
m_player->infotext="Main Quest Complete";
m_player->show=true;
mainQuest=true;
};
//check main quest
if(mainQuest==true)text("THE END",GLUT_BITMAP_HELVETICA_18,m_player->m_x+rozdil_x+20,45,m_player->m_z+rozdil_z,0,0,0);
text("<Thunder, Wise Forest Elder>",GLUT_BITMAP_HELVETICA_12,i*metric,55,j*metric,0,0,0);
glTranslated(i*metric, 0, j*metric);
if(visibilityTest(m_player->m_x+rozdil_x,m_player->m_z+rozdil_z,i*metric,j*metric))m_wiseElder->draw();
glPopMatrix();
break;
}
}
}
glPopMatrix();
//zeme
m_ground->draw();
//text umisteni mimo mapu
onOutOfMap();
}
//generování písma
void GameWorld::text(char * string, void * font, int x, int y, int z, float r, float g, float b)
{
glDisable(GL_TEXTURE_2D);
//Material::getGold();
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(0,0,z);
glRasterPos2i(x,y);
while (*string)glutBitmapCharacter(font, *string++);
glPopMatrix();
glEnable(GL_TEXTURE_2D);
}
//level od detail zobrazeni,zobrazujeme pouze objekty v urcite vzdalenosti od hrdiny (optimalizace vykonu)
bool GameWorld::visibilityTest(GLint x, GLint z, GLint i, GLint j)
{
if(abs(x-i)<levelOfdetail && abs(z-j)<levelOfdetail){return true;}else{return false;}
}
//restrikce a omezení hrací plochy
void GameWorld::onOutOfMap()
{
//pozice jichz nemuze hridna dosahnout, aby nevysel z mapy
if(m_player->m_x < (0-rozdil_x)){m_player->m_x=0-rozdil_x;};
if(m_player->m_x > ((29*metric)-rozdil_x)){m_player->m_x=(29*metric)-rozdil_x;};
if(m_player->m_z < (0-rozdil_z)){m_player->m_z=0-rozdil_z;};
if(m_player->m_z > ((29*metric)-rozdil_z)){m_player->m_z=(29*metric)-rozdil_z;};
}
//destruktor
GameWorld::~GameWorld()
{
delete m_player;
delete m_tree;
delete m_enemy;
delete m_ground;
delete m_item;
delete m_wall;
delete m_stone;
}