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->debugWorldNr = worldID; |
---|
224 | this->entities = new tList<WorldEntity>(); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /** |
---|
229 | \brief this is executed before load |
---|
230 | |
---|
231 | since the load function sometimes needs data, that has been init before |
---|
232 | the load and after the proceeding storyentity has finished |
---|
233 | */ |
---|
234 | ErrorMessage World::preLoad() |
---|
235 | { |
---|
236 | /* init the world interface */ |
---|
237 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
238 | wi->init(this); |
---|
239 | this->garbageCollector = GarbageCollector::getInstance(); |
---|
240 | this->simpleAnimation = SimpleAnimation::getInstance(); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | \brief loads the World by initializing all resources, and set their default values. |
---|
246 | */ |
---|
247 | ErrorMessage World::load() |
---|
248 | { |
---|
249 | PRINTF0("> Loading world: '%s'\n", getPath()); |
---|
250 | |
---|
251 | GameLoader* loader = GameLoader::getInstance(); |
---|
252 | |
---|
253 | if( getPath() == NULL) |
---|
254 | { |
---|
255 | PRINTF0("World has no path specified for loading"); |
---|
256 | return (ErrorMessage){213,"Path not specified","World::load()"}; |
---|
257 | } |
---|
258 | |
---|
259 | TiXmlDocument* XMLDoc = new TiXmlDocument( path); |
---|
260 | // load the campaign document |
---|
261 | if( !XMLDoc->LoadFile()) |
---|
262 | this->glmis->step(); |
---|
263 | |
---|
264 | { |
---|
265 | // report an error |
---|
266 | PRINTF0("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
267 | delete XMLDoc; |
---|
268 | return (ErrorMessage){213,"XML File parsing error","World::load()"}; |
---|
269 | } |
---|
270 | |
---|
271 | // check basic validity |
---|
272 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
273 | assert( root != NULL); |
---|
274 | |
---|
275 | if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile")) |
---|
276 | { |
---|
277 | // report an error |
---|
278 | PRINTF0("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n"); |
---|
279 | delete XMLDoc; |
---|
280 | return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"}; |
---|
281 | } |
---|
282 | |
---|
283 | // load the parameters |
---|
284 | // name |
---|
285 | char* temp; |
---|
286 | const char* string = grabParameter( root, "name"); |
---|
287 | if( string == NULL) |
---|
288 | { |
---|
289 | PRINTF0("World is missing a proper 'name'\n"); |
---|
290 | string = "Unknown"; |
---|
291 | temp = new char[strlen(string + 2)]; |
---|
292 | strcpy( temp, string); |
---|
293 | this->worldName = temp; |
---|
294 | } |
---|
295 | else |
---|
296 | { |
---|
297 | temp = new char[strlen(string + 2)]; |
---|
298 | strcpy( temp, string); |
---|
299 | this->worldName = temp; |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | // find WorldEntities |
---|
304 | TiXmlElement* element = root->FirstChildElement( "WorldEntities"); |
---|
305 | |
---|
306 | if( element == NULL) |
---|
307 | { |
---|
308 | PRINTF0("World is missing 'WorldEntities'\n"); |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | element = element->FirstChildElement(); |
---|
313 | // load Players/Objects/Whatever |
---|
314 | PRINTF0("Loading WorldEntities\n"); |
---|
315 | while( element != NULL) |
---|
316 | { |
---|
317 | WorldEntity* created = (WorldEntity*) loader->fabricate( element); |
---|
318 | if( created != NULL) this->spawn( created); |
---|
319 | // if we load a 'Player' we use it as localPlayer |
---|
320 | //todo do this more elegant |
---|
321 | if( element->Value() != NULL && !strcmp( element->Value(), "Player")) localPlayer = (Player*) created; |
---|
322 | element = element->NextSiblingElement(); |
---|
323 | } |
---|
324 | PRINTF0("Done loading WorldEntities\n"); |
---|
325 | } |
---|
326 | |
---|
327 | // find Track |
---|
328 | element = root->FirstChildElement( "Track"); |
---|
329 | if( element == NULL) |
---|
330 | { |
---|
331 | PRINTF0("World is missing a 'Track'\n"); |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | //load track |
---|
336 | PRINTF0("Loading Track\n"); |
---|
337 | trackManager = TrackManager::getInstance(); |
---|
338 | trackManager->loadTrack( element); |
---|
339 | trackManager->finalize(); |
---|
340 | PRINTF0("Done loading Track\n"); |
---|
341 | } |
---|
342 | |
---|
343 | // free the XML data |
---|
344 | delete XMLDoc; |
---|
345 | |
---|
346 | // finalize world |
---|
347 | // initialize Font |
---|
348 | testFont = new FontSet(); |
---|
349 | testFont->buildFont("../data/pictures/font.tga"); |
---|
350 | |
---|
351 | // create null parent |
---|
352 | this->nullParent = NullParent::getInstance (); |
---|
353 | this->nullParent->setName ("NullParent"); |
---|
354 | |
---|
355 | // finalize myPlayer |
---|
356 | if( localPlayer == NULL) |
---|
357 | { |
---|
358 | PRINTF0("No Player specified in World '%s'\n", this->worldName); |
---|
359 | return (ErrorMessage){213,"No Player defined","World::load()"}; |
---|
360 | } |
---|
361 | |
---|
362 | // bind input |
---|
363 | Orxonox *orx = Orxonox::getInstance (); |
---|
364 | orx->getLocalInput()->bind (localPlayer); |
---|
365 | |
---|
366 | // bind camera |
---|
367 | this->localCamera = new Camera(); |
---|
368 | this->localCamera->setName ("camera"); |
---|
369 | //this->localCamera->bind (localPlayer); |
---|
370 | this->localPlayer->addChild (this->localCamera); |
---|
371 | |
---|
372 | |
---|
373 | // stuff beyond this point remains to be loaded properly |
---|
374 | |
---|
375 | /*monitor progress*/ |
---|
376 | // this->glmis->step(); |
---|
377 | |
---|
378 | // LIGHT initialisation |
---|
379 | lightMan = LightManager::getInstance(); |
---|
380 | lightMan->setAmbientColor(.1,.1,.1); |
---|
381 | lightMan->addLight(); |
---|
382 | // lightMan->setAttenuation(1.0, .01, 0.0); |
---|
383 | // lightMan->setDiffuseColor(1,1,1); |
---|
384 | // lightMan->addLight(1); |
---|
385 | // lightMan->setPosition(20, 10, -20); |
---|
386 | // lightMan->setDiffuseColor(0,0,0); |
---|
387 | lightMan->debug(); |
---|
388 | lightMan->setPosition(-5.0, 10.0, -40.0); |
---|
389 | |
---|
390 | |
---|
391 | // Create SkySphere |
---|
392 | this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
393 | this->skySphere->setName("SkySphere"); |
---|
394 | this->localCamera->addChild(this->skySphere); |
---|
395 | this->skySphere->setMode(PNODE_MOVEMENT); |
---|
396 | |
---|
397 | |
---|
398 | // trackManager->setBindSlave(env); |
---|
399 | PNode* tn = trackManager->getTrackNode(); |
---|
400 | tn->addChild(this->localPlayer); |
---|
401 | |
---|
402 | //localCamera->setParent(TrackNode::getInstance()); |
---|
403 | tn->addChild(this->localCamera); |
---|
404 | // localCamera->lookAt(tn); |
---|
405 | this->localPlayer->setMode(PNODE_ALL); |
---|
406 | //Vector* cameraOffset = new Vector (0, 5, -10); |
---|
407 | //trackManager->condition(2, LEFTRIGHT, this->localPlayer); |
---|
408 | |
---|
409 | // initialize debug coord system |
---|
410 | objectList = glGenLists(1); |
---|
411 | glNewList (objectList, GL_COMPILE); |
---|
412 | |
---|
413 | trackManager->drawGraph(.01); |
---|
414 | trackManager->debug(2); |
---|
415 | glEndList(); |
---|
416 | |
---|
417 | terrain = new Terrain("../data/worlds/newGround.obj"); |
---|
418 | terrain->setRelCoor(new Vector(0,-10,0)); |
---|
419 | this->spawn(terrain); |
---|
420 | } |
---|
421 | |
---|
422 | |
---|
423 | /** |
---|
424 | \brief initializes a new World shortly before start |
---|
425 | |
---|
426 | this is the function, that will be loaded shortly before the world is |
---|
427 | started |
---|
428 | */ |
---|
429 | ErrorMessage World::init() |
---|
430 | { |
---|
431 | this->bPause = false; |
---|
432 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
433 | cn->addToWorld(this); |
---|
434 | cn->enable(true); |
---|
435 | PRINTF0("> Done Loading world: '%s'\n", getPath()); |
---|
436 | } |
---|
437 | |
---|
438 | |
---|
439 | /** |
---|
440 | \brief starts the World |
---|
441 | */ |
---|
442 | ErrorMessage World::start() |
---|
443 | { |
---|
444 | PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
445 | this->bQuitOrxonox = false; |
---|
446 | this->bQuitCurrentGame = false; |
---|
447 | this->mainLoop(); |
---|
448 | } |
---|
449 | |
---|
450 | /** |
---|
451 | \brief stops the world. |
---|
452 | |
---|
453 | This happens, when the player decides to end the Level. |
---|
454 | */ |
---|
455 | ErrorMessage World::stop() |
---|
456 | { |
---|
457 | PRINTF(3)("World::stop() - got stop signal\n"); |
---|
458 | this->bQuitCurrentGame = true; |
---|
459 | } |
---|
460 | |
---|
461 | /** |
---|
462 | \brief pauses the Game |
---|
463 | */ |
---|
464 | ErrorMessage World::pause() |
---|
465 | { |
---|
466 | this->isPaused = true; |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | \brief ends the pause Phase |
---|
471 | */ |
---|
472 | ErrorMessage World::resume() |
---|
473 | { |
---|
474 | this->isPaused = false; |
---|
475 | } |
---|
476 | |
---|
477 | /** |
---|
478 | \brief destroys the World |
---|
479 | */ |
---|
480 | ErrorMessage World::destroy() |
---|
481 | { |
---|
482 | |
---|
483 | } |
---|
484 | |
---|
485 | /** |
---|
486 | \brief shows the loading screen |
---|
487 | */ |
---|
488 | void World::displayLoadScreen () |
---|
489 | { |
---|
490 | PRINTF(3)("World::displayLoadScreen - start\n"); |
---|
491 | |
---|
492 | //GLMenuImageScreen* |
---|
493 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
494 | this->glmis->init(); |
---|
495 | this->glmis->setMaximum(8); |
---|
496 | this->glmis->draw(); |
---|
497 | |
---|
498 | PRINTF(3)("World::displayLoadScreen - end\n"); |
---|
499 | } |
---|
500 | |
---|
501 | /** |
---|
502 | \brief removes the loadscreen, and changes over to the game |
---|
503 | |
---|
504 | \todo take out the delay |
---|
505 | */ |
---|
506 | void World::releaseLoadScreen () |
---|
507 | { |
---|
508 | PRINTF(3)("World::releaseLoadScreen - start\n"); |
---|
509 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
510 | //SDL_Delay(500); |
---|
511 | PRINTF(3)("World::releaseLoadScreen - end\n"); |
---|
512 | } |
---|
513 | |
---|
514 | |
---|
515 | /** |
---|
516 | \brief gets the list of entities from the world |
---|
517 | \returns entity list |
---|
518 | */ |
---|
519 | tList<WorldEntity>* World::getEntities() |
---|
520 | { |
---|
521 | return this->entities; |
---|
522 | } |
---|
523 | |
---|
524 | |
---|
525 | /** |
---|
526 | \brief this returns the current game time |
---|
527 | \returns elapsed game time |
---|
528 | */ |
---|
529 | double World::getGameTime() |
---|
530 | { |
---|
531 | return this->gameTime; |
---|
532 | } |
---|
533 | |
---|
534 | |
---|
535 | /** |
---|
536 | \brief checks for collisions |
---|
537 | |
---|
538 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
539 | between them. In case of collisions the collide() method of the corresponding entities |
---|
540 | is called. |
---|
541 | */ |
---|
542 | void World::collide () |
---|
543 | { |
---|
544 | /* |
---|
545 | List *a, *b; |
---|
546 | WorldEntity *aobj, *bobj; |
---|
547 | |
---|
548 | a = entities; |
---|
549 | |
---|
550 | while( a != NULL) |
---|
551 | { |
---|
552 | aobj = a->nextElement(); |
---|
553 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
554 | { |
---|
555 | b = a->nextElement(); |
---|
556 | while( b != NULL ) |
---|
557 | { |
---|
558 | bobj = b->nextElement(); |
---|
559 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
560 | { |
---|
561 | unsigned long ahitflg, bhitflg; |
---|
562 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
563 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
564 | &bhitflg) ); |
---|
565 | { |
---|
566 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
567 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
568 | } |
---|
569 | } |
---|
570 | b = b->nextElement(); |
---|
571 | } |
---|
572 | } |
---|
573 | a = a->enumerate(); |
---|
574 | } |
---|
575 | */ |
---|
576 | } |
---|
577 | |
---|
578 | /** |
---|
579 | \brief runs through all entities calling their draw() methods |
---|
580 | */ |
---|
581 | void World::draw () |
---|
582 | { |
---|
583 | /* draw entities */ |
---|
584 | WorldEntity* entity; |
---|
585 | glLoadIdentity(); |
---|
586 | |
---|
587 | //entity = this->entities->enumerate(); |
---|
588 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
589 | entity = iterator->nextElement(); |
---|
590 | while( entity != NULL ) |
---|
591 | { |
---|
592 | if( entity->bDraw ) entity->draw(); |
---|
593 | //entity = this->entities->nextElement(); |
---|
594 | entity = iterator->nextElement(); |
---|
595 | } |
---|
596 | delete iterator; |
---|
597 | |
---|
598 | glCallList (objectList); |
---|
599 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
600 | skySphere->draw(); |
---|
601 | |
---|
602 | testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION); |
---|
603 | |
---|
604 | lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes // |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | /** |
---|
609 | \brief function to put your own debug stuff into it. it can display informations about |
---|
610 | the current class/procedure |
---|
611 | */ |
---|
612 | void World::debug() |
---|
613 | { |
---|
614 | PRINTF(2)("debug() - starting debug\n"); |
---|
615 | PNode* p1 = NullParent::getInstance (); |
---|
616 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
617 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
618 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
619 | |
---|
620 | p1->debug (); |
---|
621 | p2->debug (); |
---|
622 | p3->debug (); |
---|
623 | p4->debug (); |
---|
624 | |
---|
625 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
626 | |
---|
627 | printf("World::debug() - shift\n"); |
---|
628 | p1->debug (); |
---|
629 | p2->debug (); |
---|
630 | p3->debug (); |
---|
631 | p4->debug (); |
---|
632 | |
---|
633 | p1->update (0); |
---|
634 | |
---|
635 | printf ("World::debug() - update\n"); |
---|
636 | p1->debug (); |
---|
637 | p2->debug (); |
---|
638 | p3->debug (); |
---|
639 | p4->debug (); |
---|
640 | |
---|
641 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
642 | p1->update (0); |
---|
643 | |
---|
644 | p1->debug (); |
---|
645 | p2->debug (); |
---|
646 | p3->debug (); |
---|
647 | p4->debug (); |
---|
648 | |
---|
649 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
650 | |
---|
651 | |
---|
652 | p1->update (0); |
---|
653 | |
---|
654 | p1->debug (); |
---|
655 | p2->debug (); |
---|
656 | p3->debug (); |
---|
657 | p4->debug (); |
---|
658 | |
---|
659 | delete p1; |
---|
660 | |
---|
661 | |
---|
662 | /* |
---|
663 | WorldEntity* entity; |
---|
664 | printf("counting all entities\n"); |
---|
665 | printf("World::debug() - enumerate()\n"); |
---|
666 | entity = entities->enumerate(); |
---|
667 | while( entity != NULL ) |
---|
668 | { |
---|
669 | if( entity->bDraw ) printf("got an entity\n"); |
---|
670 | entity = entities->nextElement(); |
---|
671 | } |
---|
672 | */ |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | /** |
---|
677 | \brief main loop of the world: executing all world relevant function |
---|
678 | |
---|
679 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
680 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
681 | collisions drawing everything to the screen. |
---|
682 | */ |
---|
683 | void World::mainLoop() |
---|
684 | { |
---|
685 | this->lastFrame = SDL_GetTicks (); |
---|
686 | printf("World::mainLoop() - Entering main loop of '%s'\n", this->worldName); |
---|
687 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
688 | { |
---|
689 | PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize()); |
---|
690 | // Network |
---|
691 | this->synchronize (); |
---|
692 | // Process input |
---|
693 | this->handleInput (); |
---|
694 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
695 | break; |
---|
696 | // Process time |
---|
697 | this->tick (); |
---|
698 | // Update the state |
---|
699 | this->update (); |
---|
700 | // Process collision |
---|
701 | this->collide (); |
---|
702 | // Draw |
---|
703 | this->display (); |
---|
704 | |
---|
705 | // for( int i = 0; i < 5000000; i++) {} |
---|
706 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
707 | } |
---|
708 | PRINTF(3)("World::mainLoop() - Exiting the main loop\n"); |
---|
709 | } |
---|
710 | |
---|
711 | |
---|
712 | /** |
---|
713 | \brief synchronize local data with remote data |
---|
714 | */ |
---|
715 | void World::synchronize () |
---|
716 | { |
---|
717 | // Get remote input |
---|
718 | // Update synchronizables |
---|
719 | } |
---|
720 | |
---|
721 | |
---|
722 | /** |
---|
723 | \brief run all input processing |
---|
724 | |
---|
725 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
726 | sdl and has its own event-passing-queue. |
---|
727 | */ |
---|
728 | void World::handleInput () |
---|
729 | { |
---|
730 | // localinput |
---|
731 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
732 | cn->process(); |
---|
733 | // remoteinput |
---|
734 | } |
---|
735 | |
---|
736 | |
---|
737 | /** |
---|
738 | \brief advance the timeline |
---|
739 | |
---|
740 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
741 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
742 | a heart-beat. |
---|
743 | */ |
---|
744 | void World::tick () |
---|
745 | { |
---|
746 | Uint32 currentFrame = SDL_GetTicks(); |
---|
747 | if(!this->bPause) |
---|
748 | { |
---|
749 | this->dt = currentFrame - this->lastFrame; |
---|
750 | |
---|
751 | if( this->dt > 0) |
---|
752 | { |
---|
753 | float fps = 1000/dt; |
---|
754 | PRINTF(3)("fps = %f\n", fps); |
---|
755 | } |
---|
756 | else |
---|
757 | { |
---|
758 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
759 | nothing. |
---|
760 | */ |
---|
761 | PRINTF(2)("fps = 1000 - frame rate is adjusted\n"); |
---|
762 | SDL_Delay(10); |
---|
763 | this->dt = 10; |
---|
764 | } |
---|
765 | //this->timeSlice (dt); |
---|
766 | |
---|
767 | /* function to let all entities tick (iterate through list) */ |
---|
768 | float seconds = this->dt / 1000.0; |
---|
769 | this->gameTime += seconds; |
---|
770 | //entity = entities->enumerate(); |
---|
771 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
772 | WorldEntity* entity = iterator->nextElement(); |
---|
773 | while( entity != NULL) |
---|
774 | { |
---|
775 | entity->tick (seconds); |
---|
776 | entity = iterator->nextElement(); |
---|
777 | } |
---|
778 | delete iterator; |
---|
779 | //skySphere->updatePosition(localCamera->absCoordinate); |
---|
780 | |
---|
781 | /* update tick the rest */ |
---|
782 | this->trackManager->tick(this->dt); |
---|
783 | assert( this->localCamera != NULL); |
---|
784 | assert( this->trackManager != NULL); |
---|
785 | this->localCamera->tick(this->dt); |
---|
786 | this->garbageCollector->tick(seconds); |
---|
787 | this->simpleAnimation->tick(seconds); |
---|
788 | } |
---|
789 | this->lastFrame = currentFrame; |
---|
790 | } |
---|
791 | |
---|
792 | |
---|
793 | /** |
---|
794 | \brief this function gives the world a consistant state |
---|
795 | |
---|
796 | after ticking (updating the world state) this will give a constistant |
---|
797 | state to the whole system. |
---|
798 | */ |
---|
799 | void World::update() |
---|
800 | { |
---|
801 | this->garbageCollector->update(); |
---|
802 | this->nullParent->update (dt); |
---|
803 | } |
---|
804 | |
---|
805 | |
---|
806 | /** |
---|
807 | \brief render the current frame |
---|
808 | |
---|
809 | clear all buffers and draw the world |
---|
810 | */ |
---|
811 | void World::display () |
---|
812 | { |
---|
813 | // clear buffer |
---|
814 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
815 | // set camera |
---|
816 | this->localCamera->apply (); |
---|
817 | // draw world |
---|
818 | this->draw(); |
---|
819 | // draw HUD |
---|
820 | /* \todo draw HUD */ |
---|
821 | // flip buffers |
---|
822 | SDL_GL_SwapBuffers(); |
---|
823 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
824 | //SDL_Flip (screen); |
---|
825 | } |
---|
826 | |
---|
827 | |
---|
828 | /** |
---|
829 | \brief add and spawn a new entity to this world |
---|
830 | \param entity to be added |
---|
831 | */ |
---|
832 | void World::spawn(WorldEntity* entity) |
---|
833 | { |
---|
834 | this->entities->add (entity); |
---|
835 | entity->postSpawn (); |
---|
836 | } |
---|
837 | |
---|
838 | |
---|
839 | /** |
---|
840 | \brief add and spawn a new entity to this world |
---|
841 | \param entity to be added |
---|
842 | \param absCoor At what coordinates to add this entity. |
---|
843 | \param absDir In which direction should it look. |
---|
844 | */ |
---|
845 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
846 | { |
---|
847 | this->entities->add (entity); |
---|
848 | |
---|
849 | entity->setAbsCoor (absCoor); |
---|
850 | entity->setAbsDir (absDir); |
---|
851 | |
---|
852 | entity->postSpawn (); |
---|
853 | } |
---|
854 | |
---|
855 | |
---|
856 | /** |
---|
857 | \brief add and spawn a new entity to this world |
---|
858 | \param entity to be added |
---|
859 | \param entity to be added to (PNode) |
---|
860 | \param At what relative coordinates to add this entity. |
---|
861 | \param In which relative direction should it look. |
---|
862 | */ |
---|
863 | void World::spawn(WorldEntity* entity, PNode* parentNode, |
---|
864 | Vector* relCoor, Quaternion* relDir, |
---|
865 | int parentingMode) |
---|
866 | { |
---|
867 | this->nullParent = NullParent::getInstance(); |
---|
868 | if( parentNode != NULL) |
---|
869 | { |
---|
870 | parentNode->addChild (entity); |
---|
871 | |
---|
872 | entity->setRelCoor (relCoor); |
---|
873 | entity->setRelDir (relDir); |
---|
874 | entity->setMode(parentingMode); |
---|
875 | |
---|
876 | this->entities->add (entity); |
---|
877 | |
---|
878 | entity->postSpawn (); |
---|
879 | } |
---|
880 | } |
---|
881 | |
---|
882 | |
---|
883 | |
---|
884 | /** |
---|
885 | \brief commands that the world must catch |
---|
886 | \returns false if not used by the world |
---|
887 | */ |
---|
888 | bool World::command(Command* cmd) |
---|
889 | { |
---|
890 | if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL); |
---|
891 | else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND); |
---|
892 | else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT); |
---|
893 | else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT); |
---|
894 | else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT); |
---|
895 | else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP); |
---|
896 | |
---|
897 | return false; |
---|
898 | } |
---|
899 | |
---|
900 | void World::setPath( char* name) |
---|
901 | { |
---|
902 | path = name; |
---|
903 | } |
---|
904 | |
---|
905 | char* World::getPath() |
---|
906 | { |
---|
907 | return path; |
---|
908 | } |
---|