1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Christian Meyer |
---|
15 | */ |
---|
16 | |
---|
17 | #include "world.h" |
---|
18 | #include "world_entity.h" |
---|
19 | #include "collision.h" |
---|
20 | #include "track_manager.h" |
---|
21 | #include "track.h" |
---|
22 | #include "player.h" |
---|
23 | #include "command_node.h" |
---|
24 | #include "camera.h" |
---|
25 | #include "environment.h" |
---|
26 | #include "p_node.h" |
---|
27 | #include "null_parent.h" |
---|
28 | #include "helper_parent.h" |
---|
29 | #include "glmenu_imagescreen.h" |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | \brief create a new World |
---|
36 | |
---|
37 | This creates a new empty world! |
---|
38 | */ |
---|
39 | World::World (char* name) |
---|
40 | { |
---|
41 | this->setClassName ("World"); |
---|
42 | this->worldName = name; |
---|
43 | this->debugWorldNr = -1; |
---|
44 | this->entities = new tList<WorldEntity>(); |
---|
45 | } |
---|
46 | |
---|
47 | World::World (int worldID) |
---|
48 | { |
---|
49 | this->debugWorldNr = worldID; |
---|
50 | this->worldName = NULL; |
---|
51 | this->entities = new tList<WorldEntity>(); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | \brief remove the World from memory |
---|
56 | |
---|
57 | delete everything explicitly, that isn't contained in the parenting tree! |
---|
58 | things contained in the tree are deleted automaticaly |
---|
59 | */ |
---|
60 | World::~World () |
---|
61 | { |
---|
62 | printf("World::~World() - deleting current world\n"); |
---|
63 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
64 | cn->unbind(this->localPlayer); |
---|
65 | cn->reset(); |
---|
66 | this->localCamera->destroy(); |
---|
67 | |
---|
68 | this->nullParent->destroy (); |
---|
69 | |
---|
70 | //delete this->trackManager; |
---|
71 | |
---|
72 | /* |
---|
73 | WorldEntity* entity = entities->enumerate(); |
---|
74 | while( entity != NULL ) |
---|
75 | { |
---|
76 | entity->destroy(); |
---|
77 | entity = entities->nextElement(); |
---|
78 | } |
---|
79 | this->entities->destroy(); |
---|
80 | */ |
---|
81 | |
---|
82 | /* FIX the parent list has to be cleared - not possible if we got the old list also*/ |
---|
83 | |
---|
84 | |
---|
85 | //delete this->entities; |
---|
86 | //delete this->localCamera; |
---|
87 | /* this->localPlayer hasn't to be deleted explicitly, it is |
---|
88 | contained in entities*/ |
---|
89 | } |
---|
90 | |
---|
91 | GLfloat ctrlpoints[4][3] = { |
---|
92 | {20.0, 10.0, 5.0}, {40.0, -10.0, 0.0}, |
---|
93 | {60.0, -10.0, 5.0}, {80.0, 10.0, 5.0}}; |
---|
94 | |
---|
95 | |
---|
96 | ErrorMessage World::init() |
---|
97 | { |
---|
98 | this->bPause = false; |
---|
99 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
100 | cn->addToWorld(this); |
---|
101 | cn->enable(true); |
---|
102 | |
---|
103 | //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); |
---|
104 | //glEnable (GL_MAP1_VERTEX_3); |
---|
105 | |
---|
106 | //theNurb = gluNewNurbsRenderer (); |
---|
107 | //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); |
---|
108 | //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback ); |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | ErrorMessage World::start() |
---|
115 | { |
---|
116 | printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
117 | this->bQuitOrxonox = false; |
---|
118 | this->bQuitCurrentGame = false; |
---|
119 | this->mainLoop(); |
---|
120 | } |
---|
121 | |
---|
122 | ErrorMessage World::stop() |
---|
123 | { |
---|
124 | printf("World::stop() - got stop signal\n"); |
---|
125 | this->bQuitCurrentGame = true; |
---|
126 | } |
---|
127 | |
---|
128 | ErrorMessage World::pause() |
---|
129 | { |
---|
130 | this->isPaused = true; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | ErrorMessage World::resume() |
---|
135 | { |
---|
136 | this->isPaused = false; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void World::destroy() |
---|
141 | { |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | void World::displayLoadScreen () |
---|
146 | { |
---|
147 | printf ("World::displayLoadScreen - start\n"); |
---|
148 | |
---|
149 | //GLMenuImageScreen* |
---|
150 | this->glmis = new GLMenuImageScreen(); |
---|
151 | this->glmis->init(); |
---|
152 | this->glmis->setMaximum(10); |
---|
153 | this->glmis->draw(); |
---|
154 | |
---|
155 | printf ("World::displayLoadScreen - end\n"); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | void World::releaseLoadScreen () |
---|
160 | { |
---|
161 | printf ("World::releaseLoadScreen - start\n"); |
---|
162 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
163 | SDL_Delay(500); |
---|
164 | printf ("World::releaseLoadScreen - end\n"); |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void World::load() |
---|
169 | { |
---|
170 | // BezierCurve* tmpCurve = new BezierCurve(); |
---|
171 | if(this->debugWorldNr != -1) |
---|
172 | { |
---|
173 | trackManager = TrackManager::getInstance(); |
---|
174 | trackManager->addPoint(Vector(0,-5,0)); |
---|
175 | trackManager->addPoint(Vector(10,0,5)); |
---|
176 | trackManager->addPoint(Vector(20,0,-5)); |
---|
177 | trackManager->addPoint(Vector(30,0,5)); |
---|
178 | trackManager->addPoint(Vector(40,0,5)); |
---|
179 | trackManager->setDuration(.5); |
---|
180 | trackManager->setSavePoint(); |
---|
181 | trackManager->addPoint(Vector(50,10,10)); |
---|
182 | trackManager->addPoint(Vector(60,0, 10)); |
---|
183 | trackManager->addPoint(Vector(70,0, 10)); |
---|
184 | trackManager->addPoint(Vector(80,0,-10)); |
---|
185 | trackManager->addPoint(Vector(90,0,-10)); |
---|
186 | trackManager->setDuration(.5); |
---|
187 | trackManager->setSavePoint(); |
---|
188 | trackManager->addPoint(Vector(110,0,5)); |
---|
189 | trackManager->addPoint(Vector(120,0, 10)); |
---|
190 | trackManager->addPoint(Vector(130,0, 10)); |
---|
191 | trackManager->addPoint(Vector(140,0,-10)); |
---|
192 | trackManager->addPoint(Vector(150,0,-10)); |
---|
193 | trackManager->setDuration(.5); |
---|
194 | int fork11, fork12, fork13, fork14; |
---|
195 | trackManager->fork(4, &fork11, &fork12, &fork13, &fork14); |
---|
196 | trackManager->workOn(fork11); |
---|
197 | trackManager->addPoint(Vector(170, 0, -15)); |
---|
198 | trackManager->addPoint(Vector(180, 0, -15)); |
---|
199 | trackManager->workOn(fork12); |
---|
200 | trackManager->addPoint(Vector(170, 0, 10)); |
---|
201 | trackManager->addPoint(Vector(180, 0, 10)); |
---|
202 | trackManager->addPoint(Vector(190,2,5)); |
---|
203 | trackManager->addPoint(Vector(200,2,5)); |
---|
204 | int fork21, fork22; |
---|
205 | trackManager->fork(2, &fork21, &fork22); |
---|
206 | trackManager->workOn(fork21); |
---|
207 | trackManager->addPoint(Vector(220, 10,-10)); |
---|
208 | trackManager->addPoint(Vector(230, 0,-10)); |
---|
209 | trackManager->addPoint(Vector(240, 0, 2)); |
---|
210 | trackManager->addPoint(Vector(250, 0, 0)); |
---|
211 | trackManager->addPoint(Vector(260, 0, 5)); |
---|
212 | trackManager->join(2, fork12, fork11); |
---|
213 | trackManager->workOn(fork22); |
---|
214 | trackManager->addPoint(Vector(220, -10,10)); |
---|
215 | trackManager->addPoint(Vector(230, 0, 10)); |
---|
216 | trackManager->addPoint(Vector(240, 0, 10)); |
---|
217 | trackManager->addPoint(Vector(250, 0, 5)); |
---|
218 | trackManager->workOn(fork13); |
---|
219 | trackManager->addPoint(Vector(200,-10,5)); |
---|
220 | trackManager->addPoint(Vector(250,-10,5)); |
---|
221 | printf("fork14: %d\n", fork14); |
---|
222 | trackManager->workOn(fork14); |
---|
223 | trackManager->addPoint(Vector(200,15,0)); |
---|
224 | trackManager->addPoint(Vector(210,0,10)); |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | trackManager->join(4, fork21, fork22, fork13, fork14); |
---|
229 | |
---|
230 | /*monitor progress*/ |
---|
231 | this->glmis->step(); |
---|
232 | |
---|
233 | /* |
---|
234 | tmpCurve->addNode(Vector(10,0,-10)); |
---|
235 | //tmpCurve->addNode(Vector(10,2,5)); |
---|
236 | //tmpCurve->addNode(Vector(10,3,-5)); |
---|
237 | // tmpCurve->addNode(Vector(10,1,5)); |
---|
238 | tmpCurve->addNode(Vector(10,0,5)); |
---|
239 | */ |
---|
240 | switch(this->debugWorldNr) |
---|
241 | { |
---|
242 | /* |
---|
243 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
244 | removed by a reald world-loader, which interprets a world-file. |
---|
245 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
246 | make whatever you want... |
---|
247 | */ |
---|
248 | case DEBUG_WORLD_0: |
---|
249 | { |
---|
250 | this->nullParent = NullParent::getInstance (); |
---|
251 | this->nullParent->setName ("NullParent"); |
---|
252 | |
---|
253 | // create some path nodes |
---|
254 | this->pathnodes = new Vector[6]; |
---|
255 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
256 | this->pathnodes[1] = Vector(1000, 0, 0); |
---|
257 | // this->pathnodes[2] = Vector(-100, 140, 0); |
---|
258 | // this->pathnodes[3] = Vector(0, 180, 0); |
---|
259 | // this->pathnodes[4] = Vector(100, 140, 0); |
---|
260 | // this->pathnodes[5] = Vector(100, 40, 0); |
---|
261 | |
---|
262 | // create the tracks |
---|
263 | this->tracklen = 2; |
---|
264 | this->track = new Track[2]; |
---|
265 | for( int i = 0; i < this->tracklen; i++) |
---|
266 | { |
---|
267 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
268 | } |
---|
269 | // !\todo old track-system has to be removed |
---|
270 | |
---|
271 | //create helper for player |
---|
272 | HelperParent* hp = new HelperParent (); |
---|
273 | /* the player has to be added to this helper */ |
---|
274 | |
---|
275 | // create a player |
---|
276 | WorldEntity* myPlayer = new Player (); |
---|
277 | myPlayer->setName ("player"); |
---|
278 | this->spawn (myPlayer); |
---|
279 | this->localPlayer = myPlayer; |
---|
280 | |
---|
281 | /*monitor progress*/ |
---|
282 | this->glmis->step(); |
---|
283 | |
---|
284 | // bind input |
---|
285 | Orxonox *orx = Orxonox::getInstance (); |
---|
286 | orx->getLocalInput()->bind (myPlayer); |
---|
287 | |
---|
288 | // bind camera |
---|
289 | this->localCamera = new Camera(this); |
---|
290 | this->localCamera->setName ("camera"); |
---|
291 | this->getCamera()->bind (myPlayer); |
---|
292 | this->localPlayer->addChild (this->localCamera); |
---|
293 | |
---|
294 | /*monitor progress*/ |
---|
295 | this->glmis->step(); |
---|
296 | |
---|
297 | Vector* es = new Vector (50, 2, 0); |
---|
298 | Quaternion* qs = new Quaternion (); |
---|
299 | WorldEntity* env = new Environment(); |
---|
300 | env->setName ("env"); |
---|
301 | this->spawn(env, es, qs); |
---|
302 | |
---|
303 | /*monitor progress*/ |
---|
304 | this->glmis->step(); |
---|
305 | |
---|
306 | break; |
---|
307 | } |
---|
308 | case DEBUG_WORLD_1: |
---|
309 | { |
---|
310 | /* |
---|
311 | this->testCurve = new UPointCurve(); |
---|
312 | this->testCurve->addNode(Vector( 0, 0, 0)); |
---|
313 | this->testCurve->addNode(Vector(10, 0, 5)); |
---|
314 | this->testCurve->addNode(Vector(20, -5,-5)); |
---|
315 | this->testCurve->addNode(Vector(30, 5, 10)); |
---|
316 | this->testCurve->addNode(Vector(40, 0,-10)); |
---|
317 | this->testCurve->addNode(Vector(50, 0,-10)); |
---|
318 | */ |
---|
319 | |
---|
320 | this->nullParent = NullParent::getInstance (); |
---|
321 | this->nullParent->setName ("NullParent"); |
---|
322 | |
---|
323 | // create some path nodes |
---|
324 | this->pathnodes = new Vector[6]; |
---|
325 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
326 | this->pathnodes[1] = Vector(20, 10, 10); |
---|
327 | this->pathnodes[2] = Vector(40, 0, 10); |
---|
328 | this->pathnodes[3] = Vector(60, 10, 0); |
---|
329 | this->pathnodes[4] = Vector(80, 20, 10); |
---|
330 | this->pathnodes[5] = Vector(30, 50, 0); |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | |
---|
335 | // create the tracks |
---|
336 | this->tracklen = 6; |
---|
337 | this->track = new Track[6]; |
---|
338 | for( int i = 0; i < this->tracklen; i++) |
---|
339 | { |
---|
340 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
341 | } |
---|
342 | |
---|
343 | // create a player |
---|
344 | WorldEntity* myPlayer = new Player(); |
---|
345 | myPlayer->setName ("player"); |
---|
346 | this->spawn(myPlayer); |
---|
347 | this->localPlayer = myPlayer; |
---|
348 | |
---|
349 | // bind input |
---|
350 | Orxonox *orx = Orxonox::getInstance(); |
---|
351 | orx->getLocalInput()->bind (myPlayer); |
---|
352 | |
---|
353 | // bind camera |
---|
354 | this->localCamera = new Camera (this); |
---|
355 | this->localCamera->setName ("camera"); |
---|
356 | this->getCamera()->bind (myPlayer); |
---|
357 | this->localPlayer->addChild (this->localCamera); |
---|
358 | break; |
---|
359 | } |
---|
360 | default: |
---|
361 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
362 | } |
---|
363 | } |
---|
364 | else if(this->worldName != NULL) |
---|
365 | { |
---|
366 | |
---|
367 | } |
---|
368 | |
---|
369 | // initialize debug coord system |
---|
370 | objectList = glGenLists(1); |
---|
371 | glNewList (objectList, GL_COMPILE); |
---|
372 | glLoadIdentity(); |
---|
373 | glColor3f(1.0,0,0); |
---|
374 | glBegin(GL_QUADS); |
---|
375 | |
---|
376 | int sizeX = 100; |
---|
377 | int sizeZ = 80; |
---|
378 | float length = 1000; |
---|
379 | float width = 200; |
---|
380 | float widthX = float (length /sizeX); |
---|
381 | float widthZ = float (width /sizeZ); |
---|
382 | |
---|
383 | float height [sizeX][sizeZ]; |
---|
384 | Vector normal_vectors[sizeX][sizeZ]; |
---|
385 | |
---|
386 | |
---|
387 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
388 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
389 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
390 | #ifdef __WIN32__ |
---|
391 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
392 | #else |
---|
393 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
394 | #endif |
---|
395 | |
---|
396 | //Die Huegel ein wenig glaetten |
---|
397 | for (int h=1; h<2;h++) |
---|
398 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
399 | for(int j=1;j<sizeZ-2;j+=1) |
---|
400 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
401 | |
---|
402 | //Berechnung von normalen Vektoren |
---|
403 | for(int i=1;i<sizeX-2;i+=1) |
---|
404 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
405 | { |
---|
406 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
407 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
408 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
409 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
410 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
411 | |
---|
412 | Vector c1 = v2 - v1; |
---|
413 | Vector c2 = v3 - v1; |
---|
414 | Vector c3= v4 - v1; |
---|
415 | Vector c4 = v5 - v1; |
---|
416 | Vector zero = Vector (0,0,0); |
---|
417 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
418 | normal_vectors[i][j].normalize(); |
---|
419 | } |
---|
420 | |
---|
421 | int snowheight=3; |
---|
422 | for ( int i = 0; i<sizeX; i+=1) |
---|
423 | for (int j = 0; j<sizeZ;j+=1) |
---|
424 | { |
---|
425 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
426 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
427 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
428 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
429 | float a[3]; |
---|
430 | if(height[i][j]<snowheight){ |
---|
431 | a[0]=0; |
---|
432 | a[1]=1.0-height[i][j]/10-.3; |
---|
433 | a[2]=0; |
---|
434 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
435 | } |
---|
436 | else{ |
---|
437 | a[0]=1.0; |
---|
438 | a[1]=1.0; |
---|
439 | a[2]=1.0; |
---|
440 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
441 | |
---|
442 | } |
---|
443 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
444 | glVertex3f(v1.x, v1.y, v1.z); |
---|
445 | if(height[i+1][j]<snowheight){ |
---|
446 | a[0]=0; |
---|
447 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
448 | a[2]=0; |
---|
449 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
450 | } |
---|
451 | else{ |
---|
452 | a[0]=1.0; |
---|
453 | a[1]=1.0; |
---|
454 | a[2]=1.0; |
---|
455 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
456 | |
---|
457 | } |
---|
458 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
459 | glVertex3f(v2.x, v2.y, v2.z); |
---|
460 | if(height[i+1][j+1]<snowheight){ |
---|
461 | a[0]=0; |
---|
462 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
463 | a[2]=0; |
---|
464 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
465 | } |
---|
466 | else{ |
---|
467 | a[0]=1.0; |
---|
468 | a[1]=1.0; |
---|
469 | a[2]=1.0; |
---|
470 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
471 | |
---|
472 | |
---|
473 | } |
---|
474 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
475 | glVertex3f(v3.x, v3.y, v3.z); |
---|
476 | if(height[i][j+1]<snowheight){ |
---|
477 | a[0]=0; |
---|
478 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
479 | a[2]=0; |
---|
480 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
481 | } |
---|
482 | else{ |
---|
483 | a[0]=1.0; |
---|
484 | a[1]=1.0; |
---|
485 | a[2]=1.0; |
---|
486 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
487 | } |
---|
488 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
489 | glVertex3f(v4.x, v4.y, v4.z); |
---|
490 | |
---|
491 | } |
---|
492 | glEnd(); |
---|
493 | /* |
---|
494 | glBegin(GL_LINES); |
---|
495 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
496 | { |
---|
497 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
498 | { |
---|
499 | glColor3f(1,0,0); |
---|
500 | glVertex3f(x,y,-128.0); |
---|
501 | glVertex3f(x,y,0.0); |
---|
502 | glColor3f(0.5,0,0); |
---|
503 | glVertex3f(x,y,0.0); |
---|
504 | glVertex3f(x,y,128.0); |
---|
505 | } |
---|
506 | } |
---|
507 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
508 | { |
---|
509 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
510 | { |
---|
511 | glColor3f(0,1,0); |
---|
512 | glVertex3f(-128.0,y,z); |
---|
513 | glVertex3f(0.0,y,z); |
---|
514 | glColor3f(0,0.5,0); |
---|
515 | glVertex3f(0.0,y,z); |
---|
516 | glVertex3f(128.0,y,z); |
---|
517 | } |
---|
518 | } |
---|
519 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
520 | { |
---|
521 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
522 | { |
---|
523 | glColor3f(0,0,1); |
---|
524 | glVertex3f(x,-128.0,z); |
---|
525 | glVertex3f(x,0.0,z); |
---|
526 | glColor3f(0,0,0.5); |
---|
527 | glVertex3f(x,0.0,z); |
---|
528 | glVertex3f(x,128.0,z); |
---|
529 | } |
---|
530 | |
---|
531 | } |
---|
532 | */ |
---|
533 | //draw track |
---|
534 | glBegin(GL_LINES); |
---|
535 | glColor3f(0.0, 1.0, 1.0); |
---|
536 | for( int i = 0; i < tracklen; i++) |
---|
537 | { |
---|
538 | glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z); |
---|
539 | glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z); |
---|
540 | } |
---|
541 | glEnd(); |
---|
542 | |
---|
543 | /* |
---|
544 | glBegin(GL_LINE_STRIP); |
---|
545 | glColor3f(1.0, 5.0, 1.0); |
---|
546 | for( int i = 0; i <= 30; i++) |
---|
547 | { |
---|
548 | glEvalCoord1f ((GLfloat) i/30.0); |
---|
549 | } |
---|
550 | glEnd(); |
---|
551 | */ |
---|
552 | |
---|
553 | trackManager->drawGraph(.01); |
---|
554 | trackManager->debug(2); |
---|
555 | delete trackManager; |
---|
556 | |
---|
557 | /* |
---|
558 | glBegin(GL_LINES); |
---|
559 | float i; |
---|
560 | for(i = 0.0; i<1; i+=.01) |
---|
561 | { |
---|
562 | printf("%f, %f, %f\n",tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); |
---|
563 | glVertex3f(tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); |
---|
564 | } |
---|
565 | glEnd(); |
---|
566 | */ |
---|
567 | glEndList(); |
---|
568 | } |
---|
569 | |
---|
570 | |
---|
571 | /** |
---|
572 | \brief checks for collisions |
---|
573 | |
---|
574 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
575 | between them. In case of collisions the collide() method of the corresponding entities |
---|
576 | is called. |
---|
577 | */ |
---|
578 | void World::collide () |
---|
579 | { |
---|
580 | /* |
---|
581 | List *a, *b; |
---|
582 | WorldEntity *aobj, *bobj; |
---|
583 | |
---|
584 | a = entities; |
---|
585 | |
---|
586 | while( a != NULL) |
---|
587 | { |
---|
588 | aobj = a->nextElement(); |
---|
589 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
590 | { |
---|
591 | b = a->nextElement(); |
---|
592 | while( b != NULL ) |
---|
593 | { |
---|
594 | bobj = b->nextElement(); |
---|
595 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
596 | { |
---|
597 | unsigned long ahitflg, bhitflg; |
---|
598 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
599 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
600 | &bhitflg) ); |
---|
601 | { |
---|
602 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
603 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
604 | } |
---|
605 | } |
---|
606 | b = b->nextElement(); |
---|
607 | } |
---|
608 | } |
---|
609 | a = a->enumerate(); |
---|
610 | } |
---|
611 | */ |
---|
612 | } |
---|
613 | |
---|
614 | /** |
---|
615 | \brief runs through all entities calling their draw() methods |
---|
616 | */ |
---|
617 | void World::draw () |
---|
618 | { |
---|
619 | // draw entities |
---|
620 | WorldEntity* entity; |
---|
621 | entity = this->entities->enumerate(); |
---|
622 | while( entity != NULL ) |
---|
623 | { |
---|
624 | if( entity->bDraw ) entity->draw(); |
---|
625 | entity = this->entities->nextElement(); |
---|
626 | } |
---|
627 | |
---|
628 | //glmis = new GLMenuImageScreen(); |
---|
629 | ///glmis->init(); |
---|
630 | |
---|
631 | // draw debug coord system |
---|
632 | glCallList (objectList); |
---|
633 | |
---|
634 | } |
---|
635 | |
---|
636 | /** |
---|
637 | \brief updates Placements and notifies entities when they left the |
---|
638 | world |
---|
639 | |
---|
640 | This runs trough all WorldEntities and maps Locations to Placements |
---|
641 | if they are bound, checks whether they left the level boundaries |
---|
642 | and calls appropriate functions. |
---|
643 | */ |
---|
644 | void World::update () |
---|
645 | { |
---|
646 | /* |
---|
647 | //List<WorldEntity> *l; |
---|
648 | WorldEntity* entity; |
---|
649 | Location* loc; |
---|
650 | Placement* plc; |
---|
651 | Uint32 t; |
---|
652 | |
---|
653 | // l = entities->enumerate(); |
---|
654 | entity = this->entities->enumerate(); |
---|
655 | while( entity != NULL ) |
---|
656 | { |
---|
657 | |
---|
658 | |
---|
659 | if( !entity->isFree() ) |
---|
660 | { |
---|
661 | loc = entity->getLocation(); |
---|
662 | plc = entity->getPlacement(); |
---|
663 | t = loc->part; |
---|
664 | |
---|
665 | if( t >= tracklen ) |
---|
666 | { |
---|
667 | printf("An entity is out of the game area\n"); |
---|
668 | entity->leftWorld (); |
---|
669 | } |
---|
670 | else |
---|
671 | { |
---|
672 | while( track[t].mapCoords( loc, plc) ) |
---|
673 | { |
---|
674 | track[t].postLeave (entity); |
---|
675 | if( loc->part >= tracklen ) |
---|
676 | { |
---|
677 | printf("An entity has left the game area\n"); |
---|
678 | entity->leftWorld (); |
---|
679 | break; |
---|
680 | } |
---|
681 | track[loc->part].postEnter (entity); |
---|
682 | } |
---|
683 | } |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | } |
---|
688 | |
---|
689 | entity = entities->nextElement(); |
---|
690 | } |
---|
691 | */ |
---|
692 | } |
---|
693 | |
---|
694 | /** |
---|
695 | \brief relays the passed time since the last frame to entities and Track parts |
---|
696 | \param deltaT: the time passed since the last frame in milliseconds |
---|
697 | */ |
---|
698 | void World::timeSlice (Uint32 deltaT) |
---|
699 | { |
---|
700 | //List<WorldEntity> *l; |
---|
701 | WorldEntity* entity; |
---|
702 | float seconds = deltaT / 1000.0; |
---|
703 | |
---|
704 | this->nullParent->update (seconds); |
---|
705 | //this->nullParent->processTick (seconds); |
---|
706 | |
---|
707 | entity = entities->enumerate(); |
---|
708 | while( entity != NULL) |
---|
709 | { |
---|
710 | entity->tick (seconds); |
---|
711 | entity = entities->nextElement(); |
---|
712 | } |
---|
713 | |
---|
714 | //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
715 | } |
---|
716 | |
---|
717 | /** |
---|
718 | \brief removes level data from memory |
---|
719 | */ |
---|
720 | void World::unload() |
---|
721 | { |
---|
722 | if( pathnodes) delete []pathnodes; |
---|
723 | if( track) delete []pathnodes; |
---|
724 | } |
---|
725 | |
---|
726 | |
---|
727 | void World::setTrackLen(Uint32 len) |
---|
728 | { |
---|
729 | this->tracklen = len; |
---|
730 | } |
---|
731 | |
---|
732 | int World::getTrackLen() |
---|
733 | { |
---|
734 | return this->tracklen; |
---|
735 | } |
---|
736 | |
---|
737 | |
---|
738 | |
---|
739 | /** |
---|
740 | \brief function to put your own debug stuff into it. it can display informations about |
---|
741 | the current class/procedure |
---|
742 | */ |
---|
743 | void World::debug() |
---|
744 | { |
---|
745 | printf ("World::debug() - starting debug\n"); |
---|
746 | PNode* p1 = NullParent::getInstance (); |
---|
747 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
748 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
749 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
750 | |
---|
751 | p1->debug (); |
---|
752 | p2->debug (); |
---|
753 | p3->debug (); |
---|
754 | p4->debug (); |
---|
755 | |
---|
756 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
757 | |
---|
758 | printf("World::debug() - shift\n"); |
---|
759 | p1->debug (); |
---|
760 | p2->debug (); |
---|
761 | p3->debug (); |
---|
762 | p4->debug (); |
---|
763 | |
---|
764 | p1->update (1); |
---|
765 | |
---|
766 | printf ("World::debug() - update\n"); |
---|
767 | p1->debug (); |
---|
768 | p2->debug (); |
---|
769 | p3->debug (); |
---|
770 | p4->debug (); |
---|
771 | |
---|
772 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
773 | p1->update (2); |
---|
774 | |
---|
775 | p1->debug (); |
---|
776 | p2->debug (); |
---|
777 | p3->debug (); |
---|
778 | p4->debug (); |
---|
779 | |
---|
780 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
781 | |
---|
782 | |
---|
783 | p1->update (2); |
---|
784 | |
---|
785 | p1->debug (); |
---|
786 | p2->debug (); |
---|
787 | p3->debug (); |
---|
788 | p4->debug (); |
---|
789 | |
---|
790 | p1->destroy (); |
---|
791 | |
---|
792 | |
---|
793 | /* |
---|
794 | WorldEntity* entity; |
---|
795 | printf("counting all entities\n"); |
---|
796 | printf("World::debug() - enumerate()\n"); |
---|
797 | entity = entities->enumerate(); |
---|
798 | while( entity != NULL ) |
---|
799 | { |
---|
800 | if( entity->bDraw ) printf("got an entity\n"); |
---|
801 | entity = entities->nextElement(); |
---|
802 | } |
---|
803 | */ |
---|
804 | } |
---|
805 | |
---|
806 | |
---|
807 | /* |
---|
808 | \brief main loop of the world: executing all world relevant function |
---|
809 | |
---|
810 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
811 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
812 | collisions drawing everything to the screen. |
---|
813 | */ |
---|
814 | void World::mainLoop() |
---|
815 | { |
---|
816 | this->lastFrame = SDL_GetTicks (); |
---|
817 | printf("World::mainLoop() - Entering main loop\n"); |
---|
818 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
819 | { |
---|
820 | // Network |
---|
821 | this->synchronize (); |
---|
822 | // Process input |
---|
823 | this->handleInput (); |
---|
824 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
825 | { |
---|
826 | printf("World::mainLoop() - leaving loop earlier...\n"); |
---|
827 | break; |
---|
828 | } |
---|
829 | // Process time |
---|
830 | this->timeSlice (); |
---|
831 | // Process collision |
---|
832 | this->collision (); |
---|
833 | // Draw |
---|
834 | this->display (); |
---|
835 | |
---|
836 | for( int i = 0; i < 5000000; i++) {} |
---|
837 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
838 | } |
---|
839 | printf("World::mainLoop() - Exiting the main loop\n"); |
---|
840 | } |
---|
841 | |
---|
842 | /** |
---|
843 | \brief synchronize local data with remote data |
---|
844 | */ |
---|
845 | void World::synchronize () |
---|
846 | { |
---|
847 | // Get remote input |
---|
848 | // Update synchronizables |
---|
849 | } |
---|
850 | |
---|
851 | /** |
---|
852 | \brief run all input processing |
---|
853 | |
---|
854 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
855 | sdl and has its own event-passing-queue. |
---|
856 | */ |
---|
857 | void World::handleInput () |
---|
858 | { |
---|
859 | // localinput |
---|
860 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
861 | cn->process(); |
---|
862 | // remoteinput |
---|
863 | } |
---|
864 | |
---|
865 | /** |
---|
866 | \brief advance the timeline |
---|
867 | |
---|
868 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
869 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
870 | a heart-beat. |
---|
871 | */ |
---|
872 | void World::timeSlice () |
---|
873 | { |
---|
874 | Uint32 currentFrame = SDL_GetTicks(); |
---|
875 | if(!this->bPause) |
---|
876 | { |
---|
877 | Uint32 dt = currentFrame - this->lastFrame; |
---|
878 | |
---|
879 | if(dt > 0) |
---|
880 | { |
---|
881 | float fps = 1000/dt; |
---|
882 | printf("fps = %f\n", fps); |
---|
883 | } |
---|
884 | else |
---|
885 | { |
---|
886 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
887 | nothing. |
---|
888 | */ |
---|
889 | printf("fps = 1000 - frame rate is adjusted\n"); |
---|
890 | SDL_Delay(10); |
---|
891 | dt = 10; |
---|
892 | } |
---|
893 | this->timeSlice (dt); |
---|
894 | this->update (); |
---|
895 | this->localCamera->timeSlice(dt); |
---|
896 | } |
---|
897 | this->lastFrame = currentFrame; |
---|
898 | } |
---|
899 | |
---|
900 | |
---|
901 | /** |
---|
902 | \brief compute collision detection |
---|
903 | */ |
---|
904 | void World::collision () |
---|
905 | { |
---|
906 | this->collide (); |
---|
907 | } |
---|
908 | |
---|
909 | |
---|
910 | /** |
---|
911 | \brief render the current frame |
---|
912 | |
---|
913 | clear all buffers and draw the world |
---|
914 | */ |
---|
915 | void World::display () |
---|
916 | { |
---|
917 | // clear buffer |
---|
918 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
919 | // set camera |
---|
920 | this->localCamera->apply (); |
---|
921 | // draw world |
---|
922 | this->draw(); |
---|
923 | // draw HUD |
---|
924 | /* \todo draw HUD */ |
---|
925 | // flip buffers |
---|
926 | SDL_GL_SwapBuffers(); |
---|
927 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
928 | //SDL_Flip (screen); |
---|
929 | } |
---|
930 | |
---|
931 | /** |
---|
932 | \brief give back active camera |
---|
933 | |
---|
934 | this passes back the actualy active camera |
---|
935 | \todo ability to define more than one camera or camera-places |
---|
936 | */ |
---|
937 | Camera* World::getCamera() |
---|
938 | { |
---|
939 | return this->localCamera; |
---|
940 | } |
---|
941 | |
---|
942 | |
---|
943 | /** |
---|
944 | \brief add and spawn a new entity to this world |
---|
945 | \param entity to be added |
---|
946 | */ |
---|
947 | void World::spawn(WorldEntity* entity) |
---|
948 | { |
---|
949 | if( this->nullParent != NULL && entity->parent == NULL) |
---|
950 | this->nullParent->addChild (entity); |
---|
951 | |
---|
952 | this->entities->add (entity); |
---|
953 | |
---|
954 | entity->postSpawn (); |
---|
955 | } |
---|
956 | |
---|
957 | |
---|
958 | /** |
---|
959 | \brief add and spawn a new entity to this world |
---|
960 | \param entity to be added |
---|
961 | \param location where to add |
---|
962 | */ |
---|
963 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
964 | { |
---|
965 | entity->setAbsCoor (absCoor); |
---|
966 | entity->setAbsDir (absDir); |
---|
967 | |
---|
968 | if( this->nullParent != NULL && entity->parent == NULL) |
---|
969 | this->nullParent->addChild (entity); |
---|
970 | |
---|
971 | this->entities->add (entity); |
---|
972 | |
---|
973 | entity->postSpawn (); |
---|
974 | } |
---|
975 | |
---|
976 | |
---|
977 | |
---|
978 | /* |
---|
979 | \brief commands that the world must catch |
---|
980 | \returns false if not used by the world |
---|
981 | */ |
---|
982 | bool World::command(Command* cmd) |
---|
983 | { |
---|
984 | return false; |
---|
985 | } |
---|
986 | |
---|
987 | |
---|
988 | |
---|
989 | |
---|
990 | void World::swap (unsigned char &a, unsigned char &b) |
---|
991 | { |
---|
992 | unsigned char temp; |
---|
993 | temp = a; |
---|
994 | a = b; |
---|
995 | b = temp; |
---|
996 | } |
---|