1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: Christian Meyer |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
19 | |
---|
20 | #include "world.h" |
---|
21 | |
---|
22 | #include "orxonox.h" |
---|
23 | |
---|
24 | #include "p_node.h" |
---|
25 | #include "null_parent.h" |
---|
26 | #include "helper_parent.h" |
---|
27 | #include "track_node.h" |
---|
28 | #include "world_entity.h" |
---|
29 | #include "player.h" |
---|
30 | #include "camera.h" |
---|
31 | #include "environment.h" |
---|
32 | #include "skysphere.h" |
---|
33 | #include "terrain.h" |
---|
34 | #include "light.h" |
---|
35 | |
---|
36 | #include "track_manager.h" |
---|
37 | #include "garbage_collector.h" |
---|
38 | #include "simple_animation.h" |
---|
39 | |
---|
40 | #include "command_node.h" |
---|
41 | #include "glmenu_imagescreen.h" |
---|
42 | #include "fontset.h" |
---|
43 | #include "list.h" |
---|
44 | #include "factory.h" |
---|
45 | #include "game_loader.h" |
---|
46 | #include "track_node.h" |
---|
47 | #include "terrain.h" |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | |
---|
53 | CREATE_FACTORY(World); |
---|
54 | |
---|
55 | WorldInterface* WorldInterface::singletonRef = 0; |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | \brief private constructor because of singleton |
---|
60 | */ |
---|
61 | WorldInterface::WorldInterface() |
---|
62 | { |
---|
63 | this->worldIsInitialized = false; |
---|
64 | this->worldReference = NULL; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | \brief public deconstructor |
---|
69 | */ |
---|
70 | WorldInterface::~WorldInterface() |
---|
71 | { |
---|
72 | this->singletonRef = NULL; |
---|
73 | this->worldIsInitialized = false; |
---|
74 | this->worldReference = NULL; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | \brief gets the singleton instance |
---|
79 | \returns singleton instance |
---|
80 | */ |
---|
81 | WorldInterface* WorldInterface::getInstance() |
---|
82 | { |
---|
83 | if( singletonRef == NULL) |
---|
84 | singletonRef = new WorldInterface(); |
---|
85 | return singletonRef; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | \brief initializes the interface |
---|
91 | \param reference to the world |
---|
92 | |
---|
93 | if the worldinterface is not initilizes, there wont be any |
---|
94 | useable interface |
---|
95 | */ |
---|
96 | void WorldInterface::init(World* world) |
---|
97 | { |
---|
98 | this->worldReference = world; |
---|
99 | if( world != NULL) |
---|
100 | { |
---|
101 | this->worldIsInitialized = true; |
---|
102 | PRINTF(3)("WorldInterface up and running\n"); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | \brief gets the entity list from the world |
---|
109 | \return entity list |
---|
110 | */ |
---|
111 | tList<WorldEntity>* WorldInterface::getEntityList() |
---|
112 | { |
---|
113 | if( this->worldIsInitialized) |
---|
114 | return this->worldReference->getEntities(); |
---|
115 | PRINT(1)("Someone tried to use the WorldInterface before it has been initizlized! this can result in SEGFAULTs!\n"); |
---|
116 | return NULL; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | World::World( TiXmlElement* root) |
---|
122 | { |
---|
123 | const char *string; |
---|
124 | char *name; |
---|
125 | int id; |
---|
126 | |
---|
127 | PRINTF0("Creating a World\n"); |
---|
128 | |
---|
129 | // identifier |
---|
130 | string = grabParameter( root, "identifier"); |
---|
131 | if( string == NULL || sscanf(string, "%d", &id) != 1) |
---|
132 | { |
---|
133 | PRINTF0("World is missing a proper 'identifier'\n"); |
---|
134 | this->setStoryID( -1); |
---|
135 | } |
---|
136 | else setStoryID( id); |
---|
137 | |
---|
138 | // path |
---|
139 | string = grabParameter( root, "path"); |
---|
140 | if( string == NULL) |
---|
141 | { |
---|
142 | PRINTF0("World is missing a proper 'path'\n"); |
---|
143 | this->setPath( NULL); |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | name = new char[strlen(string + 2)]; |
---|
148 | strcpy( name, string); |
---|
149 | this->setPath( name); |
---|
150 | } |
---|
151 | |
---|
152 | this->localPlayer = NULL; |
---|
153 | this->entities = new tList<WorldEntity>(); |
---|
154 | |
---|
155 | |
---|
156 | this->setClassName ("World"); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | \brief create a new World |
---|
161 | |
---|
162 | This creates a new empty world! |
---|
163 | */ |
---|
164 | World::World (char* name) |
---|
165 | { |
---|
166 | this->init(name, -1); |
---|
167 | //NullParent* np = NullParent::getInstance(); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | \brief creates a new World... |
---|
172 | \param worldID with this ID |
---|
173 | */ |
---|
174 | World::World (int worldID) |
---|
175 | { |
---|
176 | this->init(NULL, worldID); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | \brief remove the World from memory |
---|
181 | |
---|
182 | delete everything explicitly, that isn't contained in the parenting tree! |
---|
183 | things contained in the tree are deleted automaticaly |
---|
184 | */ |
---|
185 | World::~World () |
---|
186 | { |
---|
187 | PRINTF(3)("World::~World() - deleting current world\n"); |
---|
188 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
189 | cn->unbind(this->localPlayer); |
---|
190 | cn->reset(); |
---|
191 | |
---|
192 | ResourceManager::getInstance()->debug(); |
---|
193 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
194 | ResourceManager::getInstance()->debug(); |
---|
195 | |
---|
196 | delete WorldInterface::getInstance(); |
---|
197 | |
---|
198 | delete this->nullParent; |
---|
199 | delete this->entities; |
---|
200 | delete this->lightMan; |
---|
201 | delete this->trackManager; |
---|
202 | if( this->worldName) delete this->worldName; |
---|
203 | if( this->path) delete this->path; |
---|
204 | |
---|
205 | //delete garbagecollecor |
---|
206 | //delete animator |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | \brief initializes the world. |
---|
211 | |
---|
212 | set all stuff here that is world generic and does not use to much memory |
---|
213 | because the real init() function StoryEntity::init() will be called |
---|
214 | shortly before start of the game. |
---|
215 | since all worlds are initiated/referenced before they will be started. |
---|
216 | NO LEVEL LOADING HERE - NEVER! |
---|
217 | */ |
---|
218 | void World::init(char* name, int worldID) |
---|
219 | { |
---|
220 | this->setClassName ("World"); |
---|
221 | |
---|
222 | //this->worldName = name; |
---|
223 | //this->worldName = new char[strlen(name)+1]; |
---|
224 | //strcpy(this->worldName, name); |
---|
225 | this->debugWorldNr = worldID; |
---|
226 | this->entities = new tList<WorldEntity>(); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | \brief this is executed before load |
---|
232 | |
---|
233 | since the load function sometimes needs data, that has been init before |
---|
234 | the load and after the proceeding storyentity has finished |
---|
235 | */ |
---|
236 | ErrorMessage World::preLoad() |
---|
237 | { |
---|
238 | /* init the world interface */ |
---|
239 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
240 | wi->init(this); |
---|
241 | this->garbageCollector = GarbageCollector::getInstance(); |
---|
242 | this->simpleAnimation = SimpleAnimation::getInstance(); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | /** |
---|
247 | \brief loads the World by initializing all resources, and set their default values. |
---|
248 | */ |
---|
249 | ErrorMessage World::load() |
---|
250 | { |
---|
251 | PRINTF0("> Loading world: '%s'\n", getPath()); |
---|
252 | |
---|
253 | GameLoader* loader = GameLoader::getInstance(); |
---|
254 | |
---|
255 | if( getPath() == NULL) |
---|
256 | { |
---|
257 | PRINTF0("World has no path specified for loading"); |
---|
258 | return (ErrorMessage){213,"Path not specified","World::load()"}; |
---|
259 | } |
---|
260 | |
---|
261 | TiXmlDocument* XMLDoc = new TiXmlDocument( path); |
---|
262 | // load the campaign document |
---|
263 | if( !XMLDoc->LoadFile()) |
---|
264 | //this->glmis->step(); |
---|
265 | |
---|
266 | { |
---|
267 | // report an error |
---|
268 | PRINTF0("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
269 | delete XMLDoc; |
---|
270 | return (ErrorMessage){213,"XML File parsing error","World::load()"}; |
---|
271 | } |
---|
272 | |
---|
273 | // check basic validity |
---|
274 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
275 | assert( root != NULL); |
---|
276 | |
---|
277 | if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile")) |
---|
278 | { |
---|
279 | // report an error |
---|
280 | PRINTF0("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n"); |
---|
281 | delete XMLDoc; |
---|
282 | return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"}; |
---|
283 | } |
---|
284 | |
---|
285 | // load the parameters |
---|
286 | // name |
---|
287 | char* temp; |
---|
288 | const char* string = grabParameter( root, "name"); |
---|
289 | if( string == NULL) |
---|
290 | { |
---|
291 | PRINTF0("World is missing a proper 'name'\n"); |
---|
292 | string = "Unknown"; |
---|
293 | temp = new char[strlen(string + 2)]; |
---|
294 | strcpy( temp, string); |
---|
295 | this->worldName = temp; |
---|
296 | } |
---|
297 | else |
---|
298 | { |
---|
299 | temp = new char[strlen(string + 2)]; |
---|
300 | strcpy( temp, string); |
---|
301 | this->worldName = temp; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | // find WorldEntities |
---|
306 | TiXmlElement* element = root->FirstChildElement( "WorldEntities"); |
---|
307 | |
---|
308 | if( element == NULL) |
---|
309 | { |
---|
310 | PRINTF0("World is missing 'WorldEntities'\n"); |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | element = element->FirstChildElement(); |
---|
315 | // load Players/Objects/Whatever |
---|
316 | PRINTF0("Loading WorldEntities\n"); |
---|
317 | while( element != NULL) |
---|
318 | { |
---|
319 | WorldEntity* created = (WorldEntity*) loader->fabricate( element); |
---|
320 | if( created != NULL) this->spawn( created); |
---|
321 | // if we load a 'Player' we use it as localPlayer |
---|
322 | //todo do this more elegant |
---|
323 | if( element->Value() != NULL && !strcmp( element->Value(), "Player")) localPlayer = (Player*) created; |
---|
324 | element = element->NextSiblingElement(); |
---|
325 | } |
---|
326 | PRINTF0("Done loading WorldEntities\n"); |
---|
327 | } |
---|
328 | |
---|
329 | // find Track |
---|
330 | element = root->FirstChildElement( "Track"); |
---|
331 | if( element == NULL) |
---|
332 | { |
---|
333 | PRINTF0("============>>>>>>>>>>>>>>>>>World is missing a 'Track'\n"); |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | //load track |
---|
338 | PRINTF0("============>>>>>>>>>>>>>>>>Loading Track\n"); |
---|
339 | trackManager = TrackManager::getInstance(); |
---|
340 | trackManager->loadTrack( element); |
---|
341 | trackManager->finalize(); |
---|
342 | PRINTF0("============>>>>>>>>>>>>>>>>Done loading Track\n"); |
---|
343 | } |
---|
344 | |
---|
345 | // free the XML data |
---|
346 | delete XMLDoc; |
---|
347 | |
---|
348 | // finalize world |
---|
349 | // initialize Font |
---|
350 | testFont = new FontSet(); |
---|
351 | testFont->buildFont("../data/pictures/font.tga"); |
---|
352 | |
---|
353 | // create null parent |
---|
354 | this->nullParent = NullParent::getInstance (); |
---|
355 | this->nullParent->setName ("NullParent"); |
---|
356 | |
---|
357 | // finalize myPlayer |
---|
358 | if( localPlayer == NULL) |
---|
359 | { |
---|
360 | PRINTF0("No Player specified in World '%s'\n", this->worldName); |
---|
361 | return (ErrorMessage){213,"No Player defined","World::load()"}; |
---|
362 | } |
---|
363 | |
---|
364 | // bind input |
---|
365 | Orxonox *orx = Orxonox::getInstance (); |
---|
366 | orx->getLocalInput()->bind (localPlayer); |
---|
367 | |
---|
368 | // bind camera |
---|
369 | this->localCamera = new Camera(); |
---|
370 | this->localCamera->setName ("camera"); |
---|
371 | //this->localCamera->bind (localPlayer); |
---|
372 | this->localPlayer->addChild (this->localCamera); |
---|
373 | |
---|
374 | |
---|
375 | // stuff beyond this point remains to be loaded properly |
---|
376 | |
---|
377 | /*monitor progress*/ |
---|
378 | // this->glmis->step(); |
---|
379 | |
---|
380 | // LIGHT initialisation |
---|
381 | lightMan = LightManager::getInstance(); |
---|
382 | lightMan->setAmbientColor(.1,.1,.1); |
---|
383 | lightMan->addLight(); |
---|
384 | // lightMan->setAttenuation(1.0, .01, 0.0); |
---|
385 | // lightMan->setDiffuseColor(1,1,1); |
---|
386 | // lightMan->addLight(1); |
---|
387 | // lightMan->setPosition(20, 10, -20); |
---|
388 | // lightMan->setDiffuseColor(0,0,0); |
---|
389 | lightMan->debug(); |
---|
390 | lightMan->setPosition(-5.0, 10.0, -40.0); |
---|
391 | |
---|
392 | |
---|
393 | // Create SkySphere |
---|
394 | this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
395 | this->skySphere->setName("SkySphere"); |
---|
396 | this->localCamera->addChild(this->skySphere); |
---|
397 | this->skySphere->setMode(PNODE_MOVEMENT); |
---|
398 | |
---|
399 | |
---|
400 | // trackManager->setBindSlave(env); |
---|
401 | PNode* tn = trackManager->getTrackNode(); |
---|
402 | tn->addChild(this->localPlayer); |
---|
403 | |
---|
404 | //localCamera->setParent(TrackNode::getInstance()); |
---|
405 | tn->addChild(this->localCamera); |
---|
406 | // localCamera->lookAt(tn); |
---|
407 | this->localPlayer->setMode(PNODE_ALL); |
---|
408 | //Vector* cameraOffset = new Vector (0, 5, -10); |
---|
409 | //trackManager->condition(2, LEFTRIGHT, this->localPlayer); |
---|
410 | |
---|
411 | // initialize debug coord system |
---|
412 | objectList = glGenLists(1); |
---|
413 | glNewList (objectList, GL_COMPILE); |
---|
414 | |
---|
415 | trackManager->drawGraph(.01); |
---|
416 | trackManager->debug(2); |
---|
417 | glEndList(); |
---|
418 | |
---|
419 | terrain = new Terrain("../data/worlds/newGround.obj"); |
---|
420 | terrain->setRelCoor(new Vector(0,-10,0)); |
---|
421 | this->spawn(terrain); |
---|
422 | } |
---|
423 | |
---|
424 | |
---|
425 | /** |
---|
426 | \brief initializes a new World shortly before start |
---|
427 | |
---|
428 | this is the function, that will be loaded shortly before the world is |
---|
429 | started |
---|
430 | */ |
---|
431 | ErrorMessage World::init() |
---|
432 | { |
---|
433 | this->bPause = false; |
---|
434 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
435 | cn->addToWorld(this); |
---|
436 | cn->enable(true); |
---|
437 | PRINTF0("> Done Loading world: '%s'\n", getPath()); |
---|
438 | } |
---|
439 | |
---|
440 | |
---|
441 | /** |
---|
442 | \brief starts the World |
---|
443 | */ |
---|
444 | ErrorMessage World::start() |
---|
445 | { |
---|
446 | PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
447 | this->bQuitOrxonox = false; |
---|
448 | this->bQuitCurrentGame = false; |
---|
449 | this->mainLoop(); |
---|
450 | } |
---|
451 | |
---|
452 | /** |
---|
453 | \brief stops the world. |
---|
454 | |
---|
455 | This happens, when the player decides to end the Level. |
---|
456 | */ |
---|
457 | ErrorMessage World::stop() |
---|
458 | { |
---|
459 | PRINTF(3)("World::stop() - got stop signal\n"); |
---|
460 | this->bQuitCurrentGame = true; |
---|
461 | } |
---|
462 | |
---|
463 | /** |
---|
464 | \brief pauses the Game |
---|
465 | */ |
---|
466 | ErrorMessage World::pause() |
---|
467 | { |
---|
468 | this->isPaused = true; |
---|
469 | } |
---|
470 | |
---|
471 | /** |
---|
472 | \brief ends the pause Phase |
---|
473 | */ |
---|
474 | ErrorMessage World::resume() |
---|
475 | { |
---|
476 | this->isPaused = false; |
---|
477 | } |
---|
478 | |
---|
479 | /** |
---|
480 | \brief destroys the World |
---|
481 | */ |
---|
482 | ErrorMessage World::destroy() |
---|
483 | { |
---|
484 | |
---|
485 | } |
---|
486 | |
---|
487 | /** |
---|
488 | \brief shows the loading screen |
---|
489 | */ |
---|
490 | void World::displayLoadScreen () |
---|
491 | { |
---|
492 | PRINTF(3)("World::displayLoadScreen - start\n"); |
---|
493 | |
---|
494 | //GLMenuImageScreen* |
---|
495 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
496 | this->glmis->init(); |
---|
497 | this->glmis->setMaximum(8); |
---|
498 | this->glmis->draw(); |
---|
499 | |
---|
500 | PRINTF(3)("World::displayLoadScreen - end\n"); |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | \brief removes the loadscreen, and changes over to the game |
---|
505 | |
---|
506 | \todo take out the delay |
---|
507 | */ |
---|
508 | void World::releaseLoadScreen () |
---|
509 | { |
---|
510 | PRINTF(3)("World::releaseLoadScreen - start\n"); |
---|
511 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
512 | //SDL_Delay(500); |
---|
513 | PRINTF(3)("World::releaseLoadScreen - end\n"); |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | /** |
---|
518 | \brief gets the list of entities from the world |
---|
519 | \returns entity list |
---|
520 | */ |
---|
521 | tList<WorldEntity>* World::getEntities() |
---|
522 | { |
---|
523 | return this->entities; |
---|
524 | } |
---|
525 | |
---|
526 | |
---|
527 | /** |
---|
528 | \brief this returns the current game time |
---|
529 | \returns elapsed game time |
---|
530 | */ |
---|
531 | double World::getGameTime() |
---|
532 | { |
---|
533 | return this->gameTime; |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | /** |
---|
538 | \brief checks for collisions |
---|
539 | |
---|
540 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
541 | between them. In case of collisions the collide() method of the corresponding entities |
---|
542 | is called. |
---|
543 | */ |
---|
544 | void World::collide () |
---|
545 | { |
---|
546 | /* |
---|
547 | List *a, *b; |
---|
548 | WorldEntity *aobj, *bobj; |
---|
549 | |
---|
550 | a = entities; |
---|
551 | |
---|
552 | while( a != NULL) |
---|
553 | { |
---|
554 | aobj = a->nextElement(); |
---|
555 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
556 | { |
---|
557 | b = a->nextElement(); |
---|
558 | while( b != NULL ) |
---|
559 | { |
---|
560 | bobj = b->nextElement(); |
---|
561 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
562 | { |
---|
563 | unsigned long ahitflg, bhitflg; |
---|
564 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
565 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
566 | &bhitflg) ); |
---|
567 | { |
---|
568 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
569 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
570 | } |
---|
571 | } |
---|
572 | b = b->nextElement(); |
---|
573 | } |
---|
574 | } |
---|
575 | a = a->enumerate(); |
---|
576 | } |
---|
577 | */ |
---|
578 | } |
---|
579 | |
---|
580 | /** |
---|
581 | \brief runs through all entities calling their draw() methods |
---|
582 | */ |
---|
583 | void World::draw () |
---|
584 | { |
---|
585 | /* draw entities */ |
---|
586 | WorldEntity* entity; |
---|
587 | glLoadIdentity(); |
---|
588 | |
---|
589 | //entity = this->entities->enumerate(); |
---|
590 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
591 | entity = iterator->nextElement(); |
---|
592 | while( entity != NULL ) |
---|
593 | { |
---|
594 | if( entity->bDraw ) entity->draw(); |
---|
595 | //entity = this->entities->nextElement(); |
---|
596 | entity = iterator->nextElement(); |
---|
597 | } |
---|
598 | delete iterator; |
---|
599 | |
---|
600 | glCallList (objectList); |
---|
601 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
602 | skySphere->draw(); |
---|
603 | |
---|
604 | testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION); |
---|
605 | |
---|
606 | lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes // |
---|
607 | } |
---|
608 | |
---|
609 | |
---|
610 | /** |
---|
611 | \brief function to put your own debug stuff into it. it can display informations about |
---|
612 | the current class/procedure |
---|
613 | */ |
---|
614 | void World::debug() |
---|
615 | { |
---|
616 | PRINTF(2)("debug() - starting debug\n"); |
---|
617 | PNode* p1 = NullParent::getInstance (); |
---|
618 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
619 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
620 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
621 | |
---|
622 | p1->debug (); |
---|
623 | p2->debug (); |
---|
624 | p3->debug (); |
---|
625 | p4->debug (); |
---|
626 | |
---|
627 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
628 | |
---|
629 | printf("World::debug() - shift\n"); |
---|
630 | p1->debug (); |
---|
631 | p2->debug (); |
---|
632 | p3->debug (); |
---|
633 | p4->debug (); |
---|
634 | |
---|
635 | p1->update (0); |
---|
636 | |
---|
637 | printf ("World::debug() - update\n"); |
---|
638 | p1->debug (); |
---|
639 | p2->debug (); |
---|
640 | p3->debug (); |
---|
641 | p4->debug (); |
---|
642 | |
---|
643 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
644 | p1->update (0); |
---|
645 | |
---|
646 | p1->debug (); |
---|
647 | p2->debug (); |
---|
648 | p3->debug (); |
---|
649 | p4->debug (); |
---|
650 | |
---|
651 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
652 | |
---|
653 | |
---|
654 | p1->update (0); |
---|
655 | |
---|
656 | p1->debug (); |
---|
657 | p2->debug (); |
---|
658 | p3->debug (); |
---|
659 | p4->debug (); |
---|
660 | |
---|
661 | delete p1; |
---|
662 | |
---|
663 | |
---|
664 | /* |
---|
665 | WorldEntity* entity; |
---|
666 | printf("counting all entities\n"); |
---|
667 | printf("World::debug() - enumerate()\n"); |
---|
668 | entity = entities->enumerate(); |
---|
669 | while( entity != NULL ) |
---|
670 | { |
---|
671 | if( entity->bDraw ) printf("got an entity\n"); |
---|
672 | entity = entities->nextElement(); |
---|
673 | } |
---|
674 | */ |
---|
675 | } |
---|
676 | |
---|
677 | |
---|
678 | /** |
---|
679 | \brief main loop of the world: executing all world relevant function |
---|
680 | |
---|
681 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
682 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
683 | collisions drawing everything to the screen. |
---|
684 | */ |
---|
685 | void World::mainLoop() |
---|
686 | { |
---|
687 | this->lastFrame = SDL_GetTicks (); |
---|
688 | printf("World::mainLoop() - Entering main loop of '%s'\n", this->worldName); |
---|
689 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
690 | { |
---|
691 | PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize()); |
---|
692 | // Network |
---|
693 | this->synchronize (); |
---|
694 | // Process input |
---|
695 | this->handleInput (); |
---|
696 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
697 | break; |
---|
698 | // Process time |
---|
699 | this->tick (); |
---|
700 | // Update the state |
---|
701 | this->update (); |
---|
702 | // Process collision |
---|
703 | this->collide (); |
---|
704 | // Draw |
---|
705 | this->display (); |
---|
706 | |
---|
707 | // for( int i = 0; i < 5000000; i++) {} |
---|
708 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
709 | } |
---|
710 | PRINTF(3)("World::mainLoop() - Exiting the main loop\n"); |
---|
711 | } |
---|
712 | |
---|
713 | |
---|
714 | /** |
---|
715 | \brief synchronize local data with remote data |
---|
716 | */ |
---|
717 | void World::synchronize () |
---|
718 | { |
---|
719 | // Get remote input |
---|
720 | // Update synchronizables |
---|
721 | } |
---|
722 | |
---|
723 | |
---|
724 | /** |
---|
725 | \brief run all input processing |
---|
726 | |
---|
727 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
728 | sdl and has its own event-passing-queue. |
---|
729 | */ |
---|
730 | void World::handleInput () |
---|
731 | { |
---|
732 | // localinput |
---|
733 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
734 | cn->process(); |
---|
735 | // remoteinput |
---|
736 | } |
---|
737 | |
---|
738 | |
---|
739 | /** |
---|
740 | \brief advance the timeline |
---|
741 | |
---|
742 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
743 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
744 | a heart-beat. |
---|
745 | */ |
---|
746 | void World::tick () |
---|
747 | { |
---|
748 | Uint32 currentFrame = SDL_GetTicks(); |
---|
749 | if(!this->bPause) |
---|
750 | { |
---|
751 | this->dt = currentFrame - this->lastFrame; |
---|
752 | |
---|
753 | if( this->dt > 0) |
---|
754 | { |
---|
755 | float fps = 1000/dt; |
---|
756 | PRINTF(3)("fps = %f\n", fps); |
---|
757 | } |
---|
758 | else |
---|
759 | { |
---|
760 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
761 | nothing. |
---|
762 | */ |
---|
763 | PRINTF(2)("fps = 1000 - frame rate is adjusted\n"); |
---|
764 | SDL_Delay(10); |
---|
765 | this->dt = 10; |
---|
766 | } |
---|
767 | //this->timeSlice (dt); |
---|
768 | |
---|
769 | /* function to let all entities tick (iterate through list) */ |
---|
770 | float seconds = this->dt / 1000.0; |
---|
771 | this->gameTime += seconds; |
---|
772 | //entity = entities->enumerate(); |
---|
773 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
774 | WorldEntity* entity = iterator->nextElement(); |
---|
775 | while( entity != NULL) |
---|
776 | { |
---|
777 | entity->tick (seconds); |
---|
778 | entity = iterator->nextElement(); |
---|
779 | } |
---|
780 | delete iterator; |
---|
781 | //skySphere->updatePosition(localCamera->absCoordinate); |
---|
782 | |
---|
783 | /* update tick the rest */ |
---|
784 | this->trackManager->tick(this->dt); |
---|
785 | assert( this->localCamera != NULL); |
---|
786 | assert( this->trackManager != NULL); |
---|
787 | this->localCamera->tick(this->dt); |
---|
788 | this->garbageCollector->tick(seconds); |
---|
789 | this->simpleAnimation->tick(seconds); |
---|
790 | } |
---|
791 | this->lastFrame = currentFrame; |
---|
792 | } |
---|
793 | |
---|
794 | |
---|
795 | /** |
---|
796 | \brief this function gives the world a consistant state |
---|
797 | |
---|
798 | after ticking (updating the world state) this will give a constistant |
---|
799 | state to the whole system. |
---|
800 | */ |
---|
801 | void World::update() |
---|
802 | { |
---|
803 | this->garbageCollector->update(); |
---|
804 | this->nullParent->update (dt); |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | /** |
---|
809 | \brief render the current frame |
---|
810 | |
---|
811 | clear all buffers and draw the world |
---|
812 | */ |
---|
813 | void World::display () |
---|
814 | { |
---|
815 | // clear buffer |
---|
816 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
817 | // set camera |
---|
818 | this->localCamera->apply (); |
---|
819 | // draw world |
---|
820 | this->draw(); |
---|
821 | // draw HUD |
---|
822 | /* \todo draw HUD */ |
---|
823 | // flip buffers |
---|
824 | SDL_GL_SwapBuffers(); |
---|
825 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
826 | //SDL_Flip (screen); |
---|
827 | } |
---|
828 | |
---|
829 | |
---|
830 | /** |
---|
831 | \brief add and spawn a new entity to this world |
---|
832 | \param entity to be added |
---|
833 | */ |
---|
834 | void World::spawn(WorldEntity* entity) |
---|
835 | { |
---|
836 | this->entities->add (entity); |
---|
837 | entity->postSpawn (); |
---|
838 | } |
---|
839 | |
---|
840 | |
---|
841 | /** |
---|
842 | \brief add and spawn a new entity to this world |
---|
843 | \param entity to be added |
---|
844 | \param absCoor At what coordinates to add this entity. |
---|
845 | \param absDir In which direction should it look. |
---|
846 | */ |
---|
847 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
848 | { |
---|
849 | this->entities->add (entity); |
---|
850 | |
---|
851 | entity->setAbsCoor (absCoor); |
---|
852 | entity->setAbsDir (absDir); |
---|
853 | |
---|
854 | entity->postSpawn (); |
---|
855 | } |
---|
856 | |
---|
857 | |
---|
858 | /** |
---|
859 | \brief add and spawn a new entity to this world |
---|
860 | \param entity to be added |
---|
861 | \param entity to be added to (PNode) |
---|
862 | \param At what relative coordinates to add this entity. |
---|
863 | \param In which relative direction should it look. |
---|
864 | */ |
---|
865 | void World::spawn(WorldEntity* entity, PNode* parentNode, |
---|
866 | Vector* relCoor, Quaternion* relDir, |
---|
867 | int parentingMode) |
---|
868 | { |
---|
869 | this->nullParent = NullParent::getInstance(); |
---|
870 | if( parentNode != NULL) |
---|
871 | { |
---|
872 | parentNode->addChild (entity); |
---|
873 | |
---|
874 | entity->setRelCoor (relCoor); |
---|
875 | entity->setRelDir (relDir); |
---|
876 | entity->setMode(parentingMode); |
---|
877 | |
---|
878 | this->entities->add (entity); |
---|
879 | |
---|
880 | entity->postSpawn (); |
---|
881 | } |
---|
882 | } |
---|
883 | |
---|
884 | |
---|
885 | |
---|
886 | /** |
---|
887 | \brief commands that the world must catch |
---|
888 | \returns false if not used by the world |
---|
889 | */ |
---|
890 | bool World::command(Command* cmd) |
---|
891 | { |
---|
892 | if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL); |
---|
893 | else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND); |
---|
894 | else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT); |
---|
895 | else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT); |
---|
896 | else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT); |
---|
897 | else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP); |
---|
898 | |
---|
899 | return false; |
---|
900 | } |
---|
901 | |
---|
902 | void World::setPath( char* name) |
---|
903 | { |
---|
904 | path = name; |
---|
905 | } |
---|
906 | |
---|
907 | char* World::getPath() |
---|
908 | { |
---|
909 | return path; |
---|
910 | } |
---|