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